From 7efe595e0758fd398ec253d2e2c030dd8619862b Mon Sep 17 00:00:00 2001 From: Anatoly Brizhan Date: Mon, 15 Jul 2024 17:41:08 +0200 Subject: [PATCH 1/2] Add Animator and AnimatorController extensions --- .../AnimatorControllerExtensions.cs | 77 ++++++++++++ .../Runtime/Extensions/AnimatorExtensions.cs | 119 ++++++++++++++++++ .../Extensions/AnimatorExtensions.cs.meta | 3 + 3 files changed, 199 insertions(+) create mode 100644 Assets/BetterCommons/Editor/Extensions/AnimatorControllerExtensions.cs create mode 100644 Assets/BetterCommons/Runtime/Extensions/AnimatorExtensions.cs create mode 100644 Assets/BetterCommons/Runtime/Extensions/AnimatorExtensions.cs.meta diff --git a/Assets/BetterCommons/Editor/Extensions/AnimatorControllerExtensions.cs b/Assets/BetterCommons/Editor/Extensions/AnimatorControllerExtensions.cs new file mode 100644 index 0000000..887aa52 --- /dev/null +++ b/Assets/BetterCommons/Editor/Extensions/AnimatorControllerExtensions.cs @@ -0,0 +1,77 @@ +using System.Linq; +using UnityEditor.Animations; +using UnityEngine; + +namespace Better.Commons.EditorAddons.Extensions +{ + public static class AnimatorControllerExtensions + { + #region Get Parameters + + public static string[] GetAllIntegerNames(this AnimatorController animator) + { + return animator.GetParameterNamesOfType(AnimatorControllerParameterType.Int); + } + + public static string[] GetAllFloatNames(this AnimatorController animator) + { + return animator.GetParameterNamesOfType(AnimatorControllerParameterType.Float); + } + + public static string[] GetAllBoolNames(this AnimatorController animator) + { + return animator.GetParameterNamesOfType(AnimatorControllerParameterType.Bool); + } + + private static string[] GetAllTriggerNames(this AnimatorController animator) + { + return animator.GetParameterNamesOfType(AnimatorControllerParameterType.Trigger); + } + + public static string[] GetParameterNamesOfType(this AnimatorController animator, AnimatorControllerParameterType parameterType) + { + return animator.GetParametersOfType(parameterType) + .Select(p => p.name) + .ToArray(); + } + + #endregion + + #region Has Parameters + + public static bool HasParameter(this AnimatorController animator, string name) + { + var names = animator.parameters.Select(p => p.name); + return names.Contains(name); + } + + public static bool HasInteger(this AnimatorController animator, string name) + { + return animator.GetAllIntegerNames().Contains(name); + } + + public static bool HasFloat(this AnimatorController animator, string name) + { + return animator.GetAllFloatNames().Contains(name); + } + + public static bool HasBool(this AnimatorController animator, string name) + { + return animator.GetAllBoolNames().Contains(name); + } + + public static bool HasTrigger(this AnimatorController animator, string name) + { + return animator.GetAllTriggerNames().Contains(name); + } + + #endregion + + private static AnimatorControllerParameter[] GetParametersOfType(this AnimatorController animator, AnimatorControllerParameterType parameterType) + { + return animator.parameters + .Where(p => p.type == parameterType) + .ToArray(); + } + } +} \ No newline at end of file diff --git a/Assets/BetterCommons/Runtime/Extensions/AnimatorExtensions.cs b/Assets/BetterCommons/Runtime/Extensions/AnimatorExtensions.cs new file mode 100644 index 0000000..2cdad5a --- /dev/null +++ b/Assets/BetterCommons/Runtime/Extensions/AnimatorExtensions.cs @@ -0,0 +1,119 @@ +using System.Linq; +using UnityEngine; +using Parameter = UnityEngine.AnimatorControllerParameter; +using ParameterType = UnityEngine.AnimatorControllerParameterType; + +namespace Better.Commons.EditorAddons.Extensions +{ + public static class AnimatorExtensions + { + #region Get Parameters + + public static string[] GetAllIntegerNames(this Animator animator) + { + return animator.GetParameterNamesOfType(AnimatorControllerParameterType.Int); + } + + public static string[] GetAllFloatNames(this Animator animator) + { + return animator.GetParameterNamesOfType(AnimatorControllerParameterType.Float); + } + + public static string[] GetAllBoolNames(this Animator animator) + { + return animator.GetParameterNamesOfType(AnimatorControllerParameterType.Bool); + } + + private static string[] GetAllTriggerNames(this Animator animator) + { + return animator.GetParameterNamesOfType(AnimatorControllerParameterType.Trigger); + } + + public static string[] GetParameterNamesOfType(this Animator animator, ParameterType parameterType) + { + return animator.GetParametersOfType(parameterType) + .Select(p => p.name) + .ToArray(); + } + + #endregion + + #region Has Parameters + + public static bool HasParameter(this Animator animator, string name) + { + var names = animator.parameters.Select(p => p.name); + return names.Contains(name); + } + + public static bool HasInteger(this Animator animator, string name) + { + return animator.GetAllIntegerNames().Contains(name); + } + + public static bool HasFloat(this Animator animator, string name) + { + return animator.GetAllFloatNames().Contains(name); + } + + public static bool HasBool(this Animator animator, string name) + { + return animator.GetAllBoolNames().Contains(name); + } + + public static bool HasTrigger(this Animator animator, string name) + { + return animator.GetAllTriggerNames().Contains(name); + } + + #endregion + + #region Set Parameters + + public static void ResetAllTriggers(this Animator animator) + { + var names = animator.GetAllTriggerNames(); + + for (var i = 0; i < names.Length; i++) + { + animator.ResetTrigger(names[i]); + } + } + + public static void SetAllBools(this Animator animator, bool value) + { + var names = animator.GetAllBoolNames(); + for (var i = 0; i < names.Length; i++) + { + animator.SetBool(names[i], value); + } + } + + public static void SetAllIntegers(this Animator animator, int value) + { + var names = animator.GetAllIntegerNames(); + for (var i = 0; i < names.Length; i++) + { + animator.SetInteger(names[i], value); + } + } + + public static void SetAllFloats(this Animator animator, float value) + { + var names = animator.GetAllFloatNames(); + for (var i = 0; i < names.Length; i++) + { + animator.SetFloat(names[i], value); + } + } + + #endregion + + private static Parameter[] GetParametersOfType(this Animator animator, ParameterType parameterType) + { + return animator.parameters + .Where(p => p.type == parameterType) + .ToArray(); + } + } +} \ No newline at end of file diff --git a/Assets/BetterCommons/Runtime/Extensions/AnimatorExtensions.cs.meta b/Assets/BetterCommons/Runtime/Extensions/AnimatorExtensions.cs.meta new file mode 100644 index 0000000..5f0bd8d --- /dev/null +++ b/Assets/BetterCommons/Runtime/Extensions/AnimatorExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 2b11da7b6d5345c9837c97f4a87da6ea +timeCreated: 1721057290 \ No newline at end of file From fdf463c6ab46a18fe4fd44c222a5504009d9ac53 Mon Sep 17 00:00:00 2001 From: Anatoly Brizhan Date: Mon, 15 Jul 2024 17:41:26 +0200 Subject: [PATCH 2/2] Update package version to 0.0.18 --- Assets/BetterCommons/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/BetterCommons/package.json b/Assets/BetterCommons/package.json index 3f66e12..5660900 100644 --- a/Assets/BetterCommons/package.json +++ b/Assets/BetterCommons/package.json @@ -1,7 +1,7 @@ { "name": "com.tdw.better.commons", "displayName": "Better Commons", - "version": "0.0.17", + "version": "0.0.18", "unity": "2021.3", "description": " ", "dependencies": {