1.7.0: Added option to display hidden fields for a class
This commit is contained in:
parent
9faacb6291
commit
070b82767f
|
|
@ -1,4 +1,5 @@
|
|||
using Module.Inspector.Editor.Utilities;
|
||||
using System.Reflection;
|
||||
using Module.Inspector.Editor.Utilities;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
|
|
@ -11,17 +12,21 @@ namespace Module.Inspector.Editor
|
|||
public override void OnInspectorGUI()
|
||||
{
|
||||
DrawDefaultInspector();
|
||||
DrawHiddenInspector();
|
||||
DrawMethodInspector();
|
||||
}
|
||||
|
||||
private void DrawMethodInspector()
|
||||
private void DrawHiddenInspector()
|
||||
{
|
||||
if (!IsValidMethodTarget())
|
||||
if (!IsValidTarget())
|
||||
return;
|
||||
|
||||
EditorMethodUtility.ResultPrimary[] primaries = EditorMethodUtility.QueryPrimary(target);
|
||||
FieldInfo[] fields = EditorHiddenFieldUtility.Query(target);
|
||||
|
||||
float totalHeight = CalculateMethodHeight(primaries);
|
||||
if (fields.Length == 0)
|
||||
return;
|
||||
|
||||
float totalHeight = EditorHiddenFieldUtility.CalculateHeight(fields) + EditorGUIUtility.singleLineHeight * 2;
|
||||
GUILayout.Space(totalHeight);
|
||||
|
||||
var position = new Rect(0, 0, 1, 1);
|
||||
|
|
@ -37,7 +42,48 @@ namespace Module.Inspector.Editor
|
|||
}
|
||||
|
||||
position.y = position.yMax + EditorGUIUtility.singleLineHeight;
|
||||
position.height = 0.0f;
|
||||
position.height = EditorGUIUtility.singleLineHeight;
|
||||
EditorGUI.LabelField(position, "Hidden fields", EditorStyles.miniBoldLabel);
|
||||
|
||||
position.y += EditorGUIUtility.singleLineHeight;
|
||||
|
||||
for (var i = 0; i < fields.Length; i++)
|
||||
{
|
||||
EditorHiddenFieldUtility.Draw(position, fields[i], target);
|
||||
position.y += position.height;
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawMethodInspector()
|
||||
{
|
||||
if (!IsValidTarget())
|
||||
return;
|
||||
|
||||
EditorMethodUtility.ResultPrimary[] primaries = EditorMethodUtility.QueryPrimary(target);
|
||||
|
||||
if (primaries == null || primaries.Length == 0)
|
||||
return;
|
||||
|
||||
float totalHeight = EditorMethodUtility.CalculateHeight(target, primaries) + EditorGUIUtility.singleLineHeight * 2;
|
||||
GUILayout.Space(totalHeight);
|
||||
|
||||
var position = new Rect(0, 0, 1, 1);
|
||||
|
||||
if (Event.current.type == EventType.Repaint)
|
||||
{
|
||||
position = GUILayoutUtility.GetLastRect();
|
||||
position.y -= totalHeight;
|
||||
|
||||
#if UNITY_2020_1_OR_NEWER
|
||||
position.width = EditorGUIUtility.currentViewWidth - 22.0f;
|
||||
#endif
|
||||
}
|
||||
|
||||
position.y = position.yMax + EditorGUIUtility.singleLineHeight;
|
||||
position.height = EditorGUIUtility.singleLineHeight;
|
||||
EditorGUI.LabelField(position, "Buttons", EditorStyles.miniBoldLabel);
|
||||
|
||||
position.y += EditorGUIUtility.singleLineHeight;
|
||||
|
||||
for (var i = 0; i < primaries.Length; i++)
|
||||
{
|
||||
|
|
@ -51,20 +97,7 @@ namespace Module.Inspector.Editor
|
|||
}
|
||||
}
|
||||
|
||||
private float CalculateMethodHeight(EditorMethodUtility.ResultPrimary[] primaries)
|
||||
{
|
||||
float total = EditorGUIUtility.singleLineHeight;
|
||||
|
||||
for (var i = 0; i < primaries.Length; i++)
|
||||
{
|
||||
EditorMethodUtility.ResultPrimary primary = primaries[i];
|
||||
total += primary.drawer.GetHeight(target, primary.methodInfo);
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
private bool IsValidMethodTarget()
|
||||
private bool IsValidTarget()
|
||||
{
|
||||
return target != null && targets.Length == 1;
|
||||
}
|
||||
|
|
|
|||
80
Editor/Utilities/EditorHiddenFieldUtility.cs
Normal file
80
Editor/Utilities/EditorHiddenFieldUtility.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Editor/Utilities/EditorHiddenFieldUtility.cs.meta
Normal file
3
Editor/Utilities/EditorHiddenFieldUtility.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 83e464a05c8246f2a9e67edd640b5b6c
|
||||
timeCreated: 1658598364
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using UnityEditor;
|
||||
|
||||
namespace Module.Inspector.Editor.Utilities
|
||||
{
|
||||
|
|
@ -80,6 +81,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)
|
||||
{
|
||||
const BindingFlags FLAGS = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
|
||||
|
|
|
|||
11
README.md
11
README.md
|
|
@ -157,3 +157,14 @@ List of all decorator attributes:
|
|||
* Draws a horizontal line above the field
|
||||
* `MethodHeader`
|
||||
* Draws a header/title/category text above the method (button)
|
||||
|
||||
|
||||
|
||||
## Show Hidden Fields
|
||||
|
||||
To allow fields hidden from inspector either by `NonSerialized` or `HideInInspector` add the following to the class:
|
||||
|
||||
* `EnableShowHiddenFields`
|
||||
* Enables an additional search to find any "hidden" fields and draw them below `DefaultInspector`
|
||||
* `ShowHiddenField`
|
||||
* Optional attribute for fields, if `EnableShowHiddenFields` has parameter `true`
|
||||
3
Runtime/HiddenFields.meta
Normal file
3
Runtime/HiddenFields.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 5da89eb276ee4f529122d85ca855054e
|
||||
timeCreated: 1658598477
|
||||
16
Runtime/HiddenFields/EnableShowHiddenFieldsAttribute.cs
Normal file
16
Runtime/HiddenFields/EnableShowHiddenFieldsAttribute.cs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Module.Inspector
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
|
||||
public sealed class EnableShowHiddenFieldsAttribute : PropertyAttribute
|
||||
{
|
||||
public bool UseFieldProperty { get; }
|
||||
|
||||
public EnableShowHiddenFieldsAttribute(bool useFieldProperty = false)
|
||||
{
|
||||
UseFieldProperty = useFieldProperty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c287a3fb88c74b888ac05e1c3b34238b
|
||||
timeCreated: 1658598444
|
||||
10
Runtime/HiddenFields/ShowHiddenFieldAttribute.cs
Normal file
10
Runtime/HiddenFields/ShowHiddenFieldAttribute.cs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Module.Inspector
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
|
||||
public sealed class ShowHiddenFieldAttribute : PropertyAttribute
|
||||
{
|
||||
}
|
||||
}
|
||||
3
Runtime/HiddenFields/ShowHiddenFieldAttribute.cs.meta
Normal file
3
Runtime/HiddenFields/ShowHiddenFieldAttribute.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ddf507506d4745f6904b2801b85e95bd
|
||||
timeCreated: 1658598416
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "com.module.inspector",
|
||||
"version": "1.6.0",
|
||||
"version": "1.7.0",
|
||||
"displayName": "Module.Inspector",
|
||||
"description": "Custom inspector with various useful property drawers",
|
||||
"unity": "2019.2",
|
||||
|
|
|
|||
Loading…
Reference in a new issue