Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created June 29, 2015 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/4bda5bc574e7ac1d5ce2 to your computer and use it in GitHub Desktop.
Save tsubaki/4bda5bc574e7ac1d5ce2 to your computer and use it in GitHub Desktop.
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