private static void getCond(string condPath, ref Dictionary <string, animCond> conds) { Dictionary <int, List <string> > dict = new Dictionary <int, List <string> >(); getExcelByPath(condPath, ref dict); foreach (var item in dict) { List <string> vals = item.Value; animCond cond = new animCond(); cond.clipName = vals[0]; cond.isDefault = int.Parse(vals[1]) == 1; cond.fromState = vals[2] == "0" ? "Any State" : vals[2]; cond.fromCond = vals[3]; cond.toState = vals[4] == "0" ? defaultAnim : vals[4]; cond.toCond = vals[5] == "0" ? "None" : vals[5]; conds.Add(cond.clipName, cond); } }
private static void createAC(string objPath, string objName, Dictionary <string, animCond> conds) { int index = objPath.LastIndexOf("/"); string savePath = getAnimPathByName(objName, "ac"); string clipPath = getAnimPathByName(objName, "clip"); AnimatorController ac = AnimatorController.CreateAnimatorControllerAtPath(Path.Combine(savePath, objName + ".controller")); AnimatorControllerLayer layer = ac.layers[0]; //添加clip AnimatorStateMachine stateMachine = layer.stateMachine; string[] files = Directory.GetFiles(clipPath, "*anim"); List <Motion> clips = new List <Motion>(); for (int i = 0; i < files.Length; i++) { Motion cp = AssetDatabase.LoadAssetAtPath <Motion>(files[i]); if (cp != null) { clips.Add(cp); } } int startY = 0; bool isLeft = false; int startX = 0; AnimatorState animState = null; for (int i = 0; i < clips.Count; i++) { string name = clips[i].name; bool isStand = name == defaultAnim; startY = isStand ? startY : startY + 1; if (!isStand) { startX = 430;// isLeft ? 430 + 100 : 430 - 100; isLeft = !isLeft; } animState = stateMachine.AddState(name, isStand ? new Vector2(600, 0) : new Vector2(startX, startY * 100)); animState.motion = clips[i]; if (isStand) { stateMachine.defaultState = animState; } } //连线 ChildAnimatorState[] states = stateMachine.states; for (int i = 0; i < states.Length; i++) { ChildAnimatorState currState = states[i]; string name = currState.state.name; if (conds.ContainsKey(name)) { animCond cond = conds[name]; string fromStateName = cond.fromState; if (fromStateName == "Any State") { AnimatorStateTransition trans = stateMachine.AddAnyStateTransition(currState.state); if (!isExitParam(ac, cond.fromCond)) { ac.AddParameter(cond.fromCond, AnimatorControllerParameterType.Trigger); } trans.AddCondition(AnimatorConditionMode.If, 0, cond.fromCond); } string toStateName = cond.toState; ChildAnimatorState toState = getState(states, toStateName); if (toState.state != null && name != defaultAnim) { AnimatorStateTransition trans = currState.state.AddTransition(toState.state); if (cond.toCond == "None") { trans.hasExitTime = false; } else if (cond.toCond == "Exit") { trans.hasExitTime = true; } else { if (!isExitParam(ac, cond.toCond)) { ac.AddParameter(cond.toCond, AnimatorControllerParameterType.Trigger); } trans.AddCondition(AnimatorConditionMode.If, 0, cond.toCond); } } } } ac.name = objName; //AssetDatabase.CreateAsset(ac, Path.Combine(savePath, objName + ".controller")); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); }
private static void createAC(string objPath, string objName, Dictionary <string, animCond> conds) { int index = objPath.LastIndexOf("/"); string savePath = objPath.Remove(index); string clipPath = Path.Combine(savePath, "bin/clip"); savePath = Path.Combine(savePath, "bin/ac"); if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } AnimatorController ac = new AnimatorController(); AnimatorControllerLayer layer = new AnimatorControllerLayer(); layer.name = "Base"; //添加clip AnimatorStateMachine stateMachine = new AnimatorStateMachine(); string[] files = Directory.GetFiles(clipPath, "*anim"); List <AnimationClip> clips = new List <AnimationClip>(); for (int i = 0; i < files.Length; i++) { AnimationClip cp = AssetDatabase.LoadAssetAtPath <AnimationClip>(files[i]); if (cp != null) { clips.Add(cp); } } int startY = 0; bool isLeft = false; int startX = 0; for (int i = 0; i < clips.Count; i++) { AnimatorState animState = new AnimatorState(); animState.motion = clips[i] as AnimationClip; string name = clips[i].name; animState.name = name; bool isStand = name == defaultAnim; startY = isStand ? startY : startY + 1; if (!isStand) { startX = 430;// isLeft ? 430 + 100 : 430 - 100; isLeft = !isLeft; } stateMachine.AddState(animState, isStand ? new Vector2(430, 0) : new Vector2(startX, startY * 100)); if (isStand) { stateMachine.defaultState = animState; } } //连线 ChildAnimatorState[] states = stateMachine.states; for (int i = 0; i < states.Length; i++) { ChildAnimatorState currState = states[i]; string name = currState.state.name; if (conds.ContainsKey(name)) { animCond cond = conds[name]; string fromStateName = cond.fromState; if (fromStateName == "Any State") { AnimatorStateTransition trans = stateMachine.AddAnyStateTransition(currState.state); if (!isExitParam(ac, cond.fromCond)) { ac.AddParameter(cond.fromCond, AnimatorControllerParameterType.Trigger); } trans.AddCondition(AnimatorConditionMode.Equals, 0, cond.fromCond); } string toStateName = cond.toState; ChildAnimatorState toState = getState(states, toStateName); if (toState.state != null && name != defaultAnim) { AnimatorStateTransition trans = currState.state.AddTransition(toState.state); if (cond.toCond == "None") { trans.hasExitTime = true; } else { if (!isExitParam(ac, cond.toCond)) { ac.AddParameter(cond.toCond, AnimatorControllerParameterType.Trigger); } trans.AddCondition(AnimatorConditionMode.Equals, 0, cond.toCond); } } } } layer.stateMachine = stateMachine; ac.AddLayer(layer); ac.name = objName; AssetDatabase.CreateAsset(ac, Path.Combine(savePath, objName + ".controller")); }
private static void createAC(string objPath, string objName, Dictionary <string, animCond> conds, List <animClip> lstInfo) { //查找混合树 List <animClip> blendInfo = new List <animClip>(); for (int i = 0; i < lstInfo.Count; i++) { if (lstInfo[i].blendIndex > 0) { blendInfo.Add(lstInfo[i]); } } int index = objPath.LastIndexOf("/"); string savePath = getAnimPathByName(objName, "ac"); string clipPath = getAnimPathByName(objName, "clip"); AnimatorController ac = AnimatorController.CreateAnimatorControllerAtPath(Path.Combine(savePath, objName + ".controller")); AnimatorControllerLayer layer = ac.layers[0]; //添加clip AnimatorStateMachine stateMachine = layer.stateMachine; string[] files = Directory.GetFiles(clipPath, "*anim"); Dictionary <string, Motion> dictClips = new Dictionary <string, Motion>(); for (int i = 0; i < files.Length; i++) { Motion cp = AssetDatabase.LoadAssetAtPath <Motion>(files[i]); if (cp != null) { dictClips.Add(cp.name, cp); } } AnimatorState animState = null; //先创建blendTree if (blendInfo.Count > 0) { if (!isExitParam(ac, "tree")) { ac.AddParameter("tree", AnimatorControllerParameterType.Float); } BlendTree btree = null; animState = ac.CreateBlendTreeInController(defaultAnim, out btree); btree.blendParameter = "tree"; for (int i = 0; i < blendInfo.Count; i++) { string cname = blendInfo[i].clipName; if (dictClips.ContainsKey(cname)) { btree.AddChild(dictClips[cname]); } } stateMachine.AddState(animState, new Vector2(600, -200)); stateMachine.defaultState = animState; } int startY = 0; int startX = 300; for (int i = 0; i < lstInfo.Count; i++) { if (lstInfo[i].blendIndex > 0) { continue; } string name = lstInfo[i].clipName; animState = stateMachine.AddState(name, new Vector2(startX, startY * 80)); animState.motion = dictClips[name]; startY++; } //连线 ChildAnimatorState[] states = stateMachine.states; for (int i = 0; i < states.Length; i++) { ChildAnimatorState currState = states[i]; string name = currState.state.name; if (conds.ContainsKey(name)) { animCond cond = conds[name]; string fromStateName = cond.fromState; if (fromStateName == "Any State") { AnimatorStateTransition trans = stateMachine.AddAnyStateTransition(currState.state); if (!isExitParam(ac, cond.fromCond)) { ac.AddParameter(cond.fromCond, AnimatorControllerParameterType.Trigger); } trans.AddCondition(AnimatorConditionMode.If, 0, cond.fromCond); } string toStateName = cond.toState; ChildAnimatorState toState = getState(states, toStateName); if (toState.state != null && name != defaultAnim) { AnimatorStateTransition trans = currState.state.AddTransition(toState.state); if (cond.toCond == "None") { trans.hasExitTime = false; } else if (cond.toCond == "Exit") { trans.hasExitTime = true; } else { if (!isExitParam(ac, cond.toCond)) { ac.AddParameter(cond.toCond, AnimatorControllerParameterType.Trigger); } trans.AddCondition(AnimatorConditionMode.If, 0, cond.toCond); } } } } ac.name = objName; //AssetDatabase.CreateAsset(ac, Path.Combine(savePath, objName + ".controller")); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); }