Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
- using UnityEngine;
- namespace zORgs.Utilities
- {
- /// <summary>
- /// A serialized version of <see cref="UnityEngine.AnimatorControllerParameter"/> with some nifty methods for ease of use.
- /// It draws a dropdown with all properties of Animator.Controller on that object.
- /// This is a way to hide animation code clutter to be hidden away from your important code base.
- /// </summary>
- [Serializable]
- public class AnimatorControllerParameter
- {
- [SerializeField]
- private int _hash;
- [SerializeField]
- private string _name;
- [SerializeField]
- private AnimatorControllerParameterType _type;
- public AnimatorControllerParameter(UnityEngine.AnimatorControllerParameter unitysParameter)
- {
- _name = unitysParameter.name;
- _hash = unitysParameter.nameHash;
- _type = unitysParameter.type;
- }
- public enum AnimationValueType { Float, Integer, Bool, Trigger }
- public string name => _name;
- public int nameHash => _hash;
- public AnimatorControllerParameterType type => _type;
- public static implicit operator AnimatorControllerParameter(UnityEngine.AnimatorControllerParameter unitysParameter) => new AnimatorControllerParameter(unitysParameter);
- }
- public static class AnimatorExtension
- {
- public static void SetParameter(this Animator anim, zORgs.Utilities.AnimatorControllerParameter parameter, float val)
- {
- ThrowIfWrongType(parameter, AnimatorControllerParameterType.Float);
- anim.SetFloat(parameter.nameHash, val);
- }
- public static void SetParameter(this Animator anim, zORgs.Utilities.AnimatorControllerParameter parameter, bool val)
- {
- ThrowIfWrongType(parameter, AnimatorControllerParameterType.Bool);
- anim.SetBool(parameter.nameHash, val);
- }
- public static void SetParameter(this Animator anim, zORgs.Utilities.AnimatorControllerParameter parameter, int val)
- {
- ThrowIfWrongType(parameter, AnimatorControllerParameterType.Int);
- anim.SetInteger(parameter.nameHash, val);
- }
- public static void SetTrigger(this Animator anim, zORgs.Utilities.AnimatorControllerParameter parameter)
- {
- ThrowIfWrongType(parameter, AnimatorControllerParameterType.Trigger);
- anim.SetTrigger(parameter.nameHash);
- }
- private static void ThrowIfWrongType(zORgs.Utilities.AnimatorControllerParameter parameter, AnimatorControllerParameterType expected)
- {
- if (parameter.type != expected)
- throw new ArgumentException($"Parameter type is {parameter.type}, not {expected}");
- }
- }
- }
- #if UNITY_EDITOR
- namespace zORgs.Editor.Utilities
- {
- [CustomPropertyDrawer(typeof(zORgs.Utilities.AnimatorControllerParameter))]
- public class AnimatorControllerParameterDrawer : PropertyDrawer
- {
- private Animator _animator;
- private UnityEngine.AnimatorControllerParameter[] _parameters;
- private string[] _popupOptions;
- bool _initialized;
- private int _selectedIndex;
- public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
- {
- if (!_initialized) Initialize(property);
- var labelRect = new Rect(position.position, new Vector2(Mathf.Max(position.size.x * 0.45f - 30f, 120f), position.size.y));
- var boxedValue = (zORgs.Utilities.AnimatorControllerParameter)property.boxedValue;
- var typeContent = new GUIContent(boxedValue?.type.ToString() ?? "{null}");
- var typeSize = EditorStyles.label.CalcSize(typeContent) + Vector2.right * 20;
- var typeRect = new Rect(position.position + Vector2.right * (position.width - typeSize.x), new Vector2(typeSize.x, EditorGUIUtility.singleLineHeight));
- var popupRect = new Rect(position.position + Vector2.right * labelRect.width, position.size - Vector2.right * (labelRect.width));
- EditorGUI.BeginProperty(position, label, property);
- GUI.Label(labelRect, property.name);
- if (_popupOptions.Length > 0)
- _selectedIndex = EditorGUI.Popup(popupRect, _selectedIndex, _popupOptions);
- else
- GUI.Label(popupRect, boxedValue == null ? "{null}" : boxedValue.name + " " + boxedValue.nameHash);
- if (_selectedIndex != -1)
- property.boxedValue = (zORgs.Utilities.AnimatorControllerParameter)_parameters[_selectedIndex];
- GUI.Label(typeRect, typeContent);
- EditorGUI.EndProperty();
- }
- public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
- {
- return EditorGUIUtility.singleLineHeight;
- }
- private void Initialize(SerializedProperty property)
- {
- _animator = ((MonoBehaviour)property.serializedObject.targetObject).GetComponent<Animator>();
- _parameters = new UnityEngine.AnimatorControllerParameter[_animator.parameterCount];
- _popupOptions = new string[_animator.parameterCount];
- for (int i = 0; i < _animator.parameterCount; i++)
- {
- _parameters[i] = _animator.GetParameter(i);
- _popupOptions[i] = _parameters[i].name;
- }
- _selectedIndex = System.Array.IndexOf(_popupOptions, ((zORgs.Utilities.AnimatorControllerParameter)property.boxedValue).name);
- _initialized = true;
- }
- }
- }
- #endif
Advertisement
Comments
-
- Use case:
- using UnityEngine;
- using Sirenix.OdinInspector;
- using zORgs;
- using zORgs.Utilities;
- using Parameter = zORgs.Utilities.AnimatorControllerParameter;
- [RequireComponent(typeof(Animator))]
- public class BasicLocomotionInterface_v2 : MonoBehaviour
- {
- [HorizontalGroup("Parameters")]
- [BoxGroup("Parameters/Move")]
- public Parameter MoveZ;
- [BoxGroup("Parameters/Move")]
- public Parameter IsMoving;
- [BoxGroup("Parameters/State")]
- public Parameter Grounded;
- [BoxGroup("Parameters/State")]
- public Parameter Jump;
- [BoxGroup("Parameters/State")]
- public Parameter FreeFall;
- [BoxGroup("Aim")]
- public int _aimingLayerIndex;
- [BoxGroup("Aim")]
- public Parameter Empty;
- [BoxGroup("Aim")]
- public Parameter Aim;
- [BoxGroup("Aim")]
- public Parameter Rifle;
- private Animator _animator;
- private void Awake()
- {
- _animator = GetComponent<Animator>();
- }
- public void SetForwardSpeed(float value)
- {
- _animator.SetParameter(MoveZ, value);
- _animator.SetParameter(IsMoving, value != 0f);
- }
- public void SetGrounded(bool value) => _animator.SetParameter(Grounded, value);
- public void SetJump(bool value) => _animator.SetParameter(Jump, value);
- public void SetFreeFall(bool value) => _animator.SetParameter(FreeFall, value);
- [Button]
- public void StartAiming()
- {
- this.Tween(v => _animator.SetLayerWeight(_aimingLayerIndex, v), 0, 1, .1f);
- _animator.SetParameter(Aim, true);
- }
- [Button]
- public void StopAiming()
- {
- this.Tween(v => _animator.SetLayerWeight(_aimingLayerIndex, v), 0, 1, .1f);
- _animator.SetParameter(Aim, false);
- }
- [Button]
- public void RifleEquipped()
- {
- this.Tween(v => _animator.SetLayerWeight(_aimingLayerIndex, v), 0, 1, .1f);
- _animator.SetParameter(Rifle, true);
- }
- [Button]
- public void UnEquip()
- {
- this.Tween(v => _animator.SetLayerWeight(_aimingLayerIndex, v), 1, 0, .25f);
- _animator.SetParameter(Rifle, false);
- _animator.SetParameter(Rifle, false);
- }
- }
Add Comment
Please, Sign In to add comment
Advertisement