Skip to content

Instantly share code, notes, and snippets.

@jonbro
Created November 5, 2014 23:16
Show Gist options
  • Select an option

  • Save jonbro/beee59cbbf0475234dd0 to your computer and use it in GitHub Desktop.

Select an option

Save jonbro/beee59cbbf0475234dd0 to your computer and use it in GitHub Desktop.
propertydrawereditor
// C# example.
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomPropertyDrawer(typeof(ActionInput))]
public class ActionInputDrawer : PropertyDrawer {
public int rows = 1;
// Draw the property inside the given rect
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
// Using BeginProperty / EndProperty on the parent property means that
// prefab override logic works on the entire property.
// calculate the height of the control based on the the type of control
int height = 1;
rows = 1;
float originalHeight = position.height;
if (property.FindPropertyRelative ("action").enumValueIndex == (int)ActionInput.ActionType.PRESS_KEY) {
height = 2;
rows = 2;
}
position.height *= height;
EditorGUI.BeginProperty(position, label, property);
// Draw label
// position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
// Don't make child fields be indented
int indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
// Calculate rects
Rect amountRect = new Rect(position.x, position.y, position.width, originalHeight);
Rect secondRect = new Rect(position.x, position.y+originalHeight, position.width, originalHeight);
// Draw fields - passs GUIContent.none to each so they are drawn without labels
EditorGUI.PropertyField (amountRect, property.FindPropertyRelative ("action"), GUIContent.none);
switch ((ActionInput.ActionType)property.FindPropertyRelative ("action").enumValueIndex) {
case ActionInput.ActionType.PRESS_KEY:
EditorGUI.PropertyField (secondRect, property.FindPropertyRelative ("keyToPress"), GUIContent.none);
break;
case ActionInput.ActionType.OBJECT_ACTIVE:
EditorGUI.PropertyField (secondRect, property.FindPropertyRelative ("objectCheck"), GUIContent.none);
break;
}
// Set indent back to what it was
EditorGUI.indentLevel = indent;
EditorGUI.EndProperty();
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return rows*20;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment