【unity】根据模型与模型动作,自动生成 AnimatorController

21 篇文章 0 订阅
6 篇文章 0 订阅

最近想做一个战斗内容的 demo ,来综合一下有关于战斗的学习知识,所以在网上找了到了一些模型

模型是找到了,但是这些模型大多都是没有相关的 Animator Controller 的,所以想着使用 unity 编辑器的 代码来自动生成

模型的 Controller 这样方便又省事。

 

这是模型的动作

请忽略一下生成的 Controller 

然后先建立一个对应的动作枚举,

public enum AnimationID
{
	None,
	Idle1,
	Idle2,
	attack1,
	attack2,
	crit,
	dance,
	death,
	joke,
	laugh,
	recall,
	run,
	run2,
	spell1,
	spell2,
	spell3,
	spell4,
	taunt,
}

以后播放对应动作的时候也需要使用

下面是具体的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using UnityEditor.Animations;
using System.Reflection;

public class GenerateAnimatorController
{
    private const string ctrlExtention = "controller";

    [MenuItem("Assets/GenerateAnimatorController")]
    private static void GenerateAnimatorControllerFunc()
    {
        Object[] objs = Selection.GetFiltered(typeof(Object), SelectionMode.Assets);
        string[] dirs = new string[objs.Length];
        for (int i = 0; i < objs.Length; i++)
        {
            dirs[i] = AssetDatabase.GetAssetPath(objs[i]);
        }
        CreateAnimatorController(dirs);
    }

    private static void CreateAnimatorController(string[] paths) 
    {
        Dictionary<string, int> animationToId = GetAnimationIDDicti();
        Dictionary<string, string[]> modelToAnimationFiles = new Dictionary<string, string[]>();
        foreach (string dir in paths)
        {
            if (!Directory.Exists(dir))
            {
                Debug.LogError("no path : " + dir);
            }
            DirectoryInfo dirInfo = new DirectoryInfo(dir);
            if(dirInfo.GetFiles("*." + ctrlExtention, SearchOption.TopDirectoryOnly).Length > 0)
            {
                continue;
            }
            FileInfo[] fileInfos = dirInfo.GetFiles("*@*.fbx", SearchOption.AllDirectories);
            if(fileInfos.Length == 0)
            {
                Debug.LogError("不存在 *@*.fbx 文件");
            }
            List<string> animationFiles = new List<string>();
            foreach (FileInfo item in fileInfos)
            {
                animationFiles.Add(dir + "/" + item.Name);
            }
            string controllerName = dir + "/" + dirInfo.Name + "Controller" + "." + ctrlExtention;
            modelToAnimationFiles.Add(controllerName, animationFiles.ToArray());
        }

        foreach (KeyValuePair<string, string[]> keyValue in modelToAnimationFiles)
        {
            string controllerPath = keyValue.Key;
            string[] aniFiles = keyValue.Value;
            List<AnimatorState> states = new List<AnimatorState>();
            AnimatorController controller = AnimatorController.CreateAnimatorControllerAtPath(controllerPath);
            AnimatorStateMachine baseStateMachine = controller.layers[0].stateMachine;
            controller.AddParameter("state", AnimatorControllerParameterType.Int);
            foreach (string aniFile in aniFiles)
            {
                Object[] assets = AssetDatabase.LoadAllAssetsAtPath(aniFile);
                foreach (Object clip in assets)
                {
                    if (clip.GetType() != typeof(AnimationClip)) continue;
                    if (!animationToId.ContainsKey(clip.name)) continue; 
                    AnimatorState state = baseStateMachine.AddState(clip.name);

                    if(animationToId[clip.name] == 0)
                    {
                        baseStateMachine.defaultState = state;
                    }
                    state.motion = (Motion)clip;
                    states.Add(state);
                }
            }
            LinkStatesByStatesList(ref states, ref animationToId);
        }
    }

    private static void LinkStatesByStatesList(ref List<AnimatorState> states, ref Dictionary<string, int> animationToId)
    {
        foreach (AnimatorState exitAnimState in states)
        {
            foreach (AnimatorState otherState in states)
            {
                if(otherState.Equals(exitAnimState))
                {
                    if (otherState.name == "die") continue;
                    AnimatorStateTransition trnas = exitAnimState.AddTransition(otherState);
                    trnas.duration = otherState.motion.averageDuration;
                    trnas.canTransitionToSelf = true;
                    trnas.exitTime = 0;
                    trnas.AddCondition(AnimatorConditionMode.Equals, animationToId[otherState.name], "state");
                    continue;
                }
                else
                {
                    AnimatorStateTransition trans = exitAnimState.AddTransition(otherState);
                    trans.AddCondition(AnimatorConditionMode.Equals, animationToId[otherState.name], "state");
                }
            }
        }
    }

    private static Dictionary<string, int> GetAnimationIDDicti()
    {
        Dictionary<string, int> result = new Dictionary<string, int>();
        FieldInfo[] fields = typeof(AnimationID).GetFields(BindingFlags.Static | BindingFlags.Public);
        for (int i = 0; i < fields.Length; ++i)
        {
            result.Add(fields[i].Name, i);
        }
        return result;
    }
}

 

使用方式是右键选中对应的模型的文件夹,然后就可以生成了

看到这个图,我感觉有点奇怪,因为这不符合我的预期,直到我拖到其中的一些动作的时候,

才发现是符合预期的。

 

以上就是所有内容了。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值