Files
Cielonos/Assets/OtherPlugins/GraphicsCat/Modules/Common/PropertyAttributes/EnableIfDrawer.cs
SoulliesOfficial d15957c719 更新
2025-12-17 04:19:38 -05:00

85 lines
3.1 KiB
C#

#if UNITY_EDITOR
using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
namespace GraphicsCat
{
[CustomPropertyDrawer(typeof(EnableIfAttribute))]
public class EnableIfDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var attr = (EnableIfAttribute)attribute;
bool enabled = IsEnabled(property, attr);
EditorGUI.BeginDisabledGroup(!enabled);
EditorGUI.PropertyField(position, property, label, true);
EditorGUI.EndDisabledGroup();
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUI.GetPropertyHeight(property, label, true);
}
private bool IsEnabled(SerializedProperty property, EnableIfAttribute attr)
{
SerializedProperty targetProp = property.serializedObject.FindProperty(attr.PropertyName);
if (targetProp == null) return true;
object targetVal = null;
switch (targetProp.propertyType)
{
case SerializedPropertyType.Boolean:
targetVal = targetProp.boolValue;
break;
case SerializedPropertyType.Integer:
targetVal = targetProp.intValue;
break;
case SerializedPropertyType.Float:
targetVal = targetProp.floatValue;
break;
case SerializedPropertyType.Enum:
targetVal = targetProp.enumValueIndex;
break;
case SerializedPropertyType.ObjectReference:
targetVal = targetProp.objectReferenceValue;
break;
default:
return true;
}
if (targetProp.propertyType == SerializedPropertyType.ObjectReference && attr.CompareValue == null)
{
return attr.Compare == CompareFunction.Equal ? targetVal == null : targetVal != null;
}
double targetDouble = ConvertToDouble(targetVal);
double compareDouble = ConvertToDouble(attr.CompareValue);
switch (attr.Compare)
{
case CompareFunction.Equal: return targetDouble == compareDouble;
case CompareFunction.NotEqual: return targetDouble != compareDouble;
case CompareFunction.Greater: return targetDouble > compareDouble;
case CompareFunction.GreaterEqual: return targetDouble >= compareDouble;
case CompareFunction.Less: return targetDouble < compareDouble;
case CompareFunction.LessEqual: return targetDouble <= compareDouble;
default: return true;
}
}
private double ConvertToDouble(object val)
{
if (val == null) return 0;
if (val is bool b) return b ? 1 : 0;
if (val is Enum e) return System.Convert.ToDouble(System.Convert.ToInt32(e));
return System.Convert.ToDouble(val);
}
}
}
#endif