Skip to content

Instantly share code, notes, and snippets.

@mu-777
Last active September 3, 2022 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mu-777/8646f03f00a855ab3b8f21ecb1179f3c to your computer and use it in GitHub Desktop.
Save mu-777/8646f03f00a855ab3b8f21ecb1179f3c to your computer and use it in GitHub Desktop.
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;
}
}
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