Last active
September 3, 2022 09:17
-
-
Save mu-777/77dd59ad1c764387ec5fca3c185e04c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor; | |
using GetCondFunc = System.Func<UnityEditor.SerializedProperty, ConditionalDisableInInspectorAttribute, bool>; | |
[CustomPropertyDrawer(typeof(ConditionalDisableInInspectorAttribute))] | |
internal sealed class ConditionalDisableDrawer : PropertyDrawer | |
{ | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { | |
var attr = base.attribute as ConditionalDisableInInspectorAttribute; | |
var condProp = property.serializedObject.FindProperty(attr.VariableName); | |
if(condProp == null) { | |
Debug.LogError($"Not found '{attr.VariableName}' property"); | |
EditorGUI.PropertyField(position, property, label, true); | |
} | |
var isDisable = IsDisable(attr, condProp); | |
if(attr.ConditionalInvisible && isDisable) { | |
return; | |
} | |
EditorGUI.BeginDisabledGroup(isDisable); | |
EditorGUI.PropertyField(position, property, label, true); | |
EditorGUI.EndDisabledGroup(); | |
} | |
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { | |
var attr = base.attribute as ConditionalDisableInInspectorAttribute; | |
var prop = property.serializedObject.FindProperty(attr.VariableName); | |
if(attr.ConditionalInvisible && IsDisable(attr, prop)) { | |
return -EditorGUIUtility.standardVerticalSpacing; | |
} | |
return EditorGUI.GetPropertyHeight(property, true); | |
} | |
private bool IsDisable(ConditionalDisableInInspectorAttribute attr, SerializedProperty prop) { | |
GetCondFunc disableCondFunc; | |
if(!DisableCondFuncMap.TryGetValue(attr.VariableType, out disableCondFunc)) { | |
Debug.LogError($"{attr.VariableType} type is not supported"); | |
return false; | |
} | |
return disableCondFunc(prop, attr); | |
} | |
private Dictionary<Type, GetCondFunc> DisableCondFuncMap = new Dictionary<Type, GetCondFunc>() { | |
{ typeof(bool), (prop, attr) => {return attr.TrueThenDisable ? !prop.boolValue : prop.boolValue;} }, | |
{ typeof(string), (prop, attr) => {return attr.TrueThenDisable ? prop.stringValue == attr.ComparedStr : prop.stringValue != attr.ComparedStr;} }, | |
{ typeof(int), (prop, attr) => {return attr.TrueThenDisable ? prop.intValue == attr.ComparedInt : prop.intValue != attr.ComparedInt;} }, | |
{ typeof(float), (prop, attr) => {return attr.TrueThenDisable ? prop.floatValue <= attr.ComparedFloat : prop.floatValue > attr.ComparedFloat;} } | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
public partial class ConditionalDisableInInspectorAttribute : PropertyAttribute | |
{ | |
public readonly string VariableName; | |
public readonly Type VariableType; | |
public readonly bool TrueThenDisable; | |
public readonly bool ConditionalInvisible; | |
public readonly string ComparedStr; | |
public readonly int ComparedInt; | |
public readonly float ComparedFloat; | |
private ConditionalDisableInInspectorAttribute(string variableName, Type variableType, bool trueThenDisable = false, bool conditionalInvisible = false) { | |
this.VariableName = variableName; | |
this.VariableType = variableType; | |
this.TrueThenDisable = trueThenDisable; | |
this.ConditionalInvisible = conditionalInvisible; | |
} | |
public ConditionalDisableInInspectorAttribute(string boolVariableName, bool trueThenDisable = false, bool conditionalInvisible = false) | |
: this(boolVariableName, typeof(bool), trueThenDisable, conditionalInvisible) { } | |
public ConditionalDisableInInspectorAttribute(string strVariableName, string comparedStr, bool notEqualThenEnable = false, bool conditionalInvisible = false) | |
: this(strVariableName, comparedStr.GetType(), notEqualThenEnable, conditionalInvisible) { | |
this.ComparedStr = comparedStr; | |
} | |
public ConditionalDisableInInspectorAttribute(string intVariableName, int comparedInt, bool notEqualThenEnable = false, bool conditionalInvisible = false) | |
: this(intVariableName, comparedInt.GetType(), notEqualThenEnable, conditionalInvisible) { | |
this.ComparedInt = comparedInt; | |
} | |
public ConditionalDisableInInspectorAttribute(string floatVariableName, float comparedFloat, bool greaterThanComparedThenEnable = true, bool conditionalInvisible = false) | |
: this(floatVariableName, comparedFloat.GetType(), greaterThanComparedThenEnable, conditionalInvisible) { | |
this.ComparedFloat = comparedFloat; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment