Search Unity

Question Animator: How to get parameter type from script?

Discussion in 'Animation' started by pleasurehouse, Apr 3, 2021.

  1. pleasurehouse

    pleasurehouse

    Joined:
    Oct 15, 2020
    Posts:
    96
    I need to Know the parameter type from script. I need to Play animations like this:

    I know this method exist
    public AnimatorControllerParameter GetParameter(int index);
    https://docs.unity3d.com/ScriptReference/Animator.GetParameter.html
    But i don't know the index... and also it can be change... so i want use the name.

    Code (CSharp):
    1.  
    2.  
    3. //i want a method similar to this
    4. Type type = animator.GetParameterType(parameterName);
    5.  
    6. //i want to do something like this
    7. if( typeof(type) == typeof(bool) )
    8. {
    9.          animator.SetBool(parameterName, true);
    10.          return;
    11. }
    12.  
    13. if( typeof(type)==typeof(Trigger))
    14. {
    15.          animator.SetTriggerl(parameterName);
    16.          return;
    17. }
    18.  
    19.  


    Is it possible to do? How?
    Thank you so much!!
     
    Last edited: Apr 3, 2021
  2. pleasurehouse

    pleasurehouse

    Joined:
    Oct 15, 2020
    Posts:
    96
    ok... i have this... but i think it is not good method to use in "Update()"

    Code (CSharp):
    1.  
    2.  
    3.         AnimatorControllerParameterType GetParameterType(string parameterName)
    4.         {
    5.             AnimatorControllerParameter param;
    6.  
    7.             for (int index = 0; index < animator.parameters.Length; index++)
    8.             {
    9.                 param = animator.GetParameter(index);
    10.  
    11.                 if (param.name == parameterName)
    12.                 {
    13.                     return param.type;
    14.                 }
    15.             }
    16.             return (AnimatorControllerParameterType)(-1);
    17.         }
    18.  
    a better way to do this?
     
    Last edited: Apr 3, 2021
    krakadushka likes this.