[UEP] 모델 파일에 따라 모션 컨트롤러가 있는 사전 제작 부품 생성

4643 단어 #UnityEditorPlugins
나의 동료가 관련 사고방식과 코드를 제공해 주셔서 감사합니다 = =
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;
	}
}

좋은 웹페이지 즐겨찾기