0.3.0: Added Int/String to Animator parameter and tag popup

This commit is contained in:
Anders Ejlersen 2021-10-16 01:50:06 +02:00
parent 36a8310a01
commit 4ddb552bad
14 changed files with 298 additions and 1 deletions

View file

@ -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<string> names = new List<string>();
private readonly List<int> hashes = new List<int>();
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]);
}
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 14522fd274f947f8ab9942072080b7cf
timeCreated: 1634338785

View file

@ -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<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 = (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]);
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a5da29ab894b777448d7e8985e853a8c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -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]);
}
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 700a6f921a7d4438ad31c7b2222ba7d7
timeCreated: 1634339512

View file

@ -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;

View file

@ -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;
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 390dd165549248e8aaf20ee8a056d94f
timeCreated: 1634338764

View file

@ -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;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4927d6b177be0ba43bf91a28a0259d41
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

14
Runtime/Drawers/Tag.cs Normal file
View file

@ -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()
{
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a1f818608011429d93967fa5701cac48
timeCreated: 1634339466

View file

@ -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",