资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Unity3D拓展编辑器之自动生成AnimatorContr-创新互联

很惭愧从事游戏开发三年半才开始记录,之前也有一些零散信息记录在有道云笔记里,但都不成体系。那就从现在开始吧!

公司主营业务:成都网站制作、网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出精河免费做网站回馈大家。

最近项目刚出完demo,有些衔接的时间,花了一天时间写了一个方便美术同学操作的小工具。第一次写拓展编辑器,做好之后超级兴奋的说,哈哈~这个工具主要是根据某类人物的AnimatorController的模板,拷贝并将当前人物的动作自动赋值的过程,避免了手动拖拽动作操作的繁杂和失误。比如,monster一般可以共用AnimatorController的layer信息,只是各个state中的motion要对应各个人物的动作。

当然,写的还不够灵活,有一些目录和命名的要求。

1、AnimatorController模板文件的目录地址:放在该类型人物的文件夹下,如monster文件夹下

2、每个人物模型的skin文件的目录地址:放在该人物的文件夹下,如怪物1文件夹下

3、各个动作文件命名规范:怪物名@动作名,如monster_blk@skin、monster_blk@idlel01

monster文件夹

    —AnimatorController(monster共用的AnimatorController模板文件)

    —monster_blk(怪物1的资源文件夹)

        —Anim(怪物1的动作文件夹)

        —Materials(怪物1的材质文件夹)

        —Tex(怪物1的贴图文件夹)

        —monster_blk@skin(怪物1的skin文件)

    —monster_blkw(怪物2的资源文件夹)

        —Anim(怪物2的动作文件夹)

        —Materials(怪物2的材质文件夹)

        —Tex(怪物2的贴图文件夹)

        —monster_blkw@skin(怪物2的skin文件)

和美术同学规范好细节后,就可以开始编码啦~话不多说,直接上代码~

新建一个EditorAnimatorController脚本,放在工程目录Editor下。

using UnityEditor; using UnityEngine; using System.Collections; using System.IO; public class EditorAnimatorController{         //controller模板名和拷贝后的名称 private static string fileName = "AnimatorController.controller"; [MenuItem("AnimatorController/CreateAnimatorController")] static void CreateAnimatorController() {         //获得当前选中为GameObject格式的文件 object[] objs = Selection.GetFiltered (typeof(GameObject), SelectionMode.Assets); GameObject selectObj = null; if (objs.Length < 1)  { Debug.LogError("Please select a model file"); return; } //判断是否选中了skin文件,名字含有"@skin" foreach (GameObject obj in objs)  { if(obj.name.Contains("@skin")) { selectObj = obj; } }                 //如果没有选中,则提示错误并返回 if (selectObj == null)  { Debug.LogError("Please select a skin file"); return; } //获得模板文件的路径(没有找到获得上一级上上级目录的API,只好使用字符切割了) string selectObjPath = AssetDatabase.GetAssetPath((Object) selectObj); string[] array = selectObjPath.Split('/'); string templetControlPath = null; string animatorControllerPath = null; for (int i = 0; i < array.Length; ++i)  { if(i == (array.Length - 1) ) continue; animatorControllerPath +=  array[i] + "/"; if(i == (array.Length - 2) ) continue; templetControlPath += array[i] + "/"; } //判断模板文件是否在该类型人物文件夹下 if (!File.Exists (templetControlPath + fileName)) { Debug.LogError("The templet control file is missing"); return; }                 //如果还没有生成AnimatorController文件,则将模板文件拷贝 if (!File.Exists (animatorControllerPath + fileName)) { FileUtil.CopyFileOrDirectory (templetControlPath + fileName, animatorControllerPath + fileName); Debug.Log("copy animator control success"); } //注意下方高能坑!之前没有刷新,导致下面的animatorController一直为null AssetDatabase.Refresh (); //通过AssetDatabase类load新拷贝的文件,获得animatorController对象 UnityEditor.Animations.AnimatorController animatorController = AssetDatabase.LoadAssetAtPath (animatorControllerPath + fileName, typeof(UnityEditor.Animations.AnimatorController)) as UnityEditor.Animations.AnimatorController; //获得layer信息 UnityEditor.Animations.AnimatorControllerLayer layer = animatorController.layers[0]; //获得stateMachine信息 UnityEditor.Animations.AnimatorStateMachine sm = layer.stateMachine; //获得该人物文件夹下Anim文件夹中的动作资源  string animPath = animatorControllerPath + "Anim"; string[] assets = AssetDatabase.FindAssets( "t:GameObject" , animPath.Split() ); AnimationClip[] animClip = new AnimationClip[assets.Length]; //获得目录下所有的AnimationClip对象 for (int i = 0; i < assets.Length; ++i) { string path = AssetDatabase.GUIDToAssetPath(assets[i]); animClip[i] = AssetDatabase.LoadAssetAtPath(path, typeof(AnimationClip)) as AnimationClip; } //通过名称匹配,将AnimationClip一一对应上state的motion for (int i = 0; i < sm.states.Length; ++i)  { for(int j = 0 ; j < animClip.Length ; ++j) { if(animClip[j].name.Contains(sm.states[i].state.name))  { sm.states[i].state.motion = animClip[j]; } } } } }

好啦,先记录到这~周一继续写将当前AnimatorController附加到人物身上并生成Prefab到指定目录~

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前题目:Unity3D拓展编辑器之自动生成AnimatorContr-创新互联
链接URL:http://cdkjz.cn/article/dieioj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220