这个是我现在所知最准确的MecAnim的用法了。先上代码:
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using System;
using System.Collections;
using System.Collections.Generic;
public class GetAnimationClips : AssetPostprocessor
{
void OnPostprocessModel(GameObject go)
{
List<AnimationClip> clips = new List<AnimationClip>();
UnityEngine.Object[] objects = AssetDatabase.LoadAllAssetsAtPath(assetPath);
Debug.Log(objects.Length);
foreach (var obj in objects) {
AnimationClip clip = obj as AnimationClip;
if (clip != null && clip.name.IndexOf("__preview__") == -1) {
clips.Add(clip);
}
}
CreateAnimationController(assetPath, clips);
}
static void CreateAnimationController(string path, List<AnimationClip> clips)
{
path = path.Replace("\", "/").Replace(".FBX", ".fbx");
string acPath = path.Replace(".fbx", ".controller");
string prefabPath = path.Replace(".fbx", ".prefab");
// 创立动画掌握器
AnimatorController animatorController = AnimatorController.CreateAnimatorControllerAtPath(acPath);
AnimatorControllerLayer layer = animatorController.GetLayer(0);
UnityEditorInternal.StateMachine sm = layer.stateMachine;
Vector3 anyStatePosition = sm.anyStatePosition;
float OFFSET_X = 220;
float OFFSET_Y = 60;
float ITEM_PER_LINE = 4;
float originX = anyStatePosition.x - OFFSET_X * (ITEM_PER_LINE / 2.5f);
float originY = anyStatePosition.y + OFFSET_Y;
float x = originX;
float y = originY;
foreach (AnimationClip newClip in clips) {
State state = sm.AddState(newClip.name.ToLower());
state.SetAnimationClip(newClip, layer);
state.position = new Vector3(x, y, 0);
x += OFFSET_X;
if (x >= originX + OFFSET_X * ITEM_PER_LINE) {
x = originX;
y += OFFSET_Y;
}
}
// 创立prefab
string name = path.Substring(path.LastIndexOf("/") + 1).Replace(".fbx", "");
prefabPath = "Assets/Resources/" + name + ".prefab";
GameObject go = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
go.name = name;
Animator animator = go.GetComponent<Animator>();
if (animator == null) {
animator = go.AddComponent<Animator>();
}
animator.applyRootMotion = false;
animator.runtimeAnimatorController = animatorController;
//AssetDatabase.CreateAsset(go, prefabPath);
PrefabUtility.CreatePrefab(prefabPath, go);
GameObject.DestroyImmediate(go, true);
AssetDatabase.SaveAssets();
}
}using Unit