1.6.0: Added FieldLabelFromType, StringToAnimationClipName and SingleSelectionFlag

This commit is contained in:
Anders Ejlersen 2022-07-09 23:30:15 +02:00
parent 7d99d62e11
commit 9faacb6291
15 changed files with 215 additions and 6 deletions

View file

@ -0,0 +1,28 @@
using Module.Inspector.Editor.Utilities;
using UnityEditor;
using UnityEngine;
namespace Module.Inspector.Editor
{
[CustomPropertyDrawer(typeof(SingleSelectionFlagAttribute))]
internal sealed class SingleSelectionFlagAttributeDrawer : DrawerPropertyDrawer
{
public override bool Draw(Rect position, DrawerPropertyAttribute attribute, SerializedProperty property, GUIContent label, EditorPropertyUtility.Result result)
{
if (property.propertyType != SerializedPropertyType.Enum)
return false;
EditorGUI.BeginProperty(position, label, property);
{
property.enumValueIndex = EditorGUI.Popup(position, label.text, property.enumValueIndex, property.enumDisplayNames);
}
EditorGUI.EndProperty();
return true;
}
public override string GetErrorMessage(SerializedProperty property)
{
return "Only supports enum";
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: fac15dbf3f08429982e975e7454cbe2a
timeCreated: 1657357189

View file

@ -0,0 +1,81 @@
using System.Collections.Generic;
using Module.Inspector.Editor.Utilities;
using UnityEditor;
using UnityEngine;
namespace Module.Inspector.Editor
{
[CustomPropertyDrawer(typeof(StringToAnimationClipAttribute))]
internal sealed class StringToAnimationClipAttributeDrawer : DrawerPropertyDrawer
{
private readonly List<string> names = new List<string>();
private GUIContent[] contentArr;
public override bool Draw(Rect position, DrawerPropertyAttribute attribute, SerializedProperty property, GUIContent label, EditorPropertyUtility.Result result)
{
if (property.propertyType != SerializedPropertyType.String)
return false;
var att = (StringToAnimationClipAttribute)attribute;
SerializedProperty spSibling = property.GetRelativeProperty(att.animationFieldName);
if (spSibling == null)
return false;
FetchParameters(spSibling);
EditorGUI.BeginChangeCheck();
EditorGUI.BeginProperty(position, label, property);
{
int index = names.IndexOf(property.stringValue);
if (index < 0)
index = 0;
int newIndex = EditorGUI.Popup(position, label, index, contentArr);
property.stringValue = newIndex >= 1 ? names[newIndex] : string.Empty;
}
EditorGUI.EndProperty();
bool changed = EditorGUI.EndChangeCheck();
if (changed)
property.serializedObject.ApplyModifiedProperties();
return true;
}
public override string GetErrorMessage(SerializedProperty property)
{
if (property.propertyType != SerializedPropertyType.String)
return "Only supports strings";
var att = (StringToAnimationClipAttribute)attribute;
return $"Missing field: {att.animationFieldName}";
}
private void FetchParameters(SerializedProperty property)
{
var animation = property.objectReferenceValue as Animation;
names.Clear();
names.Add("----");
if (animation != null)
{
AnimationClip[] clips = AnimationUtility.GetAnimationClips(animation.gameObject);
for (var i = 0; i < clips.Length; i++)
{
if (clips[i] != null)
names.Add(clips[i].name);
}
}
contentArr = new GUIContent[names.Count];
for (var i = 0; i < contentArr.Length; i++)
{
contentArr[i] = new GUIContent(names[i]);
}
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d2dd22fb0c4748e58b2c282194bd88e8
timeCreated: 1656856923

View file

@ -428,11 +428,30 @@ namespace Module.Inspector.Editor
foreach (string path in property.propertyPath.Split('.'))
{
Type objType = obj.GetType();
if (objType.IsArray)
{
if (path.Equals("Array"))
continue;
objType = objType.GetElementType();
if (path.StartsWith("data["))
{
int index = int.Parse(path.Substring(5, path.Length - 6));
var arr = (object[])obj;
obj = arr[index];
}
}
if (objType == null)
continue;
FieldInfo field = objType.GetField(path, FLAGS);
if (field == null)
continue;
obj = field.GetValue(obj);
type = field.FieldType;
}

View file

@ -0,0 +1,18 @@
using System;
using UnityEditor;
using UnityEngine;
namespace Module.Inspector.Editor
{
[CustomPropertyDrawer(typeof(FieldLabelFromTypeAttribute))]
internal sealed class FieldLabelFromTypeAttributeDrawer : PredrawerModifierPropertyDrawer
{
public override void Modify(PredrawerModifierPropertyAttribute attribute, SerializedProperty property, GUIContent label)
{
Type type = property.GetValueType();
if (type != null)
label.text = type.Name;
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3bdd32bf85da4f66b8096b490ff8b163
timeCreated: 1656871465