Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using UnityEngine;
using System.Collections;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class DisappearAttachedFieldAttribute : PropertyAttribute{}
#if UNITY_EDITOR
[CustomPropertyDrawer( typeof ( DisappearAttachedFieldAttribute ) )]
public class DisappearAttachedField : PropertyDrawer
{
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
{
if (property.propertyType == SerializedPropertyType.ObjectReference) {
if( property.objectReferenceValue == null ){
EditorGUI.PropertyField(position, property, label, true);
}
}
}
public override float GetPropertyHeight (SerializedProperty property, GUIContent label)
{
if (property.objectReferenceValue != null) {
return 0;
}
return base.GetPropertyHeight (property, label);
}
}
#endif
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(SphereCollider))]
public class GetRigidbody : MonoBehaviour {
[SerializeField, DisappearAttachedField]
protected Rigidbody rigidbody = null;
[SerializeField, DisappearAttachedField]
protected Transform transform = null;
[SerializeField, DisappearAttachedField]
protected Collider collider = null;
[SerializeField, DisappearAttachedField]
protected Renderer renderer = null;
// Use this for initialization
void Reset () {
rigidbody = GetComponent<Rigidbody>();
transform = GetComponent<Transform>();
collider = GetComponent<Collider>();
renderer = GetComponent<Renderer>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment