Advertisement
zORg_alex

Serialized AnimatorControllerParameter

Nov 27th, 2023 (edited)
144
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.97 KB | Source Code | 0 0
  1. using System;
  2. #if UNITY_EDITOR
  3. using UnityEditor;
  4. #endif
  5. using UnityEngine;
  6.  
  7. namespace zORgs.Utilities
  8. {
  9.     /// <summary>
  10.     /// A serialized version of <see cref="UnityEngine.AnimatorControllerParameter"/> with some nifty methods for ease of use.
  11.     /// It draws a dropdown with all properties of Animator.Controller on that object.
  12.     /// This is a way to hide animation code clutter to be hidden away from your important code base.
  13.     /// </summary>
  14.     [Serializable]
  15.     public class AnimatorControllerParameter
  16.     {
  17.         [SerializeField]
  18.         private int _hash;
  19.         [SerializeField]
  20.         private string _name;
  21.         [SerializeField]
  22.         private AnimatorControllerParameterType _type;
  23.  
  24.         public AnimatorControllerParameter(UnityEngine.AnimatorControllerParameter unitysParameter)
  25.         {
  26.             _name = unitysParameter.name;
  27.             _hash = unitysParameter.nameHash;
  28.             _type = unitysParameter.type;
  29.         }
  30.  
  31.         public enum AnimationValueType { Float, Integer, Bool, Trigger }
  32.  
  33.         public string name => _name;
  34.         public int nameHash => _hash;
  35.         public AnimatorControllerParameterType type => _type;
  36.  
  37.         public static implicit operator AnimatorControllerParameter(UnityEngine.AnimatorControllerParameter unitysParameter) => new AnimatorControllerParameter(unitysParameter);
  38.     }
  39.  
  40.     public static class AnimatorExtension
  41.     {
  42.         public static void SetParameter(this Animator anim, zORgs.Utilities.AnimatorControllerParameter parameter, float val)
  43.         {
  44.             ThrowIfWrongType(parameter, AnimatorControllerParameterType.Float);
  45.             anim.SetFloat(parameter.nameHash, val);
  46.         }
  47.         public static void SetParameter(this Animator anim, zORgs.Utilities.AnimatorControllerParameter parameter, bool val)
  48.         {
  49.             ThrowIfWrongType(parameter, AnimatorControllerParameterType.Bool);
  50.             anim.SetBool(parameter.nameHash, val);
  51.         }
  52.         public static void SetParameter(this Animator anim, zORgs.Utilities.AnimatorControllerParameter parameter, int val)
  53.         {
  54.             ThrowIfWrongType(parameter, AnimatorControllerParameterType.Int);
  55.             anim.SetInteger(parameter.nameHash, val);
  56.         }
  57.         public static void SetTrigger(this Animator anim, zORgs.Utilities.AnimatorControllerParameter parameter)
  58.         {
  59.             ThrowIfWrongType(parameter, AnimatorControllerParameterType.Trigger);
  60.             anim.SetTrigger(parameter.nameHash);
  61.         }
  62.  
  63.         private static void ThrowIfWrongType(zORgs.Utilities.AnimatorControllerParameter parameter, AnimatorControllerParameterType expected)
  64.         {
  65.             if (parameter.type != expected)
  66.                 throw new ArgumentException($"Parameter type is {parameter.type}, not {expected}");
  67.         }
  68.     }
  69. }
  70.  
  71. #if UNITY_EDITOR
  72. namespace zORgs.Editor.Utilities
  73. {
  74.     [CustomPropertyDrawer(typeof(zORgs.Utilities.AnimatorControllerParameter))]
  75.     public class AnimatorControllerParameterDrawer : PropertyDrawer
  76.     {
  77.         private Animator _animator;
  78.         private UnityEngine.AnimatorControllerParameter[] _parameters;
  79.         private string[] _popupOptions;
  80.         bool _initialized;
  81.         private int _selectedIndex;
  82.  
  83.         public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  84.         {
  85.             if (!_initialized) Initialize(property);
  86.  
  87.             var labelRect = new Rect(position.position, new Vector2(Mathf.Max(position.size.x * 0.45f - 30f, 120f), position.size.y));
  88.             var boxedValue = (zORgs.Utilities.AnimatorControllerParameter)property.boxedValue;
  89.             var typeContent = new GUIContent(boxedValue?.type.ToString() ?? "{null}");
  90.             var typeSize = EditorStyles.label.CalcSize(typeContent) + Vector2.right * 20;
  91.             var typeRect = new Rect(position.position + Vector2.right * (position.width - typeSize.x), new Vector2(typeSize.x, EditorGUIUtility.singleLineHeight));
  92.             var popupRect = new Rect(position.position + Vector2.right * labelRect.width, position.size - Vector2.right * (labelRect.width));
  93.  
  94.             EditorGUI.BeginProperty(position, label, property);
  95.  
  96.             GUI.Label(labelRect, property.name);
  97.  
  98.             if (_popupOptions.Length > 0)
  99.                 _selectedIndex = EditorGUI.Popup(popupRect, _selectedIndex, _popupOptions);
  100.             else
  101.                 GUI.Label(popupRect, boxedValue == null ? "{null}" : boxedValue.name + " " + boxedValue.nameHash);
  102.             if (_selectedIndex != -1)
  103.                 property.boxedValue = (zORgs.Utilities.AnimatorControllerParameter)_parameters[_selectedIndex];
  104.  
  105.             GUI.Label(typeRect, typeContent);
  106.  
  107.             EditorGUI.EndProperty();
  108.         }
  109.         public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  110.         {
  111.             return EditorGUIUtility.singleLineHeight;
  112.         }
  113.  
  114.         private void Initialize(SerializedProperty property)
  115.         {
  116.             _animator = ((MonoBehaviour)property.serializedObject.targetObject).GetComponent<Animator>();
  117.             _parameters = new UnityEngine.AnimatorControllerParameter[_animator.parameterCount];
  118.             _popupOptions = new string[_animator.parameterCount];
  119.             for (int i = 0; i < _animator.parameterCount; i++)
  120.             {
  121.                 _parameters[i] = _animator.GetParameter(i);
  122.                 _popupOptions[i] = _parameters[i].name;
  123.             }
  124.             _selectedIndex = System.Array.IndexOf(_popupOptions, ((zORgs.Utilities.AnimatorControllerParameter)property.boxedValue).name);
  125.             _initialized = true;
  126.         }
  127.     }
  128. }
  129. #endif
Advertisement
Comments
  • zORg_alex
    214 days
    # text 1.94 KB | 0 0
    1. Use case:
    2. using UnityEngine;
    3. using Sirenix.OdinInspector;
    4. using zORgs;
    5. using zORgs.Utilities;
    6. using Parameter = zORgs.Utilities.AnimatorControllerParameter;
    7.  
    8. [RequireComponent(typeof(Animator))]
    9. public class BasicLocomotionInterface_v2 : MonoBehaviour
    10. {
    11. [HorizontalGroup("Parameters")]
    12. [BoxGroup("Parameters/Move")]
    13. public Parameter MoveZ;
    14. [BoxGroup("Parameters/Move")]
    15. public Parameter IsMoving;
    16. [BoxGroup("Parameters/State")]
    17. public Parameter Grounded;
    18. [BoxGroup("Parameters/State")]
    19. public Parameter Jump;
    20. [BoxGroup("Parameters/State")]
    21. public Parameter FreeFall;
    22.  
    23.  
    24. [BoxGroup("Aim")]
    25. public int _aimingLayerIndex;
    26. [BoxGroup("Aim")]
    27. public Parameter Empty;
    28. [BoxGroup("Aim")]
    29. public Parameter Aim;
    30. [BoxGroup("Aim")]
    31. public Parameter Rifle;
    32.  
    33. private Animator _animator;
    34. private void Awake()
    35. {
    36. _animator = GetComponent<Animator>();
    37. }
    38.  
    39. public void SetForwardSpeed(float value)
    40. {
    41. _animator.SetParameter(MoveZ, value);
    42. _animator.SetParameter(IsMoving, value != 0f);
    43. }
    44.  
    45. public void SetGrounded(bool value) => _animator.SetParameter(Grounded, value);
    46. public void SetJump(bool value) => _animator.SetParameter(Jump, value);
    47. public void SetFreeFall(bool value) => _animator.SetParameter(FreeFall, value);
    48.  
    49.  
    50. [Button]
    51. public void StartAiming()
    52. {
    53. this.Tween(v => _animator.SetLayerWeight(_aimingLayerIndex, v), 0, 1, .1f);
    54. _animator.SetParameter(Aim, true);
    55. }
    56. [Button]
    57. public void StopAiming()
    58. {
    59. this.Tween(v => _animator.SetLayerWeight(_aimingLayerIndex, v), 0, 1, .1f);
    60. _animator.SetParameter(Aim, false);
    61. }
    62. [Button]
    63. public void RifleEquipped()
    64. {
    65. this.Tween(v => _animator.SetLayerWeight(_aimingLayerIndex, v), 0, 1, .1f);
    66. _animator.SetParameter(Rifle, true);
    67. }
    68. [Button]
    69. public void UnEquip()
    70. {
    71. this.Tween(v => _animator.SetLayerWeight(_aimingLayerIndex, v), 1, 0, .25f);
    72. _animator.SetParameter(Rifle, false);
    73. _animator.SetParameter(Rifle, false);
    74. }
    75. }
    76.  
Add Comment
Please, Sign In to add comment
Advertisement