Spine工具(根据Spine生成的Animator自动生成状态机)

由于反复操作,状态机连线太麻烦,操作重复,所以写了个工具一键制作:

代码

//=====================================================
// - FileName:      SpineAnimatorCreator.cs
// - Author:       #AuthorName#
// - CreateTime:    #CreateTime#
// - Email:         #AuthorEmail#
// - Description:   
// -  (C) Copyright 2019, webeye,Inc.
// -  All Rights Reserved.
//======================================================
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Qarth;
using UnityEditor;
using UnityEditor.Animations;
using System.IO;
using Object = UnityEngine.Object;

namespace GameWish.Game
{
    public class SpineAnimatorCreator
    {
        private static string[] animNames = new string[] { "idle", "attack", "hit", "death", "walk", "appear" };
        [MenuItem("Assets/SpineAnimTool/AutoBuildAnimator(请选中AnimatorController和他下面的所有clips)")]
        private static void CreateAnimator()
        {
            AnimatorController animator = Selection.activeObject as AnimatorController;
            if (animator == null)
            {
                Log.e("选中物体没有状态机!!!");
                return;
            }

            int len = animator.parameters.Length;
            if (len > 0)
            {
                for (int i = 0; i < len; i++)
                {
                    animator.RemoveParameter(animator.parameters[i]);
                }
            }
            if (animator.layers.Length > 0)
            {
                animator.RemoveLayer(0);
            }
            animator.AddLayer("Base Layer");
            animator.AddParameter("State", AnimatorControllerParameterType.Int);
            Object[] tar = AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetDatabase.GetAssetPath(animator));
            for (int i = 0; i < tar.Length; i++)
            {
                AnimationClip animationClip = new AnimationClip();
                animationClip = tar[i] as AnimationClip;
                if (animationClip != null)
                {
                    if (animationClip.name.Contains("idle") || animationClip.name.Contains("walk") || animationClip.name.Contains("run"))
                    {
                        AnimationClipSettings clipSettings = AnimationUtility.GetAnimationClipSettings(animationClip);
                        clipSettings.loopTime = true;
                        AnimationUtility.SetAnimationClipSettings(animationClip, clipSettings);
                    }
                    animator.AddMotion(animationClip, 0);
                }
            }
            AnimationClip[] animationClips = animator.animationClips;
            AnimatorStateMachine animatorStateMachine = animator.layers[0].stateMachine;
            ChildAnimatorState[] cas = animatorStateMachine.states;
            int stateValue = 0;
            foreach (var item in cas)
            {
                if (item.state.name.Contains("idle"))
                {
                    animatorStateMachine.defaultState = item.state;
                }
                AnimatorStateTransition atb = animatorStateMachine.AddAnyStateTransition(item.state);
                atb.hasExitTime = true;
                atb.exitTime = 0;
                atb.hasFixedDuration = false;
                atb.duration = 0;
                atb.canTransitionToSelf = false;
                atb.hasExitTime = false;
                for (int i = 0; i < animNames.Length; i++)
                {
                    if (item.state.name.Contains(animNames[i]))
                    {
                        stateValue = i;
                    }
                }
                atb.AddCondition(AnimatorConditionMode.Equals, stateValue, "State");
            }
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
    }
}

 

你可能感兴趣的:(Unity)