Last active
September 3, 2022 08:10
-
-
Save mu-777/8646f03f00a855ab3b8f21ecb1179f3c 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 UnityEngine; | |
using UnityEditor; | |
[CustomPropertyDrawer(typeof(FlagConditionalDisableInInspectorAttribute))] | |
internal sealed class FlagConditionalDisableDrawer : PropertyDrawer | |
{ | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
var attr = base.attribute as FlagConditionalDisableInInspectorAttribute; | |
var prop = property.serializedObject.FindProperty(attr.FlagVariableName); | |
if(prop == null) | |
{ | |
Debug.LogError($"Not found '{attr.FlagVariableName}' property"); | |
EditorGUI.PropertyField(position, property, label, true); | |
EditorGUI.EndDisabledGroup(); | |
} | |
var isDisable = IsDisable(attr, prop); | |
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 FlagConditionalDisableInInspectorAttribute; | |
var prop = property.serializedObject.FindProperty(attr.FlagVariableName); | |
if(attr.ConditionalInvisible && IsDisable(attr, prop)) | |
{ | |
return -EditorGUIUtility.standardVerticalSpacing; | |
} | |
return EditorGUI.GetPropertyHeight(property, true); | |
} | |
private bool IsDisable(FlagConditionalDisableInInspectorAttribute attr, SerializedProperty prop) | |
{ | |
return attr.TrueThenDisable ? prop.boolValue : !prop.boolValue; | |
} | |
} |
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 UnityEngine; | |
public class FlagConditionalDisableInInspectorAttribute : PropertyAttribute | |
{ | |
public readonly string FlagVariableName; | |
public readonly bool TrueThenDisable; | |
public readonly bool ConditionalInvisible; | |
public FlagConditionalDisableInInspectorAttribute(string flagVariableName, | |
bool trueThenDisable = false, | |
bool conditionalInvisible = false) | |
{ | |
this.FlagVariableName = flagVariableName; | |
this.TrueThenDisable = trueThenDisable; | |
this.ConditionalInvisible = conditionalInvisible; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment