【UEP】根据模型文件生成带有动画控制器的预制件

感谢我的同事提供相关思路和代码 =_=

using System.IO;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;

public class CreatePerfabsAndAnimatorControllers : AssetPostprocessor {
	//Choose Folder in Project -> Get all Fbxs -> Get the clip to make a AnimatorController -> Create a Prefab and bind the AnimatorController

	[MenuItem("Tools/Create Prefab and AnimatorController")]
	public static void Create() {
		Object[] objects = Selection.objects;
		int len = objects.Length;
		if (len == 0) {
			Debug.Log("请选中Project视图的文件夹进行预制件和动画控制器的生成!");
		}

		for (int i = 0; i < len; i++) {
			string resPath = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf('/')) + "/" + AssetDatabase.GetAssetPath(objects[i]);
			DirectoryInfo directoryInfo = new DirectoryInfo(resPath);

			SearchAllFbx(directoryInfo);
		}

		AssetDatabase.Refresh();
	}

	/// 
	/// 搜索所有的fbx文件
	/// 
	/// 
	private static void SearchAllFbx(DirectoryInfo directoryInfo) {
		FileInfo[] fileInfos = directoryInfo.GetFiles();
		foreach (FileInfo fileInfo in fileInfos) {
			if (fileInfo.Extension != ".meta") {
				int index = fileInfo.FullName.IndexOf("Assets");
				string path = fileInfo.FullName.Substring(index);

				Object obj = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject));
				bool performOnce = true;
				string prefabPath = "";
				string animatorControllerPath = "";
				if(obj != null && !obj.name.EndsWith(".meta")) {
					if (performOnce) {
						prefabPath = GetHigherFolderPath(fileInfo) + "Prefabs";
						animatorControllerPath = GetHigherFolderPath(fileInfo) + "AnimatorControllers";
						CreateFolder(prefabPath);
						CreateFolder(animatorControllerPath);
						performOnce = false;
					}

					GameObject prefab = CreatePrefab(obj, prefabPath);
					AnimatorController controller = CreateAnimatorController(obj, animatorControllerPath);
					prefab.GetComponent().runtimeAnimatorController = controller;
				} 
			}
		}

		DirectoryInfo[] directoryInfos = directoryInfo.GetDirectories();
		foreach(DirectoryInfo dir in directoryInfos) {
			SearchAllFbx(dir);
		}
	}

	/// 
	/// 获取当前文件的上一级文件夹路径
	/// 
	/// 
	private static string GetHigherFolderPath(FileInfo fileInfo) {
		string[] dirNames = fileInfo.DirectoryName.Split('\\');
		string path = string.Empty;
		for(int i = 0; i < dirNames.Length - 1; i++) {
			path += dirNames[i] + "/";
		}
		path = path.Substring(path.IndexOf("Assets"));
		return path;
	}

	/// 
	/// 创建文件夹
	/// 
	/// 
	private static void CreateFolder(string path) {
		if (!Directory.Exists(path)) {
			Directory.CreateDirectory(path);
		}
	}

	/// 
	/// 创建预制体
	/// 
	/// 
	/// 
	/// 
	private static GameObject CreatePrefab(Object obj,string path) {
		GameObject prefab = PrefabUtility.CreatePrefab(path + "/" + obj.name + ".prefab", obj as GameObject);
		return prefab;
	}

	/// 
	/// 创建动画控制器
	/// 
	/// 
	/// 
	/// 
	private static AnimatorController CreateAnimatorController(Object obj,string path) {
		string assetPath = AssetDatabase.GetAssetPath(obj);
		AnimationClip animationClip = AssetDatabase.LoadAssetAtPath(assetPath);

		AnimatorController animatorController = AnimatorController.CreateAnimatorControllerAtPath(path +"/" + obj.name + ".controller");
		animatorController.AddParameter("isLoop", AnimatorControllerParameterType.Bool);

		AnimatorControllerLayer animatorControllerLayer = animatorController.layers[0];
		AnimatorStateMachine animatorStateMachine = animatorControllerLayer.stateMachine;

		AnimatorState animatorState = animatorStateMachine.AddState(animationClip.name);
		animatorState.motion = animationClip;

		AnimatorState emptyState = animatorStateMachine.AddState("Empty", new Vector3(animatorStateMachine.entryPosition.x + 400,
			animatorStateMachine.entryPosition.y + 400, 0));

		AnimatorStateTransition animatorStateTransition = animatorState.AddTransition(emptyState);
		animatorStateTransition.hasExitTime = true;

		animatorStateTransition = emptyState.AddTransition(animatorState);
		animatorStateTransition.AddCondition(AnimatorConditionMode.If, 1, "isLoop");

		return animatorController;
	}
}

你可能感兴趣的:(#,Unity,Editor,Plugins)