Skip to content

Instantly share code, notes, and snippets.

@rutcreate
Created May 19, 2015 02:29
Show Gist options
  • Save rutcreate/80e1359057fa93e51072 to your computer and use it in GitHub Desktop.
Save rutcreate/80e1359057fa93e51072 to your computer and use it in GitHub Desktop.
Unity3D Editor : How to get other fields in PropertyDrawer.OnGUI
using UnityEngine;
public class MyProperty : PropertyAttribute {
public int intValue = 33;
public float floatValue = 12.5f;
public bool boolValue = true;
public MyProperty() {}
public MyProperty(int intValue, float floatValue, bool boolValue) {
this.intValue = intValue;
this.floatValue = floatValue;
this.boolValue = boolValue;
}
}
using UnityEditor;
[CustomPropertyDrawer(typeof(MyProperty))]
public class MyPropertyDrawer : PropertyDrawer {
private float lineHeight = 16f;
public override float GetPropertyHeight (SerializedProperty property, GUIContent label) {
return base.GetPropertyHeight (property, label) + lineHeight * 5;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
MyProperty myProperty = attribute as MyProperty;
EditorGUI.LabelField(position, "Property Int: " + myProperty.intValue);
position.y += lineHeight;
EditorGUI.LabelField(position, "Property Float: " + myProperty.floatValue);
position.y += lineHeight;
EditorGUI.LabelField(position, "Property Bool: " + myProperty.boolValue);
// Get float value.
SerializedProperty floatProperty = property.serializedObject.FindProperty("myFloat");
position.y += lineHeight;
EditorGUI.LabelField(position, "myFloat: " + floatProperty.floatValue);
// Get int value.
SerializedProperty intProperty = property.serializedObject.FindProperty("myInt");
position.y += lineHeight;
EditorGUI.LabelField(position, "myInt: " + intProperty.intValue);
// Get string value.
SerializedProperty stringProperty = property.serializedObject.FindProperty("myString");
position.y += lineHeight;
EditorGUI.LabelField(position, "myString: " + stringProperty.stringValue);
// Get object.
SerializedProperty objectProperty = property.serializedObject.FindProperty("gameObject");
GameObject gameObject = objectProperty.objectReferenceValue as GameObject;
}
}
using UnityEngine;
public class MyScript : MonoBehaviour {
public float myFloat = 1.2f;
public int myInt = 1f;
public string myString = "myString";
public GameObject gameObject;
[MyProperty]
public int useByDrawer = 20;
[MyProperty(1, 2.5f, false)]
public int useByDrawer = 20;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment