您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

71 行
2.5 KiB

using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace NaughtyAttributes.Editor
{
[PropertyDrawer(typeof(EnableIfAttribute))]
public class EnableIfPropertyDrawer : PropertyDrawer
{
public override void DrawProperty(SerializedProperty property)
{
EnableIfAttribute enableIfAttribute = PropertyUtility.GetAttribute<EnableIfAttribute>(property);
UnityEngine.Object target = PropertyUtility.GetTargetObject(property);
List<bool> conditionValues = new List<bool>();
foreach (var condition in enableIfAttribute.Conditions)
{
FieldInfo conditionField = ReflectionUtility.GetField(target, condition);
if (conditionField != null &&
conditionField.FieldType == typeof(bool))
{
conditionValues.Add((bool)conditionField.GetValue(target));
}
MethodInfo conditionMethod = ReflectionUtility.GetMethod(target, condition);
if (conditionMethod != null &&
conditionMethod.ReturnType == typeof(bool) &&
conditionMethod.GetParameters().Length == 0)
{
conditionValues.Add((bool)conditionMethod.Invoke(target, null));
}
}
if (conditionValues.Count > 0)
{
bool enabled;
if (enableIfAttribute.ConditionOperator == ConditionOperator.And)
{
enabled = true;
foreach (var value in conditionValues)
{
enabled = enabled && value;
}
}
else
{
enabled = false;
foreach (var value in conditionValues)
{
enabled = enabled || value;
}
}
if (enableIfAttribute.Reversed)
{
enabled = !enabled;
}
GUI.enabled = enabled;
EditorDrawUtility.DrawPropertyField(property);
GUI.enabled = true;
}
else
{
string warning = enableIfAttribute.GetType().Name + " needs a valid boolean condition field or method name to work";
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: target);
}
}
}
}