[Unity] Animator Tester



本篇文章主要針對 Animator 建立了一個小測試工具
能夠自動循環播放檢視所有 Animator 下的 Animation States
測試資源:Fighting Unity Chan

Overview

1. 取得 Animator 下的所有 Animation States
2. 自動循環播放
3. 自製編輯器

AnimatorTester 測試影片


1. 取得 Animator 下的所有 Animation States

利用 Animator 下的 RuntimeAnimatorController
我們可以取得 Animation FSM 中的所有 States

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. 自動循環播放

Animator 播放後,可以透過 GetCurrentAnimatorStateInfo
來取得目前的 State 長度
再利用 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. 自製編輯器
#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 完整專案

AnimatorTester.cs
AnimatorTesterInspector.cs




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

Related Post:

Tagged on: