Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Custom editor for animation controller

Discussion in 'Animation' started by ProtagonistKun, Jan 21, 2018.

  1. ProtagonistKun

    ProtagonistKun

    Joined:
    Nov 26, 2015
    Posts:
    352
    Hello and good day to you all,

    I have a question regarding the animation controller and the parameters thereof. I am trying to achieve a scalable and completely dynamic script to control the parameters from my animator on a mesh I import with an animation. What I currently have works, but only at runtime.

    What I would like is to be able to set the parameters in editor using the GUI created by the editor script below. And also preferably make a 2-way connection so that if I want to change anything in the animator itself (the actual parameter list in the animator controller) that it updates in the GUI.

    The first block of code is the script attached to the object with the animator, the second is the editor script used to custom serialize the elements to the editor.

    If anyone can point me in the right direction, or provide me a solution that would prove very helpful and I would like to thank everyone for reading and helping in advance.

    EDIT: If I edit the parameter list in the animator controller and after look at the custom inspector i made on an object, all the parameters are gone and I have to reset the prefab. This is an additional issue I would also like some help with if possible.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class AnimationController : MonoBehaviour
    4. {
    5.     private Animator _objectAnimator = null;
    6.     AnimatorControllerParameter[] _parameters;
    7.  
    8.     public Animator ObjectAnimator { get { return _objectAnimator; } set { _objectAnimator = value; } }
    9.     public AnimatorControllerParameter[] Parameters { get { return _parameters; } set { _parameters = value; } }
    10. }
    11.  
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. [CustomEditor(typeof(AnimationController))]
    5. public class AnimationControllerEditor : Editor
    6. {
    7.     public override void OnInspectorGUI()
    8.     {
    9.         AnimationController animControl = (AnimationController)target;
    10.         base.DrawDefaultInspector();
    11.  
    12.         animControl.ObjectAnimator = animControl.GetComponent<Animator>();
    13.             animControl.Parameters = animControl.ObjectAnimator.parameters;
    14.  
    15.         if (animControl.Parameters.Length != 0)
    16.         {
    17.             for (int i = 0; i < animControl.Parameters.Length; i++)
    18.             {
    19.  
    20.                 if (animControl.Parameters[i].type == AnimatorControllerParameterType.Bool
    21.                     || animControl.Parameters[i].type == AnimatorControllerParameterType.Trigger)
    22.                 {
    23.                     bool b = animControl.ObjectAnimator.GetBool(animControl.Parameters[i].name);
    24.                     b = EditorGUILayout.Toggle(animControl.Parameters[i].name, b);
    25.                     animControl.ObjectAnimator.SetBool(animControl.Parameters[i].name, b);
    26.                 }
    27.                 else if (animControl.Parameters[i].type == AnimatorControllerParameterType.Float)
    28.                 {
    29.                     float f = animControl.ObjectAnimator.GetFloat(animControl.Parameters[i].name);
    30.                     f = EditorGUILayout.FloatField(animControl.Parameters[i].name, f);
    31.                     animControl.ObjectAnimator.SetFloat(animControl.Parameters[i].name, f);
    32.                 }
    33.                 else if (animControl.Parameters[i].type == AnimatorControllerParameterType.Int)
    34.                 {
    35.                     int f = animControl.ObjectAnimator.GetInteger(animControl.Parameters[i].name);
    36.                     f = EditorGUILayout.IntField(animControl.Parameters[i].name, f);
    37.                     animControl.ObjectAnimator.SetInteger(animControl.Parameters[i].name, f);
    38.                 }
    39.             }
    40.         }
    41.  
    42.         if (GUI.changed) EditorUtility.SetDirty((AnimationController)target);
    43.     }
    44. }
     
    Last edited: Jan 21, 2018
  2. Keepabee

    Keepabee

    Joined:
    Jul 12, 2012
    Posts:
    58
    This repo seems to have a PropertyDrawer for three utility classes that allow handling AnimatorControllerParameters from the AnimatorController itself.

    https://github.com/miguel12345/AnimatorControllerParameterPicker

    The RuntimeAnimatorController has its problems for what you'd often want to do, pretty much anything for validation or selection in Edit mode: seems like miguel12345 's solution works a bit better by going through SerializedProperties.