From 4ddb552bade4cf09f9954e31c93cc15ff695675a Mon Sep 17 00:00:00 2001 From: Ejlersen Date: Sat, 16 Oct 2021 01:50:06 +0200 Subject: [PATCH] 0.3.0: Added Int/String to Animator parameter and tag popup --- .../Drawers/DrawerIntToAnimatorParameter.cs | 84 +++++++++++++++++++ .../DrawerIntToAnimatorParameter.cs.meta | 3 + .../DrawerStringToAnimatorParameter.cs | 78 +++++++++++++++++ .../DrawerStringToAnimatorParameter.cs.meta | 11 +++ Editor/Drawers/DrawerTag.cs | 53 ++++++++++++ Editor/Drawers/DrawerTag.cs.meta | 3 + Editor/ObjectEditor.cs | 4 + Runtime/Drawers/IntToAnimatorParameter.cs | 15 ++++ .../Drawers/IntToAnimatorParameter.cs.meta | 3 + Runtime/Drawers/StringToAnimatorParameter.cs | 15 ++++ .../Drawers/StringToAnimatorParameter.cs.meta | 11 +++ Runtime/Drawers/Tag.cs | 14 ++++ Runtime/Drawers/Tag.cs.meta | 3 + package.json | 2 +- 14 files changed, 298 insertions(+), 1 deletion(-) create mode 100644 Editor/Drawers/DrawerIntToAnimatorParameter.cs create mode 100644 Editor/Drawers/DrawerIntToAnimatorParameter.cs.meta create mode 100644 Editor/Drawers/DrawerStringToAnimatorParameter.cs create mode 100644 Editor/Drawers/DrawerStringToAnimatorParameter.cs.meta create mode 100644 Editor/Drawers/DrawerTag.cs create mode 100644 Editor/Drawers/DrawerTag.cs.meta create mode 100644 Runtime/Drawers/IntToAnimatorParameter.cs create mode 100644 Runtime/Drawers/IntToAnimatorParameter.cs.meta create mode 100644 Runtime/Drawers/StringToAnimatorParameter.cs create mode 100644 Runtime/Drawers/StringToAnimatorParameter.cs.meta create mode 100644 Runtime/Drawers/Tag.cs create mode 100644 Runtime/Drawers/Tag.cs.meta diff --git a/Editor/Drawers/DrawerIntToAnimatorParameter.cs b/Editor/Drawers/DrawerIntToAnimatorParameter.cs new file mode 100644 index 0000000..d7bf30c --- /dev/null +++ b/Editor/Drawers/DrawerIntToAnimatorParameter.cs @@ -0,0 +1,84 @@ +using System.Collections.Generic; +using Module.Inspector.Editor.Utilities; +using UnityEditor; +using UnityEngine; + +namespace Module.Inspector.Editor +{ + [CustomPropertyDrawer(typeof(IntToAnimatorParameter))] + internal sealed class DrawerIntToAnimatorParameter : DrawerPropertyDrawer + { + private readonly List names = new List(); + private readonly List hashes = new List(); + private GUIContent[] contentArr; + + public override bool Draw(Rect position, DrawerPropertyAttribute attribute, SerializedProperty property, GUIContent label, EditorPropertyUtility.Result result) + { + if (property.propertyType != SerializedPropertyType.Integer) + return false; + + var att = (IntToAnimatorParameter)attribute; + SerializedProperty spSibling = property.GetSibling(att.animatorFieldName); + + if (spSibling == null) + return false; + + FetchParameters(spSibling); + + EditorGUI.BeginChangeCheck(); + EditorGUI.BeginProperty(position, label, property); + { + int index = hashes.IndexOf(property.intValue); + + if (index < 0) + index = 0; + + int newIndex = EditorGUI.Popup(position, label, index, contentArr); + property.intValue = newIndex >= 1 ? hashes[newIndex] : -1; + } + EditorGUI.EndProperty(); + bool changed = EditorGUI.EndChangeCheck(); + + if (changed) + property.serializedObject.ApplyModifiedProperties(); + + return true; + } + + public override string GetErrorMessage(SerializedProperty property) + { + if (property.propertyType != SerializedPropertyType.Integer) + return "Only supports integers"; + + var att = (IntToAnimatorParameter)attribute; + return $"Missing field: {att.animatorFieldName}"; + } + + private void FetchParameters(SerializedProperty property) + { + var animator = property.objectReferenceValue as Animator; + + names.Clear(); + names.Add("----"); + + hashes.Clear(); + hashes.Add(0); + + if (animator != null) + { + for (var i = 0; i < animator.parameterCount; i++) + { + names.Add(animator.parameters[i].name); + hashes.Add(Animator.StringToHash(animator.parameters[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/DrawerIntToAnimatorParameter.cs.meta b/Editor/Drawers/DrawerIntToAnimatorParameter.cs.meta new file mode 100644 index 0000000..8692c19 --- /dev/null +++ b/Editor/Drawers/DrawerIntToAnimatorParameter.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 14522fd274f947f8ab9942072080b7cf +timeCreated: 1634338785 \ No newline at end of file diff --git a/Editor/Drawers/DrawerStringToAnimatorParameter.cs b/Editor/Drawers/DrawerStringToAnimatorParameter.cs new file mode 100644 index 0000000..7b405ec --- /dev/null +++ b/Editor/Drawers/DrawerStringToAnimatorParameter.cs @@ -0,0 +1,78 @@ +using System.Collections.Generic; +using Module.Inspector.Editor.Utilities; +using UnityEditor; +using UnityEngine; + +namespace Module.Inspector.Editor +{ + [CustomPropertyDrawer(typeof(StringToAnimatorParameter))] + internal sealed class DrawerStringToAnimatorParameter : 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 = (StringToAnimatorParameter)attribute; + SerializedProperty spSibling = property.GetSibling(att.animatorFieldName); + + 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 = (StringToAnimatorParameter)attribute; + return $"Missing field: {att.animatorFieldName}"; + } + + private void FetchParameters(SerializedProperty property) + { + var animator = property.objectReferenceValue as Animator; + names.Clear(); + names.Add("----"); + + if (animator != null) + { + for (var i = 0; i < animator.parameterCount; i++) + { + names.Add(animator.parameters[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/DrawerStringToAnimatorParameter.cs.meta b/Editor/Drawers/DrawerStringToAnimatorParameter.cs.meta new file mode 100644 index 0000000..5f191c5 --- /dev/null +++ b/Editor/Drawers/DrawerStringToAnimatorParameter.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a5da29ab894b777448d7e8985e853a8c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/Drawers/DrawerTag.cs b/Editor/Drawers/DrawerTag.cs new file mode 100644 index 0000000..c9d374f --- /dev/null +++ b/Editor/Drawers/DrawerTag.cs @@ -0,0 +1,53 @@ +using System; +using Module.Inspector.Editor.Utilities; +using UnityEditor; +using UnityEngine; + +namespace Module.Inspector.Editor +{ + [CustomPropertyDrawer(typeof(Tag))] + internal sealed class DrawerTag : DrawerPropertyDrawer + { + private string[] names; + 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; + + FetchTags(); + + EditorGUI.BeginChangeCheck(); + EditorGUI.BeginProperty(position, label, property); + { + int index = Array.IndexOf(names, property.stringValue); + int newIndex = EditorGUI.Popup(position, label, index, contentArr); + property.stringValue = newIndex >= 0 ? names[newIndex] : names[0]; + } + EditorGUI.EndProperty(); + bool changed = EditorGUI.EndChangeCheck(); + + if (changed) + property.serializedObject.ApplyModifiedProperties(); + + return true; + } + + public override string GetErrorMessage(SerializedProperty property) + { + return "Only supports strings"; + } + + private void FetchTags() + { + names = UnityEditorInternal.InternalEditorUtility.tags; + contentArr = new GUIContent[names.Length]; + + 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/DrawerTag.cs.meta b/Editor/Drawers/DrawerTag.cs.meta new file mode 100644 index 0000000..68acc6a --- /dev/null +++ b/Editor/Drawers/DrawerTag.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 700a6f921a7d4438ad31c7b2222ba7d7 +timeCreated: 1634339512 \ No newline at end of file diff --git a/Editor/ObjectEditor.cs b/Editor/ObjectEditor.cs index 93776fc..a1f1fb3 100644 --- a/Editor/ObjectEditor.cs +++ b/Editor/ObjectEditor.cs @@ -30,6 +30,10 @@ namespace Module.Inspector.Editor { 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; diff --git a/Runtime/Drawers/IntToAnimatorParameter.cs b/Runtime/Drawers/IntToAnimatorParameter.cs new file mode 100644 index 0000000..d6a3633 --- /dev/null +++ b/Runtime/Drawers/IntToAnimatorParameter.cs @@ -0,0 +1,15 @@ +using System; + +namespace Module.Inspector +{ + [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)] + public sealed class IntToAnimatorParameter : DrawerPropertyAttribute + { + public readonly string animatorFieldName; + + public IntToAnimatorParameter(string animatorFieldName) + { + this.animatorFieldName = animatorFieldName; + } + } +} \ No newline at end of file diff --git a/Runtime/Drawers/IntToAnimatorParameter.cs.meta b/Runtime/Drawers/IntToAnimatorParameter.cs.meta new file mode 100644 index 0000000..b9629d5 --- /dev/null +++ b/Runtime/Drawers/IntToAnimatorParameter.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 390dd165549248e8aaf20ee8a056d94f +timeCreated: 1634338764 \ No newline at end of file diff --git a/Runtime/Drawers/StringToAnimatorParameter.cs b/Runtime/Drawers/StringToAnimatorParameter.cs new file mode 100644 index 0000000..5a2bab5 --- /dev/null +++ b/Runtime/Drawers/StringToAnimatorParameter.cs @@ -0,0 +1,15 @@ +using System; + +namespace Module.Inspector +{ + [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)] + public sealed class StringToAnimatorParameter : DrawerPropertyAttribute + { + public readonly string animatorFieldName; + + public StringToAnimatorParameter(string animatorFieldName) + { + this.animatorFieldName = animatorFieldName; + } + } +} \ No newline at end of file diff --git a/Runtime/Drawers/StringToAnimatorParameter.cs.meta b/Runtime/Drawers/StringToAnimatorParameter.cs.meta new file mode 100644 index 0000000..72597bd --- /dev/null +++ b/Runtime/Drawers/StringToAnimatorParameter.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4927d6b177be0ba43bf91a28a0259d41 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Drawers/Tag.cs b/Runtime/Drawers/Tag.cs new file mode 100644 index 0000000..c4311b7 --- /dev/null +++ b/Runtime/Drawers/Tag.cs @@ -0,0 +1,14 @@ +using System; +using UnityEngine.Scripting; + +namespace Module.Inspector +{ + [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)] + public sealed class Tag : DrawerPropertyAttribute + { + [Preserve] + public Tag() + { + } + } +} \ No newline at end of file diff --git a/Runtime/Drawers/Tag.cs.meta b/Runtime/Drawers/Tag.cs.meta new file mode 100644 index 0000000..59119af --- /dev/null +++ b/Runtime/Drawers/Tag.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a1f818608011429d93967fa5701cac48 +timeCreated: 1634339466 \ No newline at end of file diff --git a/package.json b/package.json index ddcf6e4..81713b3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.module.inspector", - "version": "0.2.1", + "version": "0.3.0", "displayName": "Module.Inspector", "description": "Custom inspector with various useful property drawers", "unity": "2019.2",