[Unity] Animator Tester



This article create a small testor for Animator, can do automatically circular play and test all the Animation States.
Test Resources:Fighting Unity Chan

Overview

1. Get all the Animation States from Animator
2. Auto circular playing
3. Custom editor

Test Video


1. Get all the Animation States from Animator

We can get all the animation FSM states by using RuntimeAnimatorController of Animator

m_states = new List<string>();
var ac = m_animator.runtimeAnimatorController as UnityEditor.Animations.AnimatorController;
var layers = ac.layers;

foreach (var layer in layers)
{
    foreach (var state in layer.stateMachine.states)
    {
        m_states.Add(state.state.name);
    }
}
2. Auto circular playing

Use GetCurrentAnimatorStateInfo() to get current animation length and wait by a coroutine.

private IEnumerator AutoTest()
{
    Next();

    yield return new WaitForSeconds(m_animator.GetCurrentAnimatorStateInfo(0).length);
    yield return new WaitForSeconds(1);

    if (m_isAuto)
    {
        StartCoroutine(AutoTest());
    }
}
3. Custom Editor
#if UNITY_EDITOR

using UnityEditor;
using UnityEngine;

namespace HotBloodr.Editor
{
    [CustomEditor(typeof(AnimatorTester)), CanEditMultipleObjects]
    public class AnimatorTesterInspector : UnityEditor.Editor
    {
        private const int BUTTON_WIDTH = 20;
        private const int STATE_FONT_SIZE = 18;

        public override void OnInspectorGUI()
        {
            DrawDefaultInspector();

            DrawStates();

            var tester = target as AnimatorTester;
            var labelStyle = new GUIStyle(GUI.skin.label);
            labelStyle.fontSize = STATE_FONT_SIZE;

            // Next & Previous Button
            GUILayout.BeginHorizontal();
            GUI.backgroundColor = Color.green;
            {
                if (GUILayout.Button("<<"))
                {
                    tester.Previous();
                }

                GUILayout.FlexibleSpace();
                GUILayout.Label(tester.State, labelStyle);
                GUILayout.FlexibleSpace();

                if (GUILayout.Button(">>"))
                {
                    tester.Next();
                }
            }
            GUILayout.EndHorizontal();

            // AutoTest Toggle
            GUI.backgroundColor = Color.white;
            GUIHelper.HorizontalSplitter(1);
            GUILayout.Space(5);
            tester.IsAuto = GUILayout.Toggle(tester.IsAuto, "AutoTest");
            GUILayout.Space(5);

            // Reset Button
            GUI.backgroundColor = Color.yellow;
            {
                if (GUILayout.Button("Reset"))
                {
                    tester.Initialize();
                }
            }
            GUI.backgroundColor = Color.white;
        }

        private void DrawStates()
        {
            var tester = target as AnimatorTester;
            if (tester.States != null)
            {
                foreach (var state in tester.States)
                {
                    GUILayout.BeginHorizontal();
                    {
                        GUILayout.Label(state);
                        GUI.backgroundColor = Color.red;
                        if (GUILayout.Button("▶", GUILayout.Width(BUTTON_WIDTH)))
                        {
                            tester.Play(state);
                        }
                        GUI.backgroundColor = Color.white;
                        GUILayout.EndHorizontal();
                    }
                }
                GUILayout.Space(10);
            }
        }
    }
}

#endif

Github Complete Project

AnimatorTester.cs
AnimatorTesterInspector.cs




歡迎您留言與分享!(Welcome for comments or sharing!)

Related Post:

Tagged on: