用脚本创建带animator的prefab
2016-02-16 14:04
288 查看
之前简单写过用脚本创建Unity动画控制器animator,这次写的是用脚本创建prefab,源代码如下:
链接:http://pan.baidu.com/s/1mhrnNfU 密码:wxm6
/* * QQ:765459020 * * Unity编辑器 * * 这里所用资源为Unity自带的2D素材 * 为了方便,我们取其中的Idle和Run两个animation进行操作 * 取Idle为默认状态,Run停止时返回Idle状态 * PS:编辑器并未考虑性能问题 * * 假设是在已有的prefab上修改 * 可以先将原有prefab实例化 * 然后删除即可 * * 2016-02-15 * */ using UnityEngine; using System.Collections; using System.Collections.Generic; //文件读写 using System.IO; //Unity基础编辑器 using UnityEditor; //Unity基础动画编辑 using UnityEditor.Animations; public class BuildingEditor : MonoBehaviour { //读取animation地址,为了方便,暂且将创建的animator放在该文件夹内 private static string ReadAnimationPath = Application.dataPath + "/Standard Assets/2D/Animations/"; //读取prefab地址 private stat 4000 ic string ReadPrefabPath = Application.dataPath + "/Standard Assets/2D/Prefabs/"; //创造animator地址 private static string CreateAnimatorPath = "Assets/Standard Assets/2D/Animations/"; //创造prefab地址 private static string CreatePrefabPath = "Assets/Resources/Prefabs/"; //创建的animator名字,这里一定要注意加后缀".controller" private const string ANIMATOR_NAME = "Example_Animator.controller"; //创建prefab名字 private const string PREFAB_NAME = "Example_Prefab.prefab"; private const string IDLE_ANIMATION = "RobotBoyIdle"; private const string RUN_ANIMATION = "RobotBoyRun"; private const string ROBOT_PREFAB = "CharacterRobotBoy.prefab"; /// <summary> /// 创建Prefab实例 /// MenuItem:声明该内容对应主菜单 /// </summary> [MenuItem("Build/Example_Prefab")] public static void BuildExamplePrefab() { //先根据现有动画创造animator BuildExampleAnimator(); //读取目录信息 DirectoryInfo info1 = new DirectoryInfo(ReadAnimationPath); //获取刚刚创建的controller文件 FileInfo[] file1 = info1.GetFiles("*.controller"); //读取目录信息 DirectoryInfo info2 = new DirectoryInfo(ReadPrefabPath); //获取该目录下的全部prefab FileInfo[] file2 = info2.GetFiles("*.prefab"); for (int i = 0; i < file2.Length; i++) { if (file2[i].Name == ROBOT_PREFAB) { //先将目前已有的prefab实例化 GameObject go = Instantiate(AssetDatabase.LoadAssetAtPath<GameObject>(DataPathToAssetPath(file2[i].FullName))); //表示该prefab处于更改状态(可以没有) EditorUtility.SetDirty(go); Selection.activeObject = go; //添加animator if (!go.GetComponent<Animator>()) { go.AddComponent<Animator>(); } //设置animator为刚刚创建的animator go.GetComponent<Animator>().runtimeAnimatorController = AssetDatabase.LoadAssetAtPath<RuntimeAnimatorController>(DataPathToAssetPath(file1[0].FullName)); //5.0新功能,根节点动画 go.GetComponent<Animator>().applyRootMotion = true; //创建prefab PrefabUtility.CreatePrefab(CreatePrefabPath + PREFAB_NAME, go); //Editor模式下只能用这个 DestroyImmediate(go); } } //资源进行刷新保存,否则有可能丢失代码创造的资源 AssetDatabase.Refresh(); AssetDatabase.SaveAssets(); } /// <summary> /// 创建animator实例 /// </summary> private static void BuildExampleAnimator() { //读取目录信息 DirectoryInfo info = new DirectoryInfo(ReadAnimationPath); //获取全部动画文件 FileInfo[] file1 = info.GetFiles("*.anim"); AnimatorController animator = AnimatorController.CreateAnimatorControllerAtPath(CreateAnimatorPath + 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/1mhrnNfU 密码:wxm6
相关文章推荐
- 图解-自我理解的框架
- jsf 表单验证
- JavaScript实现跑马灯抽奖活动实例代码解析与优化(二)
- HDOJ 1042 N!(大数阶乘JAVA)
- 制作矢量图
- Eclipse英文版安装简体中文语言包
- 关于烂代码的那些事(下)
- Linux服务软链接
- JS公共方法:全部反选,获取url指定的参数,取值保留两位小数...
- 前端优化带来的思考,浅谈前端工程化【转】
- Xcode中pch文件的使用以及注意事项
- 最大似然估计(Maximum likelihood estimation)
- 配置sonar和jenkins进行代码审查
- You don't have permission to access / on this server.
- 前端优化带来的思考,浅谈前端工程化【转】
- narnia5
- 通过HTML5的getUserMedia实现拍照功能
- POJ2400 KM最大匹配
- Linux下jdk安装
- poj2400===km