public static void SetAnimator(GameObject go, Object selectObj, string outputFile, ImportCharacterType importCharacterType) { Debugger.Log("outputFile:" + outputFile); string prefabPath = AssetDatabase.GetAssetPath(selectObj); string tempPath = "Assets/Template/TemplateController.controller"; string fullPath = string.Empty; string suffix = string.Empty; string goName = selectObj.name; if (goName.EndsWith("_a")) { goName = goName.Substring(0, goName.LastIndexOf("_a")); } if (goName.EndsWith("_b")) { goName = goName.Substring(0, goName.LastIndexOf("_b")); } if (goName.EndsWith("_c")) { goName = goName.Substring(0, goName.LastIndexOf("_c")); } if (goName.EndsWith("_d")) { goName = goName.Substring(0, goName.LastIndexOf("_d")); } if (goName.EndsWith("_e")) { goName = goName.Substring(0, goName.LastIndexOf("_e")); } if (goName.EndsWith("_f")) { goName = goName.Substring(0, goName.LastIndexOf("_f")); } if (goName.EndsWith("_z")) { goName = goName.Substring(0, goName.LastIndexOf("_z")); } switch (importCharacterType) { case ImportCharacterType.Hero: case ImportCharacterType.Monster: //fullPath = outputFile; //fullPath = fullPath.Replace("_a.", "."); //fullPath = fullPath.Replace("_b.", "."); //fullPath = fullPath.Replace("_c.", "."); //fullPath = fullPath.Replace("_d.", "."); //fullPath = fullPath.Replace("_e.", "."); //fullPath = fullPath.Replace("_f.", "."); fullPath = outputFile + ".controller"; break; case ImportCharacterType.Player: fullPath = outputFile + ".controller"; break; //case ImportCharacterType.Transform: // suffix = "_t"; // fullPath = outputFile + suffix + ".controller"; // bool existTransform = ExistTransform(selectObj, prefabPath, suffix); // if (!existTransform) return; // break; } foreach (var s in suffixes) { if (fullPath.Contains(s + ".controller")) { suffix = s; break; } } switch (importCharacterType) { case ImportCharacterType.Hero: case ImportCharacterType.Monster: fullPath = outputFile + ".controller"; fullPath = fullPath.Replace("_a" + suffix + ".", suffix + "."); fullPath = fullPath.Replace("_b" + suffix + ".", suffix + "."); fullPath = fullPath.Replace("_c" + suffix + ".", suffix + "."); fullPath = fullPath.Replace("_d" + suffix + ".", suffix + "."); fullPath = fullPath.Replace("_e" + suffix + ".", suffix + "."); fullPath = fullPath.Replace("_f" + suffix + ".", suffix + "."); fullPath = fullPath.Replace("_z" + suffix + ".", suffix + "."); break; } Debugger.Log(fullPath); if (!File.Exists(fullPath)) { File.Copy(tempPath, fullPath, true); } AssetDatabase.Refresh(); Object obj = AssetDatabase.LoadAssetAtPath(fullPath, typeof(Object)); UnityEditor.Animations.AnimatorController ac = obj as UnityEditor.Animations.AnimatorController; //Debugger.Log(go.name); if (go) { CharacterEntity character = go.GetComponent <CharacterEntity>(); if (character) { character.anim.runtimeAnimatorController = ac; } else { go.GetComponent <Animator>().runtimeAnimatorController = ac; } } UnityEditor.Animations.AnimatorStateMachine asm = ac.layers[0].stateMachine; foreach (var kvp in asm.states) { UnityEditor.Animations.AnimatorState s = kvp.state; //Debugger.Log(kvp.state.name); bool boolFoundMotion = false; //先在模型所在目录搜索含有@motionName名称的fbx文件 DirectoryInfo dir = new DirectoryInfo(prefabPath).Parent; //FileInfo[] files = dir.GetFiles("*.FBX"); FileInfo[] files = dir.GetFiles("*.anim"); foreach (FileInfo f in files) { //if (f.Name.ToUpper().Contains(suffix.ToUpper() + "@" + kvp.state.name.ToUpper() + ".FBX")) if (f.Name.ToUpper().Contains(goName.ToUpper() + suffix.ToUpper() + "@" + kvp.state.name.ToUpper() + ".ANIM")) { string path = f.FullName.Substring(f.FullName.LastIndexOf(@"Assets")); Debugger.Log("path:" + path); AnimationClip m = AssetDatabase.LoadAssetAtPath(path, typeof(AnimationClip)) as AnimationClip; s.motion = m; Debugger.Log("use custom motion:" + f.Name); boolFoundMotion = true; break; } } if (!boolFoundMotion) { if (string.IsNullOrEmpty(suffix)) { Debugger.LogWarning("找不到动作文件:" + goName + suffix + ":" + kvp.state.name); } else { #region 如果没有高阶动画,使用基本动画代替 foreach (FileInfo f in files) { if (f.Name.ToUpper().Contains(goName.ToUpper() + "@" + kvp.state.name.ToUpper() + ".ANIM")) { string path = f.FullName.Substring(f.FullName.LastIndexOf(@"Assets")); Debugger.Log("path:" + path); AnimationClip m = AssetDatabase.LoadAssetAtPath(path, typeof(AnimationClip)) as AnimationClip; s.motion = m; Debugger.Log("use custom motion:" + f.Name); boolFoundMotion = true; break; } } if (!boolFoundMotion) { Debugger.LogWarning("找不到动作文件:" + goName + suffix + ":" + kvp.state.name); } #endregion } } } AssetDatabase.SaveAssets(); }
public static GameObject CreateCharacter(Object selectObj, string outputFile, string suffix, ImportCharacterType importCharacterType) { GameObject parent = new GameObject(); GameObject go = GameObject.Instantiate(selectObj) as GameObject; string name = go.name.Substring(0, go.name.IndexOf('('));//去掉(clone) parent.name = name + suffix; //SetAnimator(go, selectObj, outputFile); go.transform.SetParent(parent.transform, false); CharacterEntity character = null; string racPath = string.Empty; switch (importCharacterType) { case ImportCharacterType.Player: character = parent.AddComponent <PlayerEntity>(); racPath = "Assets/Res/Resources/animcontroller/player/" + name + suffix; break; case ImportCharacterType.Hero: character = parent.AddComponent <HeroEntity>(); racPath = "Assets/Res/Resources/animcontroller/hero/" + name + suffix; break; case ImportCharacterType.Monster: character = parent.AddComponent <EnemyEntity>(); racPath = "Assets/Res/Resources/animcontroller/hero/" + name + suffix; break; } Animator animator = go.GetComponent <Animator>(); animator.applyRootMotion = true; animator.cullingMode = AnimatorCullingMode.AlwaysAnimate; animator.updateMode = AnimatorUpdateMode.Normal; character.anim = animator; if (!string.IsNullOrEmpty(racPath)) { //SetAnimator(go, selectObj, racPath, ImportCharacterType.Transform); switch (importCharacterType) { case ImportCharacterType.Player: SetAnimator(go, selectObj, racPath, ImportCharacterType.Player); break; case ImportCharacterType.Hero: case ImportCharacterType.Monster: SetAnimator(go, selectObj, racPath, importCharacterType); break; } } Transform rootTrans = TransformUtil.Find("Bip001", go.transform, true); if (rootTrans) { character.rootNode = rootTrans.gameObject; } else { Debugger.LogError("can not find Bip001 from Model {0}", name); } go.name = parent.name; //Debugger.LogError(go.name+" "+parent.name); foreach (var r in parent.GetComponentsInChildren <Renderer>(true)) { r.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; r.receiveShadows = false; r.useLightProbes = false; r.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off; //add rim light 暂不开启 //if (r.gameObject.name == go.name) //{ // List<Material> mats = r.sharedMaterials.ToList(); // string goDir = AssetDatabase.GetAssetPath(selectObj); // goDir = goDir.Replace(go.name + ".FBX", string.Empty); // Debugger.Log(goDir); // string matPath = goDir + "materials/" + go.name + "_Rim_Lighting.mat"; // Debugger.Log(matPath); // Material rimMat = GetRimLightMat(goDir, go.name, matPath); // if (rimMat) // { // mats.Add(rimMat); // r.sharedMaterials = mats.ToArray(); // } //} } TransformUtil.SwitchLayer(parent.transform, (int)LayerType.Fight); GameObject prefab = null; //if (string.IsNullOrEmpty(suffix)) prefab = CreateOrReplacePrefab(parent, outputFile) as GameObject; Object.DestroyImmediate(parent); Object.DestroyImmediate(go); return(prefab); }