【Unity技巧2】簡(jiǎn)單的動(dòng)畫預(yù)覽工具
前言:當(dāng)有比較多動(dòng)畫片段需要預(yù)覽時(shí),需要找到原本的動(dòng)畫文件預(yù)覽比較麻煩。為了方便預(yù)覽較多的動(dòng)畫,就做了這個(gè)工具。


打開位置:頂部菜單欄的Tools/EditorAnimDisplayWindow
使用:把角色模型拽上去,就會(huì)自動(dòng)顯示動(dòng)畫機(jī)內(nèi)的所有動(dòng)畫。
其次是下面的輸入框可以做搜索篩選
源碼:
using UnityEditor;
using UnityEngine;
namespace Assets.Editor
{
??? public class EditorAnimDisplay : EditorWindow
??? {
??????? #region init
??????? private static EditorAnimDisplay instance;
??????? [MenuItem("Tools/EditorAnimDisplayWindow")]
??????? static void PrefabWrapTool()
??????? {
??????????? //獲取當(dāng)前窗口實(shí)例
??????????? instance = EditorWindow.GetWindow<EditorAnimDisplay>();
??????????? instance.Show();
??????????? //ShowUtility() 實(shí)體窗口樣式
??????? }
??????? #endregion
??????? public AnimationClip[] clips;
??????? public GameObject player;
??????? public string Fitter = "";
??????? private AnimationClip curAnimClip;
??????? private float Timer = 0;
??????? private int playCount = 0;
??????? private Vector2 pos = Vector2.zero;
??????? void OnGUI()
??????? {
??????????? player = EditorGUILayout.ObjectField("player", player, typeof(GameObject), true) as GameObject;
??????????? Fitter= EditorGUILayout.TextField("NameFitter",Fitter );
??????????? if (player)
??????????? {
??????????????? clips = player.GetComponent<Animator>().runtimeAnimatorController.animationClips;
??????????????? pos = GUILayout.BeginScrollView(pos, false, false);
??????????????? foreach (var item in clips)
??????????????? {
??????????????????? if (IsShow(item.name)&&GUILayout.Button(item.name))
??????????????????? {
??????????????????????? PlayAnim(item);
??????????????????? }
??????????????? }
??????????????? GUILayout.EndScrollView();
??????????? }
??????? }
??????? private bool IsShow(string clipName)
??????? {
??????????? if (Fitter=="")
??????????????? return true;
??????????? else
??????????? {
??????????????? return clipName.ToLower().Contains(Fitter.ToLower());
??????????? }??????? ?
??????? }
??????? private void PlayAnim(AnimationClip clip)
??????? {
??????????? Timer = 0;
??????????? playCount = 0;
??????????? curAnimClip = clip;
??????????? Selection.activeObject = clip;
??????????? //DragAndDrop.objectReferences[0] = clip;
??????????? //Debug.Log("yns? play");
??????? }
??????? private void Update()
??????? {
??????????????? UpdateAnim(Time.deltaTime);
??????? }
??????? private void UpdateAnim(float delta)
??????? {
??????????? if (curAnimClip != null)
??????????? {
??????????????? Timer += delta;
??????????????? if (Timer > curAnimClip.length && playCount <2)
??????????????? {
??????????????????? playCount++;
??????????????????? Timer = 0;
??????????????? }
??????????????? else
??????????????? {??????????????????? //動(dòng)畫執(zhí)行方法
??????????????????? curAnimClip.SampleAnimation(player, Timer);
??????????????? }
??????????? }
??????? }
??? }
}