ParticleEffectForUGUI/Samples~/Performance Demo/NanoMonitor/Scripts/Utilities/NumericProperty.cs

237 lines
8.4 KiB
C#
Raw Normal View History

2022-06-14 12:39:13 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
2023-08-17 08:43:02 +08:00
using UnityEngine;
2022-06-14 12:39:13 +08:00
namespace Coffee.NanoMonitor
{
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(NumericProperty))]
internal sealed class NumericPropertyDrawer : PropertyDrawer
{
private static Action<PropertyInfo> s_OnMenuSelected;
private static GenericMenu s_PropertyMenu;
private static readonly Dictionary<Type, string> s_SupportedTypes = new Dictionary<Type, string>
{
2023-08-17 08:43:02 +08:00
{ typeof(bool), "bool" },
{ typeof(sbyte), "sbyte" },
{ typeof(short), "short" },
{ typeof(int), "int" },
{ typeof(long), "long" },
{ typeof(byte), "byte" },
{ typeof(ushort), "ushort" },
{ typeof(uint), "uint" },
{ typeof(ulong), "ulong" },
{ typeof(float), "float" },
{ typeof(double), "double" },
{ typeof(decimal), "decimal" }
2022-06-14 12:39:13 +08:00
};
private static void Init()
{
if (s_PropertyMenu != null) return;
2023-08-17 08:43:02 +08:00
const BindingFlags bindingFlags = BindingFlags.Public
| BindingFlags.NonPublic
| BindingFlags.Static
| BindingFlags.GetProperty;
2022-06-14 12:39:13 +08:00
var properties = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes())
2023-08-17 08:43:02 +08:00
.SelectMany(type => type.GetProperties(bindingFlags))
2022-06-14 12:39:13 +08:00
.Where(pi => pi.GetMethod != null && s_SupportedTypes.ContainsKey(pi.PropertyType))
.OrderBy(pi => ConvertToMenuItem(pi, false))
.ToArray();
s_PropertyMenu = new GenericMenu();
2023-08-17 08:43:02 +08:00
s_PropertyMenu.AddItem(new GUIContent("No Property"), false,
arg => s_OnMenuSelected?.Invoke(arg as PropertyInfo), null);
s_PropertyMenu.AddItem(new GUIContent("(Non Public Properties)/"), false, () => { });
2022-06-14 12:39:13 +08:00
s_PropertyMenu.AddSeparator("");
foreach (var pi in properties)
{
2023-08-17 08:43:02 +08:00
s_PropertyMenu.AddItem(new GUIContent(ConvertToMenuItem(pi, true)), false,
arg => s_OnMenuSelected?.Invoke(arg as PropertyInfo), pi);
2022-06-14 12:39:13 +08:00
}
}
private static string ConvertToMenuItem(PropertyInfo p, bool propertyType)
{
var type = p.DeclaringType;
if (type == null) return "";
2023-08-17 08:43:02 +08:00
var category = p.GetMethod.IsPublic && type.IsPublic
? ""
: "(Non Public Properties)/";
2022-06-14 12:39:13 +08:00
var typeName = type.FullName;
var asmName = type.Assembly.GetName().Name;
if (asmName == "UnityEngine.CoreModule")
2023-08-17 08:43:02 +08:00
{
2022-06-14 12:39:13 +08:00
asmName = "UnityEngine";
2023-08-17 08:43:02 +08:00
}
2022-06-14 12:39:13 +08:00
return propertyType
? $"{category}{asmName}/{typeName}/{s_SupportedTypes[p.PropertyType]} {p.Name}"
: $"{category}{asmName}/{typeName}/{p.Name}";
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
Init();
label = EditorGUI.BeginProperty(position, label, property);
var path = property.FindPropertyRelative("m_Path");
var split = path.stringValue.Split(';', ' ', ',');
var name = split.Length == 4 ? $"{split[0]}.{split[3]}" : "No Property";
position = EditorGUI.PrefixLabel(position, label);
if (GUI.Button(position, name, EditorStyles.popup))
{
s_OnMenuSelected = p =>
{
2023-08-17 08:43:02 +08:00
path.stringValue = p == null
? ""
: $"{p.DeclaringType?.FullName}, {p.DeclaringType?.Assembly.GetName().Name};{p.Name}";
2022-06-14 12:39:13 +08:00
property.serializedObject.ApplyModifiedProperties();
};
s_PropertyMenu.DropDown(position);
}
EditorGUI.EndProperty();
}
}
#endif
[Serializable]
public class NumericProperty : ISerializationCallbackReceiver
{
//################################
// Serialized Members.
//################################
[SerializeField] private string m_Path = "";
//################################
2023-08-17 08:43:02 +08:00
// Private Members.
2022-06-14 12:39:13 +08:00
//################################
2023-08-17 08:43:02 +08:00
private Func<double> _get;
void ISerializationCallbackReceiver.OnBeforeSerialize()
2022-06-14 12:39:13 +08:00
{
2023-08-17 08:43:02 +08:00
}
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
var pInfo = GetPropertyInfo(m_Path);
_get = CreateFunc(pInfo?.GetMethod);
2022-06-14 12:39:13 +08:00
}
//################################
2023-08-17 08:43:02 +08:00
// Public Members.
2022-06-14 12:39:13 +08:00
//################################
2023-08-17 08:43:02 +08:00
public double Get()
{
return _get?.Invoke() ?? -1;
}
2022-06-14 12:39:13 +08:00
private static PropertyInfo GetPropertyInfo(string path)
{
var p = path.Split(';');
if (p.Length != 2) return null;
var type = Type.GetType(p[0]);
if (type == null)
{
2023-08-17 08:43:02 +08:00
Debug.LogException(new Exception($"Type '{p[0]}' is not found"));
2022-06-14 12:39:13 +08:00
return null;
}
var pInfo = type.GetProperty(p[1], BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Static);
if (pInfo == null)
{
2023-08-17 08:43:02 +08:00
Debug.LogException(new Exception($"Member '{p[1]}' is not found in type '{type}'"));
2022-06-14 12:39:13 +08:00
}
return pInfo;
}
private static Func<double> CreateFunc(MethodInfo mInfo)
{
if (mInfo == null) return null;
switch (Type.GetTypeCode(mInfo.ReturnType))
{
case TypeCode.Boolean:
2023-08-17 08:43:02 +08:00
{
var func = (Func<bool>)mInfo.CreateDelegate(typeof(Func<bool>));
return () => func() ? 1 : 0;
}
2022-06-14 12:39:13 +08:00
case TypeCode.Byte:
2023-08-17 08:43:02 +08:00
{
var func = (Func<byte>)mInfo.CreateDelegate(typeof(Func<byte>));
return () => func();
}
2022-06-14 12:39:13 +08:00
case TypeCode.SByte:
2023-08-17 08:43:02 +08:00
{
var func = (Func<sbyte>)mInfo.CreateDelegate(typeof(Func<sbyte>));
return () => func();
}
2022-06-14 12:39:13 +08:00
case TypeCode.UInt16:
2023-08-17 08:43:02 +08:00
{
var func = (Func<ushort>)mInfo.CreateDelegate(typeof(Func<ushort>));
return () => func();
}
2022-06-14 12:39:13 +08:00
case TypeCode.UInt32:
2023-08-17 08:43:02 +08:00
{
var func = (Func<uint>)mInfo.CreateDelegate(typeof(Func<uint>));
return () => func();
}
2022-06-14 12:39:13 +08:00
case TypeCode.UInt64:
2023-08-17 08:43:02 +08:00
{
var func = (Func<ulong>)mInfo.CreateDelegate(typeof(Func<ulong>));
return () => func();
}
2022-06-14 12:39:13 +08:00
case TypeCode.Int16:
2023-08-17 08:43:02 +08:00
{
var func = (Func<short>)mInfo.CreateDelegate(typeof(Func<short>));
return () => func();
}
2022-06-14 12:39:13 +08:00
case TypeCode.Int32:
2023-08-17 08:43:02 +08:00
{
var f = (Func<int>)mInfo.CreateDelegate(typeof(Func<int>));
return () => f();
}
2022-06-14 12:39:13 +08:00
case TypeCode.Int64:
2023-08-17 08:43:02 +08:00
{
var f = (Func<long>)mInfo.CreateDelegate(typeof(Func<long>));
return () => f();
}
2022-06-14 12:39:13 +08:00
case TypeCode.Decimal:
2023-08-17 08:43:02 +08:00
{
var f = (Func<decimal>)mInfo.CreateDelegate(typeof(Func<decimal>));
return () => (double)f();
}
2022-06-14 12:39:13 +08:00
case TypeCode.Double:
2023-08-17 08:43:02 +08:00
{
var f = (Func<double>)mInfo.CreateDelegate(typeof(Func<double>));
return f;
}
2022-06-14 12:39:13 +08:00
case TypeCode.Single:
2023-08-17 08:43:02 +08:00
{
var f = (Func<float>)mInfo.CreateDelegate(typeof(Func<float>));
return () => f();
}
2022-06-14 12:39:13 +08:00
default:
2023-08-17 08:43:02 +08:00
{
var message = $"Method '{mInfo.DeclaringType}.{mInfo.Name} ({mInfo.ReturnType})' is not supported.";
Debug.LogException(new Exception(message));
2022-06-14 12:39:13 +08:00
return null;
2023-08-17 08:43:02 +08:00
}
2022-06-14 12:39:13 +08:00
}
}
}
}