80 lines
3.2 KiB
C#
80 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using Object = UnityEngine.Object;
|
|
|
|
namespace Module.Inspector.Editor.Utilities
|
|
{
|
|
internal static class EditorHiddenFieldUtility
|
|
{
|
|
private static Dictionary<Type, FieldInfo[]> CACHED_TYPE_TO_PRIMARIES;
|
|
|
|
public static FieldInfo[] Query(Object target)
|
|
{
|
|
Type type = target.GetType();
|
|
var att = type.GetCustomAttribute<EnableShowHiddenFieldsAttribute>();
|
|
|
|
if (att == null)
|
|
return new FieldInfo[0];
|
|
|
|
if (CACHED_TYPE_TO_PRIMARIES == null)
|
|
CACHED_TYPE_TO_PRIMARIES = new Dictionary<Type, FieldInfo[]>();
|
|
|
|
if (CACHED_TYPE_TO_PRIMARIES.TryGetValue(type, out FieldInfo[] fields))
|
|
return fields;
|
|
|
|
fields = InternalFetch(type, att.UseFieldProperty);
|
|
CACHED_TYPE_TO_PRIMARIES.Add(type, fields);
|
|
return fields;
|
|
}
|
|
|
|
public static float CalculateHeight(FieldInfo[] fields)
|
|
{
|
|
return EditorGUIUtility.singleLineHeight * fields.Length;
|
|
}
|
|
|
|
public static void Draw(Rect rect, FieldInfo field, Object target)
|
|
{
|
|
bool prevEnabled = GUI.enabled;
|
|
GUI.enabled = false;
|
|
string nicifiedName = ObjectNames.NicifyVariableName(field.Name);
|
|
|
|
if (field.FieldType == typeof(float))
|
|
EditorGUI.FloatField(rect, nicifiedName, (float)field.GetValue(target));
|
|
else if (field.FieldType == typeof(int))
|
|
EditorGUI.IntField(rect, nicifiedName, (int)field.GetValue(target));
|
|
else if (field.FieldType == typeof(double))
|
|
EditorGUI.DoubleField(rect, nicifiedName, (double)field.GetValue(target));
|
|
else if (field.FieldType == typeof(string))
|
|
EditorGUI.TextField(rect, nicifiedName, (string)field.GetValue(target));
|
|
else if (typeof(Object).IsAssignableFrom(field.FieldType))
|
|
EditorGUI.ObjectField(rect, nicifiedName, (Object)field.GetValue(target), typeof(Object), true);
|
|
else
|
|
EditorGUI.LabelField(rect, ObjectNames.NicifyVariableName(field.Name), "Unsupported type");
|
|
|
|
GUI.enabled = prevEnabled;
|
|
}
|
|
|
|
private static FieldInfo[] InternalFetch(Type type, bool useFieldProperty)
|
|
{
|
|
const BindingFlags FLAGS = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
|
|
|
|
FieldInfo[] fields = type.GetFields(FLAGS);
|
|
var list = new List<FieldInfo>(fields.Length);
|
|
|
|
for (var i = 0; i < fields.Length; i++)
|
|
{
|
|
FieldInfo fi = fields[i];
|
|
|
|
if (fi.GetCustomAttribute<NonSerializedAttribute>() == null && fi.GetCustomAttribute<HideInInspector>() == null)
|
|
continue;
|
|
if (!useFieldProperty || fi.GetCustomAttribute<ShowHiddenFieldAttribute>() != null)
|
|
list.Add(fi);
|
|
}
|
|
|
|
return list.ToArray();
|
|
}
|
|
}
|
|
} |