用脚本创建animator控制器

来源:互联网 发布:农业 知乎 编辑:程序博客网 时间:2024/05/30 02:53
    依旧博客新手,没有那么老练的文字描述以及层层递进水准,所以暂且所有解释性内容均在代码里进行比较详细的说明,文末附上工程链接。
/* * QQ:765459020 * * 用代码生成animator控制器 * * 这里所用资源为Unity自带的2D素材 * 为了方便,我们取其中的Idle和Run两个animation进行操作 * 取Idle为默认状态,Run停止时返回Idle状态 * PS:编辑器并未考虑性能问题 * * 2016-02-15 * */using UnityEngine;using System.Collections;using System.Collections.Generic;//文件读写using System.IO;//Unity基础编辑器using UnityEditor;//Unity基础动画编辑using UnityEditor.Animations;public class BuildingAnimator : MonoBehaviour{    //读取动画地址,为了方便,暂且将创建的animator放在该文件夹内    private static string ReadExamplePath = Application.dataPath + "/Standard Assets/2D/Animations/";    //创造animator地址    private static string CreateExamplePath = "Assets/Standard Assets/2D/Animations/";    //创建的animator名字,这里一定要注意加后缀".controller"    private const string ANIMATOR_NAME = "Example_Animator.controller";    private const string IDLE_ANIMATION = "RobotBoyIdle";    private const string RUN_ANIMATION = "RobotBoyRun";    /// <summary>    /// 创建实例    /// MenuItem:声明该内容对应主菜单    /// </summary>    [MenuItem("Build/Example_Animator")]    public static void BuildExampleAnimator()    {        //读取目录信息        DirectoryInfo info = new DirectoryInfo(ReadExamplePath);        //获取全部动画文件        FileInfo[] file1 = info.GetFiles("*.anim");        AnimatorController animator = AnimatorController.CreateAnimatorControllerAtPath(CreateExamplePath + ANIMATOR_NAME);        //controller里边有层级关系,这里我们取其默认层级,即Base Layer        AnimatorControllerLayer layer = animator.layers[0];        for (int i = 0; i < file1.Length; i++)        {            AnimationClip clip = AssetDatabase.LoadAssetAtPath<AnimationClip>(DataPathToAssetPath(file1[i].FullName));            if (clip.name == IDLE_ANIMATION)            {                //添加名为clip.name的状态,其位置为(250,100,0)                AnimatorState state = layer.stateMachine.AddState(clip.name, new Vector3(250, 100, 0));                //该状态的动画为clip                state.motion = clip;                //设置该状态为默认状态                layer.stateMachine.defaultState = state;            }        }        for (int i = 0; i < file1.Length; i++)        {            AnimationClip clip = AssetDatabase.LoadAssetAtPath<AnimationClip>(DataPathToAssetPath(file1[i].FullName));            if (clip.name == RUN_ANIMATION)            {                AnimatorState state = layer.stateMachine.AddState(clip.name, new Vector3(250, 0, 0));                state.motion = clip;                //添加转换状态,true代表激活退出状态                state.AddTransition(layer.stateMachine.defaultState, true);                //这是让播完动画才能退出                state.transitions[0].exitTime = 1F;                state.transitions[0].duration = 0;            }        }        //资源进行刷新保存,否则有可能丢失代码创造的资源        AssetDatabase.Refresh();        AssetDatabase.SaveAssets();    }    /// <summary>    /// 系统路径转换为Unity路径    /// </summary>    /// <param name="path">路径</param>    /// <returns></returns>    public static string DataPathToAssetPath(string path)    {        if (Application.platform == RuntimePlatform.WindowsEditor)        {            return path.Substring(path.IndexOf("Assets\\"));        }        else        {            return path.Substring(path.IndexOf("Assets/"));        }    }}
    百度云盘链接:
    链接:http://pan.baidu.com/s/1pJUM8Gz 密码:ept3


0 0
原创粉丝点击