昨天研究了下Unity中AnimatorController自动生成。今天稍微完善了操作流程,并且补充说明了在MMO中如何使用模型资源(AssetBundle),这个使用方法是MMO这样模型资源非常大的情况下才需要的。如果是比较小的情况下,直接塞到Resources目录下,加载Prefab就OK了,根本不用操心这些问题。
先附上最终修改后的脚本。它创建了一个菜单选项,选择一个文件夹(或者文件),右键选择CreateAnimation就可以自动遍历所有的模型资源,然后创建AnimatorController并创建Prefab文件。在Prefab都生成完毕后(或者在生成的同时),可以使用创建AssetBundle的功能在磁盘任意目录创建AssetBundle。
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using System.IO;
using System.Collections;
using System.Collections.Generic;
public class CreateAnimation : Editor
{
//生成出的Prefab的路径
private static readonly string PREFAB_PATH = "Assets/Prefab/";
private static readonly string PATH_TAG = "Assets/";
private static bool _ingnoreExist = true;
// 这些动画是循环播放的(除此之外的循环动画需要手动创建)
private static readonly string[] LOOP_TAG = new string[] { "idle", "walk", "run" };
[MenuItem("Assets/CreateAnimation")]
static void DoCreateAnimation()
{
UnityEngine.Object obj = Selection.activeObject;
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
if (Directory.Exists(path)) {
// 如果是路径的话
Walk(path, (string fbxPath)=>{
fbxPath = fbxPath.Replace("\\", "/").Replace(".FBX", ".fbx");
string relativePath = fbxPath.Substring(fbxPath.IndexOf(PATH_TAG));
if (!relativePath.EndsWith(".fbx")) {
return;
}
DoCreateController(relativePath);
DoCreatePrefab(relativePath);
});
} else if (File.Exists(path)) {
// 如果是文件的话
string fbxPath = path.Replace("\\", "/").Replace(".FBX", ".fbx");
string relativePath = fbxPath.Substring(fbxPath.IndexOf(PATH_TAG));
if (!relativePath.EndsWith(".fbx")) {
return;
}
DoCreateController(relativePath);
DoCreatePrefab(relativePath);
} else {
// 路径不存在
Debug.LogError(string.Format("路径不存在: {0}", path));
}