1.8.0: Added AssetsOnly, SceneObjectsOnly, AssignAssetIfNull, AssignComponentIfNull
- Attributes: Added AssetsOnly, SceneObjectsOnly, AssignAssetIfNull, AssignComponentIfNull - Attributes: Replaced AssignIfNull with AssignComponentIfNull
This commit is contained in:
parent
070b82767f
commit
eb19150d98
23 changed files with 302 additions and 73 deletions
|
|
@ -1,5 +1,4 @@
|
|||
using System.Reflection;
|
||||
using Module.Inspector.Editor.Utilities;
|
||||
using Module.Inspector.Editor.Utilities;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
|
|
@ -21,25 +20,20 @@ namespace Module.Inspector.Editor
|
|||
if (!IsValidTarget())
|
||||
return;
|
||||
|
||||
FieldInfo[] fields = EditorHiddenFieldUtility.Query(target);
|
||||
EditorHiddenFieldUtility.EditableFieldInfo[] fields = EditorHiddenFieldUtility.Query(target);
|
||||
|
||||
if (fields.Length == 0)
|
||||
return;
|
||||
|
||||
float totalHeight = EditorHiddenFieldUtility.CalculateHeight(fields) + 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
|
||||
}
|
||||
Rect 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;
|
||||
|
|
|
|||
|
|
@ -9,20 +9,20 @@ namespace Module.Inspector.Editor.Utilities
|
|||
{
|
||||
internal static class EditorHiddenFieldUtility
|
||||
{
|
||||
private static Dictionary<Type, FieldInfo[]> CACHED_TYPE_TO_PRIMARIES;
|
||||
private static Dictionary<Type, EditableFieldInfo[]> CACHED_TYPE_TO_PRIMARIES;
|
||||
|
||||
public static FieldInfo[] Query(Object target)
|
||||
public static EditableFieldInfo[] Query(Object target)
|
||||
{
|
||||
Type type = target.GetType();
|
||||
var att = type.GetCustomAttribute<EnableShowHiddenFieldsAttribute>();
|
||||
|
||||
if (att == null)
|
||||
return new FieldInfo[0];
|
||||
return new EditableFieldInfo[0];
|
||||
|
||||
if (CACHED_TYPE_TO_PRIMARIES == null)
|
||||
CACHED_TYPE_TO_PRIMARIES = new Dictionary<Type, FieldInfo[]>();
|
||||
CACHED_TYPE_TO_PRIMARIES = new Dictionary<Type, EditableFieldInfo[]>();
|
||||
|
||||
if (CACHED_TYPE_TO_PRIMARIES.TryGetValue(type, out FieldInfo[] fields))
|
||||
if (CACHED_TYPE_TO_PRIMARIES.TryGetValue(type, out EditableFieldInfo[] fields))
|
||||
return fields;
|
||||
|
||||
fields = InternalFetch(type, att.UseFieldProperty);
|
||||
|
|
@ -30,39 +30,66 @@ namespace Module.Inspector.Editor.Utilities
|
|||
return fields;
|
||||
}
|
||||
|
||||
public static float CalculateHeight(FieldInfo[] fields)
|
||||
public static float CalculateHeight(EditableFieldInfo[] fields)
|
||||
{
|
||||
return EditorGUIUtility.singleLineHeight * fields.Length;
|
||||
}
|
||||
|
||||
public static void Draw(Rect rect, FieldInfo field, Object target)
|
||||
public static void Draw(Rect rect, EditableFieldInfo field, Object target)
|
||||
{
|
||||
bool prevEnabled = GUI.enabled;
|
||||
GUI.enabled = false;
|
||||
GUI.enabled = field.editable;
|
||||
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);
|
||||
if (field.IsType<float>())
|
||||
{
|
||||
float temp = EditorGUI.FloatField(rect, nicifiedName, field.GetValue<float>(target));
|
||||
|
||||
if (field.editable)
|
||||
field.SetValue(target, temp);
|
||||
}
|
||||
else if (field.IsType<int>())
|
||||
{
|
||||
int temp = EditorGUI.IntField(rect, nicifiedName, field.GetValue<int>(target));
|
||||
|
||||
if (field.editable)
|
||||
field.SetValue(target, temp);
|
||||
}
|
||||
else if (field.IsType<double>())
|
||||
{
|
||||
double temp = EditorGUI.DoubleField(rect, nicifiedName, field.GetValue<double>(target));
|
||||
|
||||
if (field.editable)
|
||||
field.SetValue(target, temp);
|
||||
}
|
||||
else if (field.IsType<string>())
|
||||
{
|
||||
string temp = EditorGUI.TextField(rect, nicifiedName, field.GetValue<string>(target));
|
||||
|
||||
if (field.editable)
|
||||
field.SetValue(target, temp);
|
||||
}
|
||||
else if (field.IsAssignableFrom<Object>())
|
||||
{
|
||||
Object temp = EditorGUI.ObjectField(rect, nicifiedName, field.GetValue<Object>(target), typeof(Object), true);
|
||||
|
||||
if (field.editable)
|
||||
field.SetValue(target, temp);
|
||||
}
|
||||
else
|
||||
{
|
||||
EditorGUI.LabelField(rect, ObjectNames.NicifyVariableName(field.Name), "Unsupported type");
|
||||
}
|
||||
|
||||
GUI.enabled = prevEnabled;
|
||||
}
|
||||
|
||||
private static FieldInfo[] InternalFetch(Type type, bool useFieldProperty)
|
||||
private static EditableFieldInfo[] 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);
|
||||
var list = new List<EditableFieldInfo>(fields.Length);
|
||||
|
||||
for (var i = 0; i < fields.Length; i++)
|
||||
{
|
||||
|
|
@ -70,11 +97,48 @@ namespace Module.Inspector.Editor.Utilities
|
|||
|
||||
if (fi.GetCustomAttribute<NonSerializedAttribute>() == null && fi.GetCustomAttribute<HideInInspector>() == null)
|
||||
continue;
|
||||
if (!useFieldProperty || fi.GetCustomAttribute<ShowHiddenFieldAttribute>() != null)
|
||||
list.Add(fi);
|
||||
|
||||
var att = fi.GetCustomAttribute<ShowHiddenFieldAttribute>();
|
||||
|
||||
if (!useFieldProperty || att != null)
|
||||
list.Add(new EditableFieldInfo(fi, att != null && att.editable));
|
||||
}
|
||||
|
||||
return list.ToArray();
|
||||
}
|
||||
|
||||
public sealed class EditableFieldInfo
|
||||
{
|
||||
public readonly FieldInfo fieldInfo;
|
||||
public readonly bool editable;
|
||||
|
||||
public string Name => fieldInfo.Name;
|
||||
|
||||
public EditableFieldInfo(FieldInfo fieldInfo, bool editable)
|
||||
{
|
||||
this.fieldInfo = fieldInfo;
|
||||
this.editable = editable;
|
||||
}
|
||||
|
||||
public T GetValue<T>(object target)
|
||||
{
|
||||
return (T)fieldInfo.GetValue(target);
|
||||
}
|
||||
|
||||
public void SetValue<T>(object target, T value)
|
||||
{
|
||||
fieldInfo.SetValue(target, value);
|
||||
}
|
||||
|
||||
public bool IsType<T>()
|
||||
{
|
||||
return fieldInfo.FieldType == typeof(T);
|
||||
}
|
||||
|
||||
public bool IsAssignableFrom<T>()
|
||||
{
|
||||
return typeof(T).IsAssignableFrom(fieldInfo.FieldType);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
25
Editor/ValueModifiers/AssetsOnlyAttributeDrawer.cs
Normal file
25
Editor/ValueModifiers/AssetsOnlyAttributeDrawer.cs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Module.Inspector.Editor
|
||||
{
|
||||
[CustomPropertyDrawer(typeof(AssetsOnlyAttribute))]
|
||||
internal sealed class AssetsOnlyAttributeDrawer : ValueModifierPropertyDrawer
|
||||
{
|
||||
public override void Modify(ValueModifierPropertyAttribute attribute, SerializedProperty property)
|
||||
{
|
||||
if (property.propertyType != SerializedPropertyType.ObjectReference)
|
||||
return;
|
||||
|
||||
Object obj = property.objectReferenceValue;
|
||||
|
||||
if (obj != null && !IsValid(obj))
|
||||
property.objectReferenceValue = null;
|
||||
}
|
||||
|
||||
private static bool IsValid(Object obj)
|
||||
{
|
||||
return AssetDatabase.Contains(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Editor/ValueModifiers/AssetsOnlyAttributeDrawer.cs.meta
Normal file
3
Editor/ValueModifiers/AssetsOnlyAttributeDrawer.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 84b797e92aa740e4866d71c9afaa6ff2
|
||||
timeCreated: 1660250173
|
||||
43
Editor/ValueModifiers/AssignAssetIfNullAttributeDrawer.cs
Normal file
43
Editor/ValueModifiers/AssignAssetIfNullAttributeDrawer.cs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
using System;
|
||||
using UnityEditor;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace Module.Inspector.Editor
|
||||
{
|
||||
[CustomPropertyDrawer(typeof(AssignAssetIfNullAttribute))]
|
||||
internal sealed class AssignAssetIfNullAttributeDrawer : ValueModifierPropertyDrawer
|
||||
{
|
||||
public override void Modify(ValueModifierPropertyAttribute attribute, SerializedProperty property)
|
||||
{
|
||||
if (property.propertyType != SerializedPropertyType.ObjectReference)
|
||||
return;
|
||||
if (property.objectReferenceValue != null)
|
||||
return;
|
||||
|
||||
var att = (AssignAssetIfNullAttribute)attribute;
|
||||
Type type = att.useType ? att.type : property.GetValueType();
|
||||
property.objectReferenceValue = AssignAsset(type, att.filter, att.searchFolders);
|
||||
}
|
||||
|
||||
private static Object AssignAsset(Type type, string filter, string[] folders)
|
||||
{
|
||||
string[] guids;
|
||||
|
||||
if (folders != null && folders.Length != 0)
|
||||
guids = AssetDatabase.FindAssets($"{type.Name} {filter}", folders);
|
||||
else
|
||||
guids = AssetDatabase.FindAssets($"{type.Name} {filter}");
|
||||
|
||||
for (var i = 0; i < guids.Length; i++)
|
||||
{
|
||||
string path = AssetDatabase.GUIDToAssetPath(guids[i]);
|
||||
Object obj = AssetDatabase.LoadAssetAtPath(path, type);
|
||||
|
||||
if (obj != null)
|
||||
return obj;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 68325a7d22ed40e2aae8e00fad5f05dc
|
||||
timeCreated: 1661599398
|
||||
|
|
@ -5,8 +5,8 @@ using Object = UnityEngine.Object;
|
|||
|
||||
namespace Module.Inspector.Editor
|
||||
{
|
||||
[CustomPropertyDrawer(typeof(AssignIfNullAttribute))]
|
||||
internal sealed class AssignIfNullAttributeDrawer : ValueModifierPropertyDrawer
|
||||
[CustomPropertyDrawer(typeof(AssignComponentIfNullAttribute))]
|
||||
internal sealed class AssignComponentIfNullAttributeDrawer : ValueModifierPropertyDrawer
|
||||
{
|
||||
public override void Modify(ValueModifierPropertyAttribute attribute, SerializedProperty property)
|
||||
{
|
||||
|
|
@ -15,7 +15,7 @@ namespace Module.Inspector.Editor
|
|||
if (property.objectReferenceValue != null)
|
||||
return;
|
||||
|
||||
var att = (AssignIfNullAttribute)attribute;
|
||||
var att = (AssignComponentIfNullAttribute)attribute;
|
||||
Type type = att.useType ? att.type : property.GetValueType();
|
||||
|
||||
if (!typeof(Component).IsAssignableFrom(type))
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 38c05c2014ac40c0915f6e36df16c7e4
|
||||
timeCreated: 1661600081
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e029b0d7c1384ab2b961e74a2bf23fe0
|
||||
timeCreated: 1638695373
|
||||
25
Editor/ValueModifiers/SceneObjectsOnlyAttributeDrawer.cs
Normal file
25
Editor/ValueModifiers/SceneObjectsOnlyAttributeDrawer.cs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Module.Inspector.Editor
|
||||
{
|
||||
[CustomPropertyDrawer(typeof(SceneObjectsOnlyAttribute))]
|
||||
internal sealed class SceneObjectsOnlyAttributeDrawer : ValueModifierPropertyDrawer
|
||||
{
|
||||
public override void Modify(ValueModifierPropertyAttribute attribute, SerializedProperty property)
|
||||
{
|
||||
if (property.propertyType != SerializedPropertyType.ObjectReference)
|
||||
return;
|
||||
|
||||
Object obj = property.objectReferenceValue;
|
||||
|
||||
if (obj != null && !IsValid(obj))
|
||||
property.objectReferenceValue = null;
|
||||
}
|
||||
|
||||
private static bool IsValid(Object obj)
|
||||
{
|
||||
return !AssetDatabase.Contains(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 48dbd634fecb45699637d9dfff17f8e9
|
||||
timeCreated: 1660250619
|
||||
Loading…
Add table
Add a link
Reference in a new issue