From 9faacb629157a70f6b2c416164e738aa516a6707 Mon Sep 17 00:00:00 2001 From: Anders Ejlersen Date: Sat, 9 Jul 2022 23:30:15 +0200 Subject: [PATCH] 1.6.0: Added FieldLabelFromType, StringToAnimationClipName and SingleSelectionFlag --- .../SingleSelectionFlagAttributeDrawer.cs | 28 +++++++ ...SingleSelectionFlagAttributeDrawer.cs.meta | 3 + ...tringToAnimationClipNameAttributeDrawer.cs | 81 +++++++++++++++++++ ...ToAnimationClipNameAttributeDrawer.cs.meta | 3 + .../Extensions/SerializedPropertyExtension.cs | 21 ++++- .../FieldLabelFromTypeAttributeDrawer.cs | 18 +++++ .../FieldLabelFromTypeAttributeDrawer.cs.meta | 3 + README.md | 15 +++- .../Drawers/SingleSelectionFlagAttribute.cs | 14 ++++ .../SingleSelectionFlagAttribute.cs.meta | 3 + .../StringToAnimationClipNameAttribute.cs | 15 ++++ ...StringToAnimationClipNameAttribute.cs.meta | 3 + .../Predrawers/FieldLabelFromTypeAttribute.cs | 9 +++ .../FieldLabelFromTypeAttribute.cs.meta | 3 + package.json | 2 +- 15 files changed, 215 insertions(+), 6 deletions(-) create mode 100644 Editor/Drawers/SingleSelectionFlagAttributeDrawer.cs create mode 100644 Editor/Drawers/SingleSelectionFlagAttributeDrawer.cs.meta create mode 100644 Editor/Drawers/StringToAnimationClipNameAttributeDrawer.cs create mode 100644 Editor/Drawers/StringToAnimationClipNameAttributeDrawer.cs.meta create mode 100644 Editor/Predrawers/FieldLabelFromTypeAttributeDrawer.cs create mode 100644 Editor/Predrawers/FieldLabelFromTypeAttributeDrawer.cs.meta create mode 100644 Runtime/Drawers/SingleSelectionFlagAttribute.cs create mode 100644 Runtime/Drawers/SingleSelectionFlagAttribute.cs.meta create mode 100644 Runtime/Drawers/StringToAnimationClipNameAttribute.cs create mode 100644 Runtime/Drawers/StringToAnimationClipNameAttribute.cs.meta create mode 100644 Runtime/Predrawers/FieldLabelFromTypeAttribute.cs create mode 100644 Runtime/Predrawers/FieldLabelFromTypeAttribute.cs.meta diff --git a/Editor/Drawers/SingleSelectionFlagAttributeDrawer.cs b/Editor/Drawers/SingleSelectionFlagAttributeDrawer.cs new file mode 100644 index 0000000..212ff19 --- /dev/null +++ b/Editor/Drawers/SingleSelectionFlagAttributeDrawer.cs @@ -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"; + } + } +} \ No newline at end of file diff --git a/Editor/Drawers/SingleSelectionFlagAttributeDrawer.cs.meta b/Editor/Drawers/SingleSelectionFlagAttributeDrawer.cs.meta new file mode 100644 index 0000000..fdad09d --- /dev/null +++ b/Editor/Drawers/SingleSelectionFlagAttributeDrawer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: fac15dbf3f08429982e975e7454cbe2a +timeCreated: 1657357189 \ No newline at end of file diff --git a/Editor/Drawers/StringToAnimationClipNameAttributeDrawer.cs b/Editor/Drawers/StringToAnimationClipNameAttributeDrawer.cs new file mode 100644 index 0000000..57a89d7 --- /dev/null +++ b/Editor/Drawers/StringToAnimationClipNameAttributeDrawer.cs @@ -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 names = new List(); + 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]); + } + } + } +} \ No newline at end of file diff --git a/Editor/Drawers/StringToAnimationClipNameAttributeDrawer.cs.meta b/Editor/Drawers/StringToAnimationClipNameAttributeDrawer.cs.meta new file mode 100644 index 0000000..3a05b92 --- /dev/null +++ b/Editor/Drawers/StringToAnimationClipNameAttributeDrawer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d2dd22fb0c4748e58b2c282194bd88e8 +timeCreated: 1656856923 \ No newline at end of file diff --git a/Editor/Extensions/SerializedPropertyExtension.cs b/Editor/Extensions/SerializedPropertyExtension.cs index 9805517..7210f56 100644 --- a/Editor/Extensions/SerializedPropertyExtension.cs +++ b/Editor/Extensions/SerializedPropertyExtension.cs @@ -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; } diff --git a/Editor/Predrawers/FieldLabelFromTypeAttributeDrawer.cs b/Editor/Predrawers/FieldLabelFromTypeAttributeDrawer.cs new file mode 100644 index 0000000..a0d661a --- /dev/null +++ b/Editor/Predrawers/FieldLabelFromTypeAttributeDrawer.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/Editor/Predrawers/FieldLabelFromTypeAttributeDrawer.cs.meta b/Editor/Predrawers/FieldLabelFromTypeAttributeDrawer.cs.meta new file mode 100644 index 0000000..d7e8195 --- /dev/null +++ b/Editor/Predrawers/FieldLabelFromTypeAttributeDrawer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 3bdd32bf85da4f66b8096b490ff8b163 +timeCreated: 1656871465 \ No newline at end of file diff --git a/README.md b/README.md index df6c0ae..7c2eb08 100644 --- a/README.md +++ b/README.md @@ -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) + ## Access List of all available access attributes: @@ -56,12 +57,16 @@ List of all available access attributes: * Show method (button) given value of another field + ## Pre-drawer List of all available pre-drawer attributes: * `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 @@ -99,8 +104,10 @@ List of all available drawer attributes: * Adds a popup with all scenes in EditorBuildSettings scenes * `SerializeReferenceTo` * 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` - * 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` * Adds popup with all animator parameter names provided by animator field specified * `StringToField` @@ -110,7 +117,7 @@ List of all available drawer attributes: * `Tag` * Adds popup with all tag values for field of type string * `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 -list of all validate attributes +List of all validate attributes: * `NotNullField` * Checks if field is null diff --git a/Runtime/Drawers/SingleSelectionFlagAttribute.cs b/Runtime/Drawers/SingleSelectionFlagAttribute.cs new file mode 100644 index 0000000..7e93cfc --- /dev/null +++ b/Runtime/Drawers/SingleSelectionFlagAttribute.cs @@ -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() + { + } + } +} \ No newline at end of file diff --git a/Runtime/Drawers/SingleSelectionFlagAttribute.cs.meta b/Runtime/Drawers/SingleSelectionFlagAttribute.cs.meta new file mode 100644 index 0000000..1f5ce84 --- /dev/null +++ b/Runtime/Drawers/SingleSelectionFlagAttribute.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 668934e9c678422a9c99bbf73530b9da +timeCreated: 1657357154 \ No newline at end of file diff --git a/Runtime/Drawers/StringToAnimationClipNameAttribute.cs b/Runtime/Drawers/StringToAnimationClipNameAttribute.cs new file mode 100644 index 0000000..3c41bef --- /dev/null +++ b/Runtime/Drawers/StringToAnimationClipNameAttribute.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/Runtime/Drawers/StringToAnimationClipNameAttribute.cs.meta b/Runtime/Drawers/StringToAnimationClipNameAttribute.cs.meta new file mode 100644 index 0000000..82c6018 --- /dev/null +++ b/Runtime/Drawers/StringToAnimationClipNameAttribute.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: db8c0a1e63b14b4fb0ed092b97e03f45 +timeCreated: 1656855775 \ No newline at end of file diff --git a/Runtime/Predrawers/FieldLabelFromTypeAttribute.cs b/Runtime/Predrawers/FieldLabelFromTypeAttribute.cs new file mode 100644 index 0000000..e5db869 --- /dev/null +++ b/Runtime/Predrawers/FieldLabelFromTypeAttribute.cs @@ -0,0 +1,9 @@ +using System; + +namespace Module.Inspector +{ + [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)] + public sealed class FieldLabelFromTypeAttribute : PredrawerModifierPropertyAttribute + { + } +} \ No newline at end of file diff --git a/Runtime/Predrawers/FieldLabelFromTypeAttribute.cs.meta b/Runtime/Predrawers/FieldLabelFromTypeAttribute.cs.meta new file mode 100644 index 0000000..c87facf --- /dev/null +++ b/Runtime/Predrawers/FieldLabelFromTypeAttribute.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 12906b50075143b5b9a0a92923ae0d83 +timeCreated: 1656871443 \ No newline at end of file diff --git a/package.json b/package.json index dd08bcd..2d2fcc9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.module.inspector", - "version": "1.5.0", + "version": "1.6.0", "displayName": "Module.Inspector", "description": "Custom inspector with various useful property drawers", "unity": "2019.2",