Net.Like.Xue.Tokyo/Assets/Plugins/Le Tai's Asset/TranslucentImage/Script/Editor/EditorProperty.cs

77 lines
2.9 KiB
C#
Raw Normal View History

2024-11-03 16:42:23 +08:00
using System;
using System.Globalization;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace LeTai.Asset.TranslucentImage.Editor
{
public class EditorProperty
{
public readonly SerializedProperty serializedProperty;
readonly SerializedObject serializedObject;
readonly MethodInfo propertySetter;
readonly SerializedProperty dirtyFlag;
public EditorProperty(SerializedObject obj, string name)
{
var propertyName = char.ToLowerInvariant(name[0]) + name.Substring(1);
serializedObject = obj;
serializedProperty = serializedObject.FindProperty(propertyName);
propertySetter = serializedObject.targetObject.GetType().GetProperty(name).SetMethod;
dirtyFlag = serializedObject.FindProperty("modifiedFromInspector");
}
public void Draw(params GUILayoutOption[] options)
{
using (var scope = new EditorGUI.ChangeCheckScope())
{
EditorGUILayout.PropertyField(serializedProperty, options);
if (!scope.changed)
return;
if (dirtyFlag != null)
dirtyFlag.boolValue = true;
serializedObject.ApplyModifiedProperties();
foreach (var target in serializedObject.targetObjects)
{
switch (serializedProperty.propertyType)
{
case SerializedPropertyType.ObjectReference:
propertySetter.Invoke(target, new object[] { serializedProperty.objectReferenceValue });
break;
case SerializedPropertyType.Float:
propertySetter.Invoke(target, new object[] { serializedProperty.floatValue });
break;
case SerializedPropertyType.Integer:
propertySetter.Invoke(target, new object[] { serializedProperty.intValue });
break;
case SerializedPropertyType.Rect:
propertySetter.Invoke(target, new object[] { serializedProperty.rectValue });
break;
case SerializedPropertyType.Enum:
propertySetter.Invoke(target, new object[] { serializedProperty.enumValueIndex });
break;
case SerializedPropertyType.Boolean:
propertySetter.Invoke(target, new object[] { serializedProperty.boolValue });
break;
case SerializedPropertyType.Color:
propertySetter.Invoke(target, new object[] { serializedProperty.colorValue });
break;
case SerializedPropertyType.Generic:
// Not needed for now
break;
default: throw new NotImplementedException($"Type {serializedProperty.propertyType} is not implemented");
}
}
serializedObject.Update();
}
}
}
}