1.7.0: Added option to display hidden fields for a class

This commit is contained in:
Anders Ejlersen 2022-07-23 22:56:43 +02:00
parent 9faacb6291
commit 070b82767f
12 changed files with 200 additions and 24 deletions

View file

@ -0,0 +1,80 @@
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();
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 83e464a05c8246f2a9e67edd640b5b6c
timeCreated: 1658598364

View file

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
namespace Module.Inspector.Editor.Utilities
{
@ -79,6 +80,19 @@ namespace Module.Inspector.Editor.Utilities
return result;
}
public static float CalculateHeight(UnityEngine.Object target, ResultPrimary[] primaries)
{
float total = EditorGUIUtility.singleLineHeight;
for (var i = 0; i < primaries.Length; i++)
{
ResultPrimary primary = primaries[i];
total += primary.drawer.GetHeight(target, primary.methodInfo);
}
return total;
}
private static ResultPrimary[] InternalFetchPrimary(Type type)
{