VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
AVR_Attributes_PropertyDrawers.cs
1 using UnityEditor;
2 using UnityEngine;
4 
5 namespace AVR.Core.Attributes {
6  [CustomPropertyDrawer(typeof(ReadOnly))]
7  public class ReadOnlyDrawer : PropertyDrawer
8  {
9  public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
10  return EditorGUI.GetPropertyHeight(property, label, true);
11  }
12 
13  public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
14  GUI.enabled = false;
15  EditorGUI.PropertyField(position, property, label, true);
16  GUI.enabled = true;
17  }
18  }
19 
20  [CustomPropertyDrawer(typeof(FoldoutGroup))]
21  public class FoldoutPropertyDrawer : PropertyDrawer {
22  private static bool last_foldout = false;
23 
24 #nullable enable
25  // Here we store the group of the last drawn FoldoutProperty
26  private static string? last_group = null;
27 
28  // Here we store a string-based reference for some property. We use this as a cycle-reference to determine when to reset last_group
29  private static string? prop_reference = null;
30 #nullable disable
31 
32  // Here we store a reference to the last object for which we drew a FoldoutProperty. We use this to determine when to reset prop_reference
33  private static Object obj_reference = null;
34 
35  private bool foldout = false;
36 
37  public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
38  {
39  return 0;
40  }
41 
42  private void NewGroup(string group_name, SerializedProperty prop) {
43  last_group = group_name;
44 
45  // Begin foldout
46  foldout = EditorGUILayout.Foldout(foldout, group_name);
47  EditorGUI.indentLevel++;
48  if (foldout) EditorGUILayout.PropertyField(prop, new GUIContent(prop.displayName));
49  EditorGUI.indentLevel--;
50 
51  last_foldout = foldout;
52  }
53 
54  private void ContinuationGroup(SerializedProperty prop) {
55  if (last_foldout)
56  {
57  EditorGUI.indentLevel++;
58  EditorGUILayout.PropertyField(prop, new GUIContent(prop.displayName));
59  EditorGUI.indentLevel--;
60  }
61  foldout = last_foldout;
62  }
63 
64  public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
65  {
66  // TL;DR: If the reference object is changed (by for instance selecting a different object in the inspector, or when drawing multpile objects with foldout-groups)
67  // then we reset the prop-reference and last-group, as not to interfere with the foreign object.
68  if(obj_reference==null) obj_reference = property.serializedObject.targetObject;
69  else if(obj_reference!=property.serializedObject.targetObject) {
70  prop_reference = null;
71  last_group = null;
72  obj_reference = property.serializedObject.targetObject;
73  }
74 
75  // TL;DR: prop_reference acts as a cycle reference. if prop_reference==property.name, that means we are drawing a property we have already drawn previously
76  // meaning, we are in a brand new OnGUI cycle. Respectively, we need to reset the last_group variable.
77  if(prop_reference==null) prop_reference = property.name;
78  else if(prop_reference==property.name) {
79  last_group = null;
80  }
81 
82  // Get attribute
83  FoldoutGroup attr = attribute as FoldoutGroup;
84 
85  if(last_group!=attr.group_id) {
86  NewGroup(attr.group_id, property);
87  }
88  else {
89  ContinuationGroup(property);
90  }
91  }
92  }
93 
94  [CustomPropertyDrawer(typeof(ConditionalHideInInspector))]
95  public class DrawIfPropertyDrawer : PropertyDrawer
96  {
97  // Reference to the attribute on the property.
99 
100  // Field that is being compared.
101  SerializedProperty hideConditionProperty;
102 
103  // Height of the property.
104  private float propertyHeight;
105 
106  public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
107  {
108  return propertyHeight;
109  }
110 
111  public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
112  {
113  // Get attribute
115 
116  // Get property that represents the hide condition
117  SerializedProperty prop = property.serializedObject.FindProperty(attr.hideConditionPropertyName);
118 
119  // Get its value
120  bool val = false;
121 
122  if(prop.type=="bool") {
123  val = prop.boolValue;
124  }
125  else if(prop.type=="float") {
126  val =
127  attr.ctype == ct.BIGGEREQUAL && prop.floatValue >= attr.compareValue ||
128  attr.ctype == ct.BIGGER && prop.floatValue > attr.compareValue ||
129  attr.ctype == ct.SMALLEREQUAL && prop.floatValue <= attr.compareValue ||
130  attr.ctype == ct.SMALLER && prop.floatValue < attr.compareValue ||
131  attr.ctype == ct.EQUAL && prop.floatValue == attr.compareValue ||
132  attr.ctype == ct.UNEQUAL && prop.floatValue != attr.compareValue;
133  }
134  else if (prop.type == "int") {
135  val =
136  attr.ctype == ct.BIGGEREQUAL && prop.intValue >= attr.compareValue ||
137  attr.ctype == ct.BIGGER && prop.intValue > attr.compareValue ||
138  attr.ctype == ct.SMALLEREQUAL && prop.intValue <= attr.compareValue ||
139  attr.ctype == ct.SMALLER && prop.intValue < attr.compareValue ||
140  attr.ctype == ct.EQUAL && prop.intValue == attr.compareValue ||
141  attr.ctype == ct.UNEQUAL && prop.intValue != attr.compareValue;
142  }
143  else if (prop.type == "double") {
144  val =
145  attr.ctype == ct.BIGGEREQUAL && prop.doubleValue >= attr.compareValue ||
146  attr.ctype == ct.BIGGER && prop.doubleValue > attr.compareValue ||
147  attr.ctype == ct.SMALLEREQUAL && prop.doubleValue <= attr.compareValue ||
148  attr.ctype == ct.SMALLER && prop.doubleValue < attr.compareValue ||
149  attr.ctype == ct.EQUAL && prop.doubleValue == attr.compareValue ||
150  attr.ctype == ct.UNEQUAL && prop.doubleValue != attr.compareValue;
151  }
152  else if (prop.type == "long") {
153  val =
154  attr.ctype == ct.BIGGEREQUAL && prop.longValue >= attr.compareValue ||
155  attr.ctype == ct.BIGGER && prop.longValue > attr.compareValue ||
156  attr.ctype == ct.SMALLEREQUAL && prop.longValue <= attr.compareValue ||
157  attr.ctype == ct.SMALLER && prop.longValue < attr.compareValue ||
158  attr.ctype == ct.EQUAL && prop.longValue == attr.compareValue ||
159  attr.ctype == ct.UNEQUAL && prop.longValue != attr.compareValue;
160  }
161  else if (prop.type == "Enum") {
162  val =
163  attr.ctype == ct.BIGGEREQUAL && prop.enumValueIndex >= attr.compareValue ||
164  attr.ctype == ct.BIGGER && prop.enumValueIndex > attr.compareValue ||
165  attr.ctype == ct.SMALLEREQUAL && prop.enumValueIndex <= attr.compareValue ||
166  attr.ctype == ct.SMALLER && prop.enumValueIndex < attr.compareValue ||
167  attr.ctype == ct.EQUAL && prop.enumValueIndex == attr.compareValue ||
168  attr.ctype == ct.UNEQUAL && prop.enumValueIndex != attr.compareValue;
169  }
170  else {
171  val = prop.objectReferenceValue != null;
172  }
173 
174  val ^= attr.invertCondition;
175 
176 
177  if (val)
178  {
179  // Dont draw
180  propertyHeight = 0f;
181  }
182  else
183  {
184  // draw
185  propertyHeight = base.GetPropertyHeight(property, label);
186  EditorGUI.PropertyField(position, property, new GUIContent(property.displayName));
187  }
188  }
189  }
190 }
override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
override float GetPropertyHeight(SerializedProperty property, GUIContent label)
Makes a field read-only in the inspector
Assigns given attributes to a foldout group in the inspector. The way these are drawn is determined b...
override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
Allows for simple hiding of properties in the UnityEditor depending on certain conditions. For instance, in the following example the "type" field will only be displayed in the inspector if the "tracking" field is set to true:
override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
override float GetPropertyHeight(SerializedProperty property, GUIContent label)
void NewGroup(string group_name, SerializedProperty prop)
override float GetPropertyHeight(SerializedProperty property, GUIContent label)