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,6 +428,25 @@ namespace Module.Inspector.Editor
foreach (string path in property.propertyPath.Split('.')) foreach (string path in property.propertyPath.Split('.'))
{ {
Type objType = obj.GetType(); 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); FieldInfo field = objType.GetField(path, FLAGS);
if (field == null) if (field == null)

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

View file

@ -22,6 +22,7 @@ There are six different kinds of attributes:
* Adds additional information or styling to a field/method (E.g. Header displays a title/category to fields after attribute) * Adds additional information or styling to a field/method (E.g. Header displays a title/category to fields after attribute)
## Access ## Access
List of all available access attributes: List of all available access attributes:
@ -56,12 +57,16 @@ List of all available access attributes:
* Show method (button) given value of another field * Show method (button) given value of another field
## Pre-drawer ## Pre-drawer
List of all available pre-drawer attributes: List of all available pre-drawer attributes:
* `FieldLabelFrom` * `FieldLabelFrom`
* Sets GUIContent label for SerializedProperty to value from another field * Sets GUIContent label for `SerializedProperty` to value from another field
* `FieldLabelFromType`
* Sets GUIContent label for `SerializedProperty` to value type from another field
## Drawer ## Drawer
@ -99,8 +104,10 @@ List of all available drawer attributes:
* Adds a popup with all scenes in EditorBuildSettings scenes * Adds a popup with all scenes in EditorBuildSettings scenes
* `SerializeReferenceTo` * `SerializeReferenceTo`
* Adds a popup for `SerializeReference` fields with types inheriting from assigned type or field type * Adds a popup for `SerializeReference` fields with types inheriting from assigned type or field type
* `SingleSelectionFlag`
* Adds popup, where only a single value from the flag can be selected
* `Slider` * `Slider`
* Adds a min & max slider and clamps value (Requirement: MinValue and MaxValue) * Adds a min & max slider and clamps value (Requirement: `MinValue` and `MaxValue`)
* `StringToAnimatorParameter` * `StringToAnimatorParameter`
* Adds popup with all animator parameter names provided by animator field specified * Adds popup with all animator parameter names provided by animator field specified
* `StringToField` * `StringToField`
@ -110,7 +117,7 @@ List of all available drawer attributes:
* `Tag` * `Tag`
* Adds popup with all tag values for field of type string * Adds popup with all tag values for field of type string
* `UrlGoTo` * `UrlGoTo`
* Adds a button to the field that calls Application.OpenUrl with string value * Adds a button to the field that calls `Application.OpenUrl` with string value
@ -135,7 +142,7 @@ List of all value attributes:
## Validate ## Validate
list of all validate attributes List of all validate attributes:
* `NotNullField` * `NotNullField`
* Checks if field is null * Checks if field is null

View file

@ -0,0 +1,14 @@
using System;
using UnityEngine.Scripting;
namespace Module.Inspector
{
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public sealed class SingleSelectionFlagAttribute : DrawerPropertyAttribute
{
[Preserve]
public SingleSelectionFlagAttribute()
{
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 668934e9c678422a9c99bbf73530b9da
timeCreated: 1657357154

View file

@ -0,0 +1,15 @@
using System;
namespace Module.Inspector
{
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public sealed class StringToAnimationClipAttribute : DrawerPropertyAttribute
{
public readonly string animationFieldName;
public StringToAnimationClipAttribute(string animationFieldName)
{
this.animationFieldName = animationFieldName;
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: db8c0a1e63b14b4fb0ed092b97e03f45
timeCreated: 1656855775

View file

@ -0,0 +1,9 @@
using System;
namespace Module.Inspector
{
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public sealed class FieldLabelFromTypeAttribute : PredrawerModifierPropertyAttribute
{
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 12906b50075143b5b9a0a92923ae0d83
timeCreated: 1656871443

View file

@ -1,6 +1,6 @@
{ {
"name": "com.module.inspector", "name": "com.module.inspector",
"version": "1.5.0", "version": "1.6.0",
"displayName": "Module.Inspector", "displayName": "Module.Inspector",
"description": "Custom inspector with various useful property drawers", "description": "Custom inspector with various useful property drawers",
"unity": "2019.2", "unity": "2019.2",