using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI; namespace MoreMountains.Tools { /// /// Animator extensions /// public static class MMAnimatorExtensions { /// /// Determines if an animator contains a certain parameter, based on a type and a name /// /// true if has parameter of type the specified self name type; otherwise, false. /// Self. /// Name. /// Type. public static bool MMHasParameterOfType(this Animator self, string name, AnimatorControllerParameterType type) { if (string.IsNullOrEmpty(name)) { return false; } AnimatorControllerParameter[] parameters = self.parameters; foreach (AnimatorControllerParameter currParam in parameters) { if (currParam.type == type && currParam.name == name) { return true; } } return false; } /// /// Adds an animator parameter name to a parameter list if that parameter exists. /// /// /// /// /// /// public static void AddAnimatorParameterIfExists(Animator animator, string parameterName, out int parameter, AnimatorControllerParameterType type, HashSet parameterList) { if (string.IsNullOrEmpty(parameterName)) { parameter = -1; return; } parameter = Animator.StringToHash(parameterName); if (animator.MMHasParameterOfType(parameterName, type)) { parameterList.Add(parameter); } } /// /// Adds an animator parameter name to a parameter list if that parameter exists. /// /// /// /// /// public static void AddAnimatorParameterIfExists(Animator animator, string parameterName, AnimatorControllerParameterType type, HashSet parameterList) { if (animator.MMHasParameterOfType(parameterName, type)) { parameterList.Add(parameterName); } } // SIMPLE METHODS ------------------------------------------------------------------------------------------------------------------------------------------------------------- #region SimpleMethods // /// Updates the animator bool. /// /// Animator. /// Parameter name. /// If set to true value. public static void UpdateAnimatorBool(Animator animator, string parameterName, bool value) { animator.SetBool(parameterName, value); } /// /// Updates the animator integer. /// /// Animator. /// Parameter name. /// Value. public static void UpdateAnimatorInteger(Animator animator, string parameterName, int value) { animator.SetInteger(parameterName, value); } /// /// Updates the animator's float /// /// /// /// public static void UpdateAnimatorFloat(Animator animator, string parameterName, float value, bool performSanityCheck = true) { animator.SetFloat(parameterName, value); } #endregion // INT PARAMETER METHODS ------------------------------------------------------------------------------------------------------------------------------------------------------------- // /// Updates the animator bool. /// /// Animator. /// Parameter name. /// If set to true value. public static bool UpdateAnimatorBool(Animator animator, int parameter, bool value, HashSet parameterList, bool performSanityCheck = true) { if (performSanityCheck && !parameterList.Contains(parameter)) { return false; } animator.SetBool(parameter, value); return true; } /// /// Sets an animator's trigger of the int parameter specified /// /// /// /// public static bool UpdateAnimatorTrigger(Animator animator, int parameter, HashSet parameterList, bool performSanityCheck = true) { if (performSanityCheck && !parameterList.Contains(parameter)) { return false; } animator.SetTrigger(parameter); return true; } /// /// Triggers an animator trigger. /// /// Animator. /// Parameter name. /// If set to true value. public static bool SetAnimatorTrigger(Animator animator, int parameter, HashSet parameterList, bool performSanityCheck = true) { if (performSanityCheck && !parameterList.Contains(parameter)) { return false; } animator.SetTrigger(parameter); return true; } /// /// Updates the animator float. /// /// Animator. /// Parameter name. /// Value. public static bool UpdateAnimatorFloat(Animator animator, int parameter, float value, HashSet parameterList, bool performSanityCheck = true) { if (performSanityCheck && !parameterList.Contains(parameter)) { return false; } animator.SetFloat(parameter, value); return true; } /// /// Updates the animator integer. /// /// Animator. /// Parameter name. /// Value. public static bool UpdateAnimatorInteger(Animator animator, int parameter, int value, HashSet parameterList, bool performSanityCheck = true) { if (performSanityCheck && !parameterList.Contains(parameter)) { return false; } animator.SetInteger(parameter, value); return true; } // STRING PARAMETER METHODS ------------------------------------------------------------------------------------------------------------------------------------------------------------- #region StringParameterMethods // /// Updates the animator bool. /// /// Animator. /// Parameter name. /// If set to true value. public static void UpdateAnimatorBool(Animator animator, string parameterName, bool value, HashSet parameterList, bool performSanityCheck = true) { if (parameterList.Contains(parameterName)) { animator.SetBool(parameterName, value); } } /// /// Sets an animator's trigger of the string parameter name specified /// /// /// /// public static void UpdateAnimatorTrigger(Animator animator, string parameterName, HashSet parameterList, bool performSanityCheck = true) { if (parameterList.Contains(parameterName)) { animator.SetTrigger(parameterName); } } /// /// Triggers an animator trigger. /// /// Animator. /// Parameter name. /// If set to true value. public static void SetAnimatorTrigger(Animator animator, string parameterName, HashSet parameterList, bool performSanityCheck = true) { if (parameterList.Contains(parameterName)) { animator.SetTrigger(parameterName); } } /// /// Updates the animator float. /// /// Animator. /// Parameter name. /// Value. public static void UpdateAnimatorFloat(Animator animator, string parameterName, float value, HashSet parameterList, bool performSanityCheck = true) { if (parameterList.Contains(parameterName)) { animator.SetFloat(parameterName, value); } } /// /// Updates the animator integer. /// /// Animator. /// Parameter name. /// Value. public static void UpdateAnimatorInteger(Animator animator, string parameterName, int value, HashSet parameterList, bool performSanityCheck = true) { if (parameterList.Contains(parameterName)) { animator.SetInteger(parameterName, value); } } // /// Updates the animator bool after checking the parameter's existence. /// /// Animator. /// Parameter name. /// If set to true value. public static void UpdateAnimatorBoolIfExists(Animator animator, string parameterName, bool value, bool performSanityCheck = true) { if (animator.MMHasParameterOfType(parameterName, AnimatorControllerParameterType.Bool)) { animator.SetBool(parameterName, value); } } /// /// Updates an animator trigger if it exists /// /// /// public static void UpdateAnimatorTriggerIfExists(Animator animator, string parameterName, bool performSanityCheck = true) { if (animator.MMHasParameterOfType(parameterName, AnimatorControllerParameterType.Trigger)) { animator.SetTrigger(parameterName); } } /// /// Triggers an animator trigger after checking for the parameter's existence. /// /// Animator. /// Parameter name. /// If set to true value. public static void SetAnimatorTriggerIfExists(Animator animator, string parameterName, bool performSanityCheck = true) { if (animator.MMHasParameterOfType(parameterName, AnimatorControllerParameterType.Trigger)) { animator.SetTrigger(parameterName); } } /// /// Updates the animator float after checking for the parameter's existence. /// /// Animator. /// Parameter name. /// Value. public static void UpdateAnimatorFloatIfExists(Animator animator, string parameterName, float value, bool performSanityCheck = true) { if (animator.MMHasParameterOfType(parameterName, AnimatorControllerParameterType.Float)) { animator.SetFloat(parameterName, value); } } /// /// Updates the animator integer after checking for the parameter's existence. /// /// Animator. /// Parameter name. /// Value. public static void UpdateAnimatorIntegerIfExists(Animator animator, string parameterName, int value, bool performSanityCheck = true) { if (animator.MMHasParameterOfType(parameterName, AnimatorControllerParameterType.Int)) { animator.SetInteger(parameterName, value); } } #endregion } }