关于unity3d动画控制器的一些小工具的写法

一般动作类游戏的工程目录一定会有这样的目录结构



一般会由策划或者动画师把这么多的动画拉进一个animator中.是不是这位负责人每次都想泪奔到厕所画圈圈.今天就是把这么多步操作简化成程序一键创建。

我用的是unity3d 5.1.1f1

我直接上代码吧~


<pre name="code" class="csharp">[MenuItem("Assets/GameTools/AnimTools/CreateAnimator")]
	public static void CreateAnimator()
	{
		string path;
		path = AssetDatabase.GetAssetPath( Selection.activeObject.GetInstanceID ());
		path = path.Replace(".FBX","_animator.controller");
		Debug.Log ("Path is " + path);
		UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath (path);
		AnimatorController ac = AssetDatabase.LoadAssetAtPath<AnimatorController> (path);
		///Get all animclip from dir

		string animclipPath = path.Replace ("/" + ac.name + ".controller", "");
		animclipPath = animclipPath.Replace("Assets","");
		Debug.Log ("AnimclipPath is " + animclipPath);

		string objPath = Application.dataPath + animclipPath;  
		string[] animclipStr;  
		Debug.Log ("objPath is " + objPath);
		//返回指定的目录中文件和子目录的名称的数组或空数组
		animclipStr = System.IO.Directory.GetFileSystemEntries(objPath);   
		AnimationClip clip;
		int animclipCount = 0;
		int attackIndex = 0;
		int otherIndex = 0;
		AnimatorState defaultState = new AnimatorState ();
		bool isDazhaoqianExist = false;
		string idleClipPath = "";
		for (int i = 0; i < animclipStr.Length; i++) 
		{
			if (animclipStr [i].EndsWith (".anim")) {
				animclipCount++;
				animclipPath = animclipStr [i].Replace ("/client", ";");
				string[] pathSplited = animclipPath.Split (';');
				path = pathSplited [1];
				path = path.Remove (0, 1);
				path = path.Replace ('\\', '/');
				Debug.Log ("animclipPath is " + path);
				clip = AssetDatabase.LoadAssetAtPath<AnimationClip>(path);
				Motion m = clip as Motion;
				if(clip.name.Contains(""))

				if(clip.name.Contains("attack") || clip.name.Contains("skill") || clip.name.Contains("bati"))
				{
					ac.layers[0].stateMachine.AddState(clip.name,new Vector3(AttackPosXInAnimator,attackIndex * HeightInAnimator,0)).motion = m;
					attackIndex++;

				}
				else if(clip.name == "idle")
				{
					idleClipPath = AssetDatabase.GetAssetPath(clip.GetInstanceID());
					defaultState = ac.layers[0].stateMachine.AddState(clip.name,new Vector3(OtherPosXInAnimator,otherIndex * HeightInAnimator,0));
					defaultState.motion = m;
					otherIndex++;
				}
				else
				{
					ac.layers[0].stateMachine.AddState(clip.name,new Vector3(OtherPosXInAnimator,otherIndex * HeightInAnimator,0)).motion = m;
					otherIndex++;
				}

			}
		}
		ac.layers [0].stateMachine.defaultState = defaultState;

	}


 


用法是右击原模就是那个唯一的fbx.选择GameTools/AnimTools/CreateAnimator

原理是把被选择的fbx同目录后缀为.anim的文件全部加入到一个新的animatorcontroller里面。然后以idle为最初始的状态。

我这里做了个小处理。把攻击型技能分到了左边,状态类动画分到右边。容易分辨。

PS:如果原目录下有animatorcontroller是会覆盖掉的。所以如果改了animator中的数据例如:skill1的IKFoot是勾上的。那么再次CreateAnimator时数据会丢失。需要重新勾选0 0、

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值