12using System.Collections.Generic;
26 Where(t => t !=
null).
42 private bool _canCallSpecifyFunctions =
false;
43 private GUIStyle _boldFoldoutStyle;
57 throwIfNotInOnEnable(
"specifyCustomDrawer");
70 throwIfNotInOnEnable(
"specifyCustomDecorator");
76 List<Action<SerializedProperty>> list;
78 list =
new List<Action<SerializedProperty>>();
82 list.Add(decoratorDrawer);
91 throwIfNotInOnEnable(
"specifyCustomPostDecorator");
97 List<Action<SerializedProperty>> list;
99 list =
new List<Action<SerializedProperty>>();
103 list.Add(decoratorDrawer);
113 throwIfNotInOnEnable(
"specifyConditionalDrawing");
119 SerializedProperty conditionalProp = serializedObject.FindProperty(conditionalName);
121 if (conditionalProp.hasMultipleDifferentValues) {
124 return conditionalProp.boolValue;
126 }, dependantProperties);
130 throwIfNotInOnEnable(
"specifyConditionalDrawing");
136 SerializedProperty enumProp = serializedObject.FindProperty(enumName);
138 if (enumProp.hasMultipleDifferentValues) {
141 return enumProp.intValue == enumValue;
143 }, dependantProperties);
147 throwIfNotInOnEnable(
"hideField");
153 throwIfNotInOnEnable(
"specifyConditionalDrawing");
155 for (
int i = 0; i < dependantProperties.Length; i++) {
156 string dependant = dependantProperties[i];
162 List<Func<bool>> list;
164 list =
new List<Func<bool>>();
167 list.Add(conditional);
177 throwIfNotInOnEnable(
"deferProperty");
190 throwIfNotInOnEnable(
"addPropertyToFoldout");
196 list =
new List<string>();
209 foreach (
string property
in foldout.Value) {
210 if (property.Equals(propertyName)) {
isInFoldout =
true;
break; }
218 var scriptProp = serializedObject.FindProperty(
"m_Script");
219 EditorGUI.BeginDisabledGroup(disable);
220 EditorGUILayout.PropertyField(scriptProp);
221 EditorGUI.EndDisabledGroup();
226 if (serializedObject ==
null) { }
227 }
catch (NullReferenceException) {
228 DestroyImmediate(
this);
229 throw new Exception(
"Cleaning up an editor of type " + GetType() +
". Make sure to always destroy your editors when you are done with them!");
239 _canCallSpecifyFunctions =
true;
243 if (serializedObject.FindProperty(propertyName) ==
null) {
244 Debug.LogWarning(
"Property " + propertyName +
" does not exist, was it removed or renamed?");
256 if (_boldFoldoutStyle ==
null) {
257 _boldFoldoutStyle =
new GUIStyle(EditorStyles.foldout);
258 _boldFoldoutStyle.fontStyle = FontStyle.Bold;
261 _canCallSpecifyFunctions =
false;
264 SerializedProperty iterator = serializedObject.GetIterator();
267 while (iterator.NextVisible(isFirst)) {
278 using (
new EditorGUI.DisabledGroupScope(isFirst)) {
279 drawProperty(iterator);
287 drawProperty(serializedObject.FindProperty(deferredProperty));
293 EditorGUILayout.Foldout(
_foldoutStates[foldout.Key], foldout.Key, _boldFoldoutStyle);
296 foreach (var property
in foldout.Value) {
298 drawProperty(serializedObject.FindProperty(property));
302 foreach (var property
in foldout.Value) {
304 drawProperty(serializedObject.FindProperty(property));
310 serializedObject.ApplyModifiedProperties();
313 private void drawProperty(SerializedProperty property) {
314 List<Func<bool>> conditionalList;
317 for (
int i = 0; i < conditionalList.Count; i++) {
318 allTrue &= conditionalList[i]();
325 Action<SerializedProperty> customDrawer;
327 List<Action<SerializedProperty>> decoratorList;
329 for (
int i = 0; i < decoratorList.Count; i++) {
330 decoratorList[i](property);
334 EditorGUI.BeginChangeCheck();
337 customDrawer(property);
339 EditorGUILayout.PropertyField(property,
true);
342 if (EditorGUI.EndChangeCheck()) {
347 List<Action<SerializedProperty>> postDecoratorList;
349 for (
int i = 0; i < postDecoratorList.Count; i++) {
350 postDecoratorList[i](property);
355 private void throwIfNotInOnEnable(
string methodName) {
356 if (!_canCallSpecifyFunctions) {
357 throw new InvalidOperationException(
"Cannot call " + methodName +
" from within any other function but OnEnable. Make sure you also call base.OnEnable as well!");
void specifyConditionalDrawing(string conditionalName, params string[] dependantProperties)
Specify a list of properties that should only be displayed if the conditional property has a value of...
void dontShowScriptField()
bool isInFoldout(string propertyName)
Check whether a property is inside of a foldout drop-down.
bool validateProperty(string propertyName)
void deferProperty(string propertyName)
Defer rendering of a property until the end of the inspector. Deferred properties are drawn in the RE...
List< string > _deferredProperties
void specifyConditionalDrawing(Func< bool > conditional, params string[] dependantProperties)
Dictionary< string, bool > _foldoutStates
void hideField(string propertyName)
Dictionary< string, List< Action< SerializedProperty > > > _specifiedDecorators
void drawScriptField(bool disable=true)
Dictionary< string, List< Action< SerializedProperty > > > _specifiedPostDecorators
void addPropertyToFoldout(string propertyName, string foldoutName, bool foldoutStartOpen=false)
Condition the drawing of a property based on the status of a foldout drop-down.
void specifyCustomPostDecorator(string propertyName, Action< SerializedProperty > decoratorDrawer)
Specify a callback to be used to draw a decorator AFTER a specific named property.
void specifyCustomDrawer(string propertyName, Action< SerializedProperty > propertyDrawer)
Specify a callback to be used to draw a specific named property. Should be called in OnEnable.
Dictionary< string, List< Func< bool > > > _conditionalProperties
Dictionary< string, Action< SerializedProperty > > _specifiedDrawers
List< SerializedProperty > _modifiedProperties
void specifyCustomDecorator(string propertyName, Action< SerializedProperty > decoratorDrawer)
Specify a callback to be used to draw a decorator for a specific named property. Should be called in ...
Dictionary< string, List< string > > _foldoutProperties
override void OnInspectorGUI()
void specifyConditionalDrawing(string enumName, int enumValue, params string[] dependantProperties)