private void CreateStateMachine() { if ((this.m_AvatarPreview != null) && (this.m_AvatarPreview.Animator != null)) { if (this.m_Controller == null) { this.m_Controller = new AnimatorController(); this.m_Controller.pushUndo = false; this.m_Controller.AddLayer("preview"); this.m_StateMachine = this.m_Controller.layers[0].stateMachine; this.m_StateMachine.pushUndo = false; this.CreateParameters(); this.m_State = this.m_StateMachine.AddState("preview"); this.m_State.pushUndo = false; this.m_State.motion = this.m_BlendTree; this.m_State.iKOnFeet = this.m_AvatarPreview.IKOnFeet; this.m_State.hideFlags = HideFlags.HideAndDontSave; this.m_Controller.hideFlags = HideFlags.HideAndDontSave; this.m_StateMachine.hideFlags = HideFlags.HideAndDontSave; AnimatorController.SetAnimatorController(this.m_AvatarPreview.Animator, this.m_Controller); this.m_Controller.OnAnimatorControllerDirty = (Action) Delegate.Combine(this.m_Controller.OnAnimatorControllerDirty, new Action(this, (IntPtr) this.ControllerDirty)); this.m_ControllerIsDirty = false; } if (AnimatorController.GetEffectiveAnimatorController(this.m_AvatarPreview.Animator) != this.m_Controller) { AnimatorController.SetAnimatorController(this.m_AvatarPreview.Animator, this.m_Controller); } } }
private void CreateStateMachine() { if (this.m_AvatarPreview == null || !((UnityEngine.Object) this.m_AvatarPreview.Animator != (UnityEngine.Object) null)) return; if ((UnityEngine.Object) this.m_Controller == (UnityEngine.Object) null) { this.m_Controller = new AnimatorController(); this.m_Controller.pushUndo = false; this.m_Controller.AddLayer("preview"); this.m_StateMachine = this.m_Controller.layers[0].stateMachine; this.m_StateMachine.pushUndo = false; this.CreateParameters(); this.m_State = this.m_StateMachine.AddState("preview"); this.m_State.pushUndo = false; this.m_State.motion = (Motion) this.m_BlendTree; this.m_State.iKOnFeet = this.m_AvatarPreview.IKOnFeet; this.m_State.hideFlags = HideFlags.HideAndDontSave; this.m_Controller.hideFlags = HideFlags.HideAndDontSave; this.m_StateMachine.hideFlags = HideFlags.HideAndDontSave; AnimatorController.SetAnimatorController(this.m_AvatarPreview.Animator, this.m_Controller); this.m_Controller.OnAnimatorControllerDirty += new System.Action(this.ControllerDirty); this.m_ControllerIsDirty = false; } if (!((UnityEngine.Object) AnimatorController.GetEffectiveAnimatorController(this.m_AvatarPreview.Animator) != (UnityEngine.Object) this.m_Controller)) return; AnimatorController.SetAnimatorController(this.m_AvatarPreview.Animator, this.m_Controller); }
private void BuildDestinationStatesRecursive(AnimatorTransitionBase transition, AnimatorStateMachine rootStateMachine, AnimatorStateMachine currentStateMachine) { if (transition.destinationState != null) { this.m_DstStates.Add(transition.destinationState); } else if (transition.isExit) { AnimatorStateMachine machine = rootStateMachine.FindParent(currentStateMachine); if (machine != null) { AnimatorTransition[] stateMachineTransitions = machine.GetStateMachineTransitions(currentStateMachine); foreach (AnimatorTransition transition2 in stateMachineTransitions) { this.BuildDestinationStatesRecursive(transition2, rootStateMachine, machine); } } } else if ((transition.destinationStateMachine != null) && !this.m_DstStateMachines.Contains(transition.destinationStateMachine)) { this.m_DstStateMachines.Add(transition.destinationStateMachine); if (transition.destinationStateMachine.defaultState != null) { this.m_DstStates.Add(transition.destinationStateMachine.defaultState); } AnimatorTransition[] entryTransitions = transition.destinationStateMachine.entryTransitions; foreach (AnimatorTransition transition3 in entryTransitions) { this.BuildDestinationStatesRecursive(transition3, rootStateMachine, transition.destinationStateMachine); } } }
public TransitionEditionContext(AnimatorTransitionBase aTransition, AnimatorState aSourceState, AnimatorStateMachine aSourceStateMachine, AnimatorStateMachine aOwnerStateMachine) { this.transition = aTransition; this.sourceState = aSourceState; this.sourceStateMachine = aSourceStateMachine; this.ownerStateMachine = aOwnerStateMachine; this.BuildNames(); }
private void ClearStateMachine() { if (this.m_Animator != null) { AnimatorController.SetAnimatorController(this.m_Animator, null); } UnityEngine.Object.DestroyImmediate(this.m_Controller); UnityEngine.Object.DestroyImmediate(this.m_State); this.m_StateMachine = null; this.m_Controller = null; this.m_State = null; }
public void OnEnable() { this.m_Name = base.serializedObject.FindProperty("m_Name"); this.m_RootStateMachine = ((AnimatorControllerTool.tool == null) || (AnimatorControllerTool.tool.animatorController == null)) ? null : AnimatorControllerTool.tool.animatorController.layers[AnimatorControllerTool.tool.selectedLayerIndex].stateMachine; this.m_ActiveStateMachine = ((AnimatorControllerTool.tool == null) || (AnimatorControllerTool.tool.stateMachineGraph == null)) ? null : AnimatorControllerTool.tool.stateMachineGraph.activeStateMachine; this.Init(); if (this.m_TransitionEditor != null) { this.m_TransitionEditor.OnEnable(); } this.m_BehavioursEditor.OnEnable(); }
private void AddClipsToState(AnimatorStateMachine stateMachine, List<AnimationClip> animationClips, string prefix = "") { foreach (var clip in animationClips) { var state = stateMachine.AddState(clip.name); state.motion = clip; var transition = stateMachine.AddEntryTransition(state); transition.AddCondition(AnimatorConditionMode.If, 1f, prefix + clip.name); var exitTransition = state.AddExitTransition(); exitTransition.hasExitTime = true; exitTransition.duration = 0.1f; } }
public static bool StateMachineRelativePath(AnimatorStateMachine parent, AnimatorStateMachine toFind, ref List<AnimatorStateMachine> hierarchy) { hierarchy.Add(parent); if ((Object) parent == (Object) toFind) return true; for (int index = 0; index < parent.stateMachines.Length; ++index) { if (MecanimUtilities.StateMachineRelativePath(parent.stateMachines[index].stateMachine, toFind, ref hierarchy)) return true; } hierarchy.Remove(parent); return false; }
void Reset() { displayEvents = null; targetController = null; targetStateMachine = null; targetState = null; targetEvent = null; selectedLayer = 0; selectedState = 0; selectedEvent = 0; MecanimEventEditorPopup.Destroy(); }
private void ClearStateMachine() { if (this.m_Animator != null) { AnimatorController.SetAnimatorController(this.m_Animator, null); } if (this.m_Controller != null) { this.m_Controller.OnAnimatorControllerDirty = (System.Action) Delegate.Remove(this.m_Controller.OnAnimatorControllerDirty, new System.Action(this.ControllerDirty)); } UnityEngine.Object.DestroyImmediate(this.m_Controller); UnityEngine.Object.DestroyImmediate(this.m_State); this.m_StateMachine = null; this.m_Controller = null; this.m_State = null; }
private void ClearStateMachine() { if ((this.m_AvatarPreview != null) && (this.m_AvatarPreview.Animator != null)) { AnimatorController.SetAnimatorController(this.m_AvatarPreview.Animator, null); } if (this.m_Controller != null) { this.m_Controller.OnAnimatorControllerDirty = (Action) Delegate.Remove(this.m_Controller.OnAnimatorControllerDirty, new Action(this, (IntPtr) this.ControllerDirty)); } Object.DestroyImmediate(this.m_Controller); Object.DestroyImmediate(this.m_State); this.m_StateMachine = null; this.m_Controller = null; this.m_State = null; }
private void ClearController() { if ((this.m_AvatarPreview != null) && (this.m_AvatarPreview.Animator != null)) { AnimatorController.SetAnimatorController(this.m_AvatarPreview.Animator, null); } UnityEngine.Object.DestroyImmediate(this.m_Controller); UnityEngine.Object.DestroyImmediate(this.m_SrcState); UnityEngine.Object.DestroyImmediate(this.m_DstState); UnityEngine.Object.DestroyImmediate(this.m_Transition); this.m_StateMachine = null; this.m_Controller = null; this.m_SrcState = null; this.m_DstState = null; this.m_Transition = null; }
public static bool StateMachineRelativePath(AnimatorStateMachine parent, AnimatorStateMachine toFind, ref List<AnimatorStateMachine> hierarchy) { hierarchy.Add(parent); if (parent == toFind) { return true; } for (int i = 0; i < parent.stateMachines.Length; i++) { if (StateMachineRelativePath(parent.stateMachines[i].stateMachine, toFind, ref hierarchy)) { return true; } } hierarchy.Remove(parent); return false; }
void DrawClip(AnimatorState animaState, AnimationClip motion, AnimatorStateMachine stateMachine) { CreateObjectField(animaState); CreateObjectField(motion); if (null != animaState) { EditorGUI.indentLevel = 0; var transitions = animaState.transitions; BeginHorizontal(); DoButton("SelectAll", () => { Selection.objects = transitions; }); DoButton<AnimatorStateTransition[], string>("ExitTime", BatchChangeProperty, transitions, "ExitTime"); DoButton("FixedDuration", () => { }); DoButton("Trans Duration", () => { }); DoButton("Trans Offset", () => { }); EndHorizontal(); EditorGUI.indentLevel = 2; for (int i = 0; i < transitions.Length; i++) { BeginHorizontal(); CreateObjectField(transitions[i]); CreateLabel(transitions[i].GetDisplayName(stateMachine)); EndHorizontal(); } EditorGUI.indentLevel = 0; } if (null != motion) { EditorGUILayout.BeginHorizontal(); CreateLabel(motion.name); CreateLabel(motion.averageDuration.ToString()); CreateLabel(motion.isLooping.ToString()); EditorGUILayout.EndHorizontal(); } }
static void AddSuMechie(AnimatorStateMachine machine, int index1, string path, AnimatorControllerLayer layer, string sunStateMachine) { ////创建子状态机 //for (int k = 1; k < index1; k++) //{ // AnimatorStateMachine sub2Machine = machine.AddStateMachine("sub2Machine", new Vector3(100, 300, 0)); //} AnimatorStateMachine sub2Machine = machine.AddStateMachine(sunStateMachine, new Vector3(100, 300, 0)); // 根据动画文件读取它的AnimationClip对象 var datas = AssetDatabase.LoadAllAssetsAtPath(path); if (datas.Length == 0) { Debug.Log(string.Format("Can't find clip in {0}", path)); return; } foreach (var data in datas) { int index = 0; if (!(data is AnimationClip)) { continue; } var newClip = data as AnimationClip; if (newClip.name.StartsWith("__")) { continue; } // 取出动画名字,添加到state里面 AnimatorState state = sub2Machine.AddState(newClip.name, new Vector3(500, sub2Machine.states.Length * 60, 0)); stateList.Add(state); if (state.name == "walk") { sub2Machine.defaultState = state; } Debug.Log(string.Format("<color=red>{0}</color>", state)); index++; state.motion = newClip; // 把State添加在Layer里面 sub2Machine.AddAnyStateTransition(state); } }
void DrawClip(AnimatorState animaState, AnimationClip motion, AnimatorStateMachine stateMachine) { CreateObjectField(animaState); CreateObjectField(motion); if (null != animaState) { EditorGUI.indentLevel = 0; var transitions = animaState.transitions; BeginHorizontal(); DoButton("SelectAll", () => { Selection.objects = transitions; }); DoButton <AnimatorStateTransition[], string>("ExitTime", BatchChangeProperty, transitions, "ExitTime"); DoButton("FixedDuration", () => { }); DoButton("Trans Duration", () => { }); DoButton("Trans Offset", () => { }); EndHorizontal(); EditorGUI.indentLevel = 2; for (int i = 0; i < transitions.Length; i++) { BeginHorizontal(); CreateObjectField(transitions[i]); CreateLabel(transitions[i].GetDisplayName(stateMachine)); EndHorizontal(); } EditorGUI.indentLevel = 0; } if (null != motion) { EditorGUILayout.BeginHorizontal(); CreateLabel(motion.name); CreateLabel(motion.averageDuration.ToString()); CreateLabel(motion.isLooping.ToString()); EditorGUILayout.EndHorizontal(); } }
private void LiveLink() { this.m_LiveLinkInfo.Clear(); if (this.tool.liveLink) { AnimatorControllerPlayable controller = AnimatorController.FindAnimatorControllerPlayable(this.tool.previewAnimator, this.tool.animatorController); if (controller.node.IsValid()) { AnimatorStateInfo currentAnimatorStateInfo = controller.GetCurrentAnimatorStateInfo(AnimatorControllerTool.tool.selectedLayerIndex); AnimatorStateInfo nextAnimatorStateInfo = controller.GetNextAnimatorStateInfo(AnimatorControllerTool.tool.selectedLayerIndex); AnimatorTransitionInfo animatorTransitionInfo = controller.GetAnimatorTransitionInfo(AnimatorControllerTool.tool.selectedLayerIndex); int shortNameHash = currentAnimatorStateInfo.shortNameHash; int nameHash = nextAnimatorStateInfo.shortNameHash; this.m_LiveLinkInfo.currentStateMachine = (shortNameHash == 0) ? null : this.rootStateMachine.FindStateMachine(this.ResolveHash(controller, currentAnimatorStateInfo.fullPathHash)); this.m_LiveLinkInfo.currentState = (shortNameHash == 0) ? null : this.m_LiveLinkInfo.currentStateMachine.FindState(shortNameHash).state; this.m_LiveLinkInfo.currentStateNormalizedTime = currentAnimatorStateInfo.normalizedTime; this.m_LiveLinkInfo.currentStateLoopTime = currentAnimatorStateInfo.loop; if (this.m_LiveLinkInfo.currentState != null) { this.m_LiveLinkInfo.nextStateMachine = (nameHash == 0) ? null : this.rootStateMachine.FindStateMachine(this.ResolveHash(controller, nextAnimatorStateInfo.fullPathHash)); this.m_LiveLinkInfo.nextState = (nameHash == 0) ? null : this.m_LiveLinkInfo.nextStateMachine.FindState(nameHash).state; this.m_LiveLinkInfo.nextStateNormalizedTime = nextAnimatorStateInfo.normalizedTime; this.m_LiveLinkInfo.nextStateLoopTime = nextAnimatorStateInfo.loop; this.m_LiveLinkInfo.srcNode = this.stateMachineGraph.FindNode(this.m_LiveLinkInfo.currentState); this.m_LiveLinkInfo.dstNode = (this.m_LiveLinkInfo.nextState == null) ? null : this.stateMachineGraph.FindNode(this.m_LiveLinkInfo.nextState); this.m_LiveLinkInfo.transitionInfo = animatorTransitionInfo; if (this.tool.autoLiveLink) { AnimatorStateMachine currentStateMachine = this.m_LiveLinkInfo.currentStateMachine; if (((this.m_LiveLinkInfo.currentState != null) && (this.m_LiveLinkInfo.nextState != null)) && ((this.m_LiveLinkInfo.transitionInfo.normalizedTime > 0.5) || animatorTransitionInfo.anyState)) { currentStateMachine = this.m_LiveLinkInfo.nextStateMachine; } if (((shortNameHash != 0) && (currentStateMachine != this.activeStateMachine)) && (Event.current.type == EventType.Repaint)) { List <AnimatorStateMachine> hierarchy = new List <AnimatorStateMachine>(); MecanimUtilities.StateMachineRelativePath(this.rootStateMachine, currentStateMachine, ref hierarchy); this.tool.BuildBreadCrumbsFromSMHierarchy(hierarchy); } } } } } }
/// <summary> /// Show how to add a child machine /// </summary> /// <param name="fbxPath"></param> private void CreateCrouchWalk(string fbxPath) { AnimatorStateMachine walkMachine = crouchLayerMachine.AddStateMachine("Walk", new Vector3(0f, 100f)); walkMachine.entryPosition = Vector3.zero; walkMachine.anyStatePosition = new Vector3(0f, -200f); walkMachine.exitPosition = new Vector3(0f, -400f); AnimationClip walkClip = AnimatorFactoryUtil.LoadAnimClip(fbxPath, "HumanoidCrouchWalk"); AnimationClip walkLeftClip = AnimatorFactoryUtil.LoadAnimClip(fbxPath, "HumanoidCrouchWalkLeft"); AnimationClip walkRightClip = AnimatorFactoryUtil.LoadAnimClip(fbxPath, "HumanoidCrouchWalkRight"); stateWalk = walkMachine.AddState("Walk", new Vector3(200f, 0f)); stateWalk.motion = walkClip; stateWalkLeft = walkMachine.AddState("WalkLeft", new Vector3(200f, -200f)); stateWalkLeft.motion = walkLeftClip; stateWalkRight = walkMachine.AddState("WalkRight", new Vector3(200f, -400f)); stateWalkRight.motion = walkRightClip; }
static void AddStates(AnimatorStateMachine _stateMachine, ref string _enumStr, string subStateName = null) { foreach (var subState in _stateMachine.states) { string stateName = subState.state.name; string enumName = stateName.CapitalizeFirstLetter(); if (subStateName != null) { stateName = subStateName + "." + stateName; enumName = subStateName + enumName; } enumName = enumName.Replace(" ", ""); _enumStr += "\t" + enumName + " = " + Animator.StringToHash(stateName) + ",\n"; } foreach (var sm in _stateMachine.stateMachines) { AddStates(sm.stateMachine, ref _enumStr, sm.stateMachine.name); } }
public static void GenericMenuForStateMachineNode(StateMachineNode toStateMachineNode, bool showStateMachine, GenericMenu.MenuFunction2 func) { AnimatorStateMachine y = null; List <ChildAnimatorState> list = new List <ChildAnimatorState>(); List <ChildAnimatorStateMachine> list2 = new List <ChildAnimatorStateMachine>(); Node.PopulateSubNodeList(toStateMachineNode, ref list, ref list2, ref y); if (list.Count == 0 && list2.Count == 1) { func(list2[0].stateMachine); return; } GenericMenu genericMenu = new GenericMenu(); foreach (ChildAnimatorState current in list) { string text = current.state.name; AnimatorStateMachine currentParent = current.state.FindParent(AnimatorControllerTool.tool.stateMachineGraph.rootStateMachine); while (currentParent != null && currentParent != y) { text = text.Insert(0, currentParent.name + "/"); currentParent = list2.Find((ChildAnimatorStateMachine sm) => sm.stateMachine.IsDirectParent(currentParent)).stateMachine; } if (list.Count > 0 && list2.Count > 0 && showStateMachine) { text = text.Insert(0, "States/"); } genericMenu.AddItem(new GUIContent(text), false, func, current.state); } if (showStateMachine) { foreach (ChildAnimatorStateMachine current2 in list2) { string text2 = current2.stateMachine.name; if (list.Count > 0 && list2.Count > 0) { text2 = text2.Insert(0, "StateMachine/"); } genericMenu.AddItem(new GUIContent(text2), false, func, current2.stateMachine); } } genericMenu.ShowAsContext(); }
private void PersistStateMachine(AnimatorController controller, AnimatorStateMachine stateMachine) { foreach (ChildAnimatorState cState in stateMachine.states) { AssetDatabase.AddObjectToAsset(cState.state, controller); foreach (AnimatorStateTransition transition in cState.state.transitions) { AssetDatabase.AddObjectToAsset(transition, controller); } } foreach (AnimatorStateTransition transition in stateMachine.anyStateTransitions) { AssetDatabase.AddObjectToAsset(transition, controller); } foreach (AnimatorTransition transition in stateMachine.entryTransitions) { AssetDatabase.AddObjectToAsset(transition, controller); } }
private static AnimatorController BuildAnimatorController(List <AnimationClip> list, string name) { AnimatorController animatorController = AnimatorController.CreateAnimatorControllerAtPath(animatorPath + "/" + name + ".controller"); AnimatorControllerLayer layer = animatorController.layers[0]; AnimatorStateMachine state = layer.stateMachine; foreach (var clip in list) { AnimatorState st = state.AddState(clip.name); st.motion = clip; if (clip.name == "Idle") { state.defaultState = st; } } AssetDatabase.SaveAssets(); return(animatorController); }
private AnimatorState GetStateFromMachine(AnimatorStateMachine machine, string clipName) { foreach (var state in machine.states) { if (state.state.name == clipName) { return(state.state); } } foreach (var cmachine in machine.stateMachines) { var state = GetStateFromMachine(cmachine.stateMachine, clipName); if (state != null) { return(state); } } return(null); }
private void ClearStateMachine() { if (avatarPreview != null && avatarPreview.Animator != null) { AnimatorController.SetAnimatorController(avatarPreview.Animator, null); } if (controller != null) { controller.RemoveOnAnimatorControllerDirtyCallback(ControllerDitry); } DestroyImmediate(controller); //Object.DestroyImmediate(this.stateMachine); DestroyImmediate(state); stateMachine = null; controller = null; state = null; }
private static void GetAnimationClipsFromStatemachine(int layer, AnimatorStateMachine stateMachine, ref List <AnimationClip> clips, ref List <string> stateNames, ref List <int> stateLayers) { foreach (ChildAnimatorState state in stateMachine.states) { AnimationClip clip = state.state.motion as AnimationClip; if (clip != null && !clips.Contains(clip)) { clips.Add(clip); stateNames.Add(state.state.name); stateLayers.Add(layer); } } foreach (ChildAnimatorStateMachine childStateMachine in stateMachine.stateMachines) { GetAnimationClipsFromStatemachine(layer, childStateMachine.stateMachine, ref clips, ref stateNames, ref stateLayers); } }
/// <summary> /// 生成控制器animator /// </summary> static AnimatorController Generate2DAnimator(List <AnimationClip> clips, DirectoryInfo directoryInfo) { string controllerFile = s_toAnimatorPath + "/" + directoryInfo.Name + ".controller"; Debug.LogWarningFormat("save controller to path={0}.", controllerFile); var animatorController = AnimatorController.CreateAnimatorControllerAtPath(controllerFile); AnimatorStateMachine stateMachine = animatorController.layers[0].stateMachine; foreach (var clip in clips) { AnimatorState animatorState = stateMachine.AddState(clip.name); animatorState.motion = clip; var stateTransistion = stateMachine.AddAnyStateTransition(animatorState); stateTransistion.hasExitTime = true; } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); return(animatorController); }
void PlayAnimation() { AnimatorController controller = actorController.gameObject.GetComponentInChildren <Animator>().runtimeAnimatorController as AnimatorController; AnimatorStateMachine asm = controller.layers[0].stateMachine; foreach (ChildAnimatorState cas in asm.states) { if (cas.state.name == BehaviorEditorWindow.win.selectedBehavior.name) { if (cas.state.motion != null) { currentAnimDuration = cas.state.motion.averageDuration; // Debug.Log(currentAnimDuration); model.anim.Play(BehaviorEditorWindow.win.selectedBehavior.name, 0, normalizedTime); break; } } } }
private void UpdateAnimatorController() { // The new controller that will be created based on Manager animations AnimatorController newController = new AnimatorController(); newController.AddLayer("DefaultLayer"); // Add a parameter that will determine the animation states AnimatorControllerParameter animatorParameter = new AnimatorControllerParameter(); animatorParameter.type = AnimatorControllerParameterType.Int; animatorParameter.name = "TextAnimation"; animatorParameter.defaultInt = 999; newController.AddParameter(animatorParameter); // Add state machine AnimatorStateMachine rootStateMachine = newController.layers[0].stateMachine; AnimatorStateMachine stateMachine = rootStateMachine.AddStateMachine("TextAnimationStateMachine"); // Create a default state to prevent animation auto playing index 0 AnimatorState waitingState = stateMachine.AddState("Waiting"); //foreach (AnimationClip clip in DamageNumberManager.instance.animations) for (int i = 0; i < DamageNumberManager.instance.animations.Length; i++) { AnimationClip clip = DamageNumberManager.instance.animations[i]; // Add new state based on the AnimationClip AnimatorState state = stateMachine.AddState(clip.name); state.motion = clip; // Create transition from "Waiting" to the new state AnimatorStateTransition transition = waitingState.AddTransition(state, false); transition.AddCondition(AnimatorConditionMode.Equals, i, "TextAnimation"); } // Override the existing Animator Controller AnimatorOverrideController overrideController = new AnimatorOverrideController(); overrideController.runtimeAnimatorController = newController; GetComponent <Animator>().runtimeAnimatorController = overrideController; }
public static AnimatorState FindState(AnimatorStateMachine machine, string name) { foreach (var st in machine.states) { if (st.state.name == name) { return(st.state); } } foreach (var m in machine.stateMachines) { var st = FindState(m.stateMachine, name); if (st != null) { return(st); } } return(null); }
public void CollectStates(AnimatorController ac, int layerIndex) { mStates = new List <int>(); mStateNames = new List <string>(); mStateIndexLookup = new Dictionary <int, int>(); mStateParentLookup = new Dictionary <int, int>(); mStateIndexLookup[0] = mStates.Count; mStateNames.Add("(default)"); mStates.Add(0); if (ac != null && layerIndex >= 0 && layerIndex < ac.layers.Length) { AnimatorStateMachine fsm = ac.layers[layerIndex].stateMachine; string name = fsm.name; int hash = Animator.StringToHash(name); CollectStatesFromFSM(fsm, name + ".", hash, string.Empty); } }
//Cette fonction fait en sorte que toutes les transitions de l'animator, y compris celles crées à la main (sur la State Machine de base, mais pourrait être étendu sans difficulté) //soient automatiquement fixées sans Exit Time et avec une durée de transition égale à 0, ce qui est optimal pour une StateMachine. void CorrectAllTransitions(AnimatorStateMachine sM) { AnimatorState[] stateArray = new AnimatorState[sM.states.Length]; List <AnimatorStateTransition> transitionList = new List <AnimatorStateTransition>(); for (int i = 0; i < sM.states.Length; i++) { stateArray[i] = sM.states[i].state; } for (int i = 0; i < stateArray.Length; i++) { transitionList.AddRange(stateArray[i].transitions); } for (int i = 0; i < transitionList.Count; i++) { transitionList[i].duration = 0f; transitionList[i].hasExitTime = false; } }
protected static void HandleStateMachine(string modelName, AnimatorStateMachine stateMachine, AnimatorGenerateConfig config) { if (stateMachine.states == null || stateMachine.states.Length == 0) { return; } AnimatorState state = null; Motion motion = null; for (int i = 0; i < stateMachine.states.Length; ++i) { state = stateMachine.states[i].state; var index = config.stateClips.FindIndex((AnimatorGenerateConfig.StateToClip stateToClip) => { return(stateToClip.state == state.name); }); if (index == -1) { Debug.LogWarningFormat("没有找到状态机上名称为: {0}在{1}里的对应配置. ", state.name, config.name); continue; } motion = state.motion; if (motion is BlendTree) { } else { var clipName = config.stateClips[index].clip; state.motion = GetAnimationClip(modelName, clipName); if (state.motion == null) { Debug.LogWarningFormat("为{0}状态设置动画失败!!!", state.name); } } } foreach (var childStateMachine in stateMachine.stateMachines) { HandleStateMachine(modelName, childStateMachine.stateMachine, config); } }
public static void CreateAnimatorcontroller(string modelName) { string path = animControllerFolder + modelName + ".controller"; if (File.Exists(path)) { File.Delete(path); } AnimatorController.CreateAnimatorControllerAtPath(path); AnimatorController ac = AssetDatabase.LoadAssetAtPath <AnimatorController>(path); AnimatorStateMachine machine = ac.layers[0].stateMachine; string modelPath = modelFolder + modelName + ".FBX"; GameObject model = AssetDatabase.LoadAssetAtPath <GameObject>(modelPath); if (model.GetComponent <Animator>() == null) { ConfigModel(modelName); } Object[] clips = AssetDatabase.LoadAllAssetsAtPath(modelPath); int clipNum = 0; foreach (var v in clips) { if (v.GetType() == typeof(AnimationClip) && v.name != "__preview__Take 001") { AnimatorState state = ac.AddMotion(v as Motion, 0); clipNum++; if (v.name == "swim") { machine.defaultState = state; } } } if (clipNum == 0) { Debug.LogError(modelName + "没有动画clip!!"); } Debug.Log("create animatorController:" + path); }
public void Build() { var expressionActiveState = _controller.ContentFrame.Q <Toggle>("expression-active-state"); var expressionObject = _controller.ContentFrame.Q <ObjectField>("expression-object"); var expName = _expressionInfo.ExpressionName; var controller = _expressionInfo.Controller; AnimatorControllerLayer layer = AnimUtility.AddLayer(controller, expName, _dirtyAssets); controller.AddParameter(expName, AnimatorControllerParameterType.Bool); AnimatorStateMachine stateMachine = layer.stateMachine; var empty = AnimUtility.AddState(stateMachine, "Empty", true, _dirtyAssets); AnimatorState toggleState = AnimUtility.AddState(stateMachine, expName, false, _dirtyAssets); var animationClip = AnimUtility.CreateAnimation(_expressionInfo.AnimationsFolder.GetPath(), expName, _dirtyAssets); AnimUtility.SetObjectReferenceKeyframe(animationClip, _renderer, $"m_Materials.Array.data[{_materialSlot}]", _material, _dirtyAssets); _dirtyAssets.Add(animationClip); toggleState.motion = animationClip; AnimatorStateTransition anyStateTransition = stateMachine.AddAnyStateTransition(toggleState); anyStateTransition.AddCondition(AnimatorConditionMode.If, 1, expName); AnimatorStateTransition exitTransition = toggleState.AddExitTransition(false); exitTransition.AddCondition(AnimatorConditionMode.IfNot, 0, expName); AnimUtility.AddVRCExpressionsParameter(_expressionInfo.AvatarDescriptor, VRCExpressionParameters.ValueType.Bool, expName, _dirtyAssets); if (_expressionInfo.Menu != null) { AnimUtility.AddVRCExpressionsMenuControl(_expressionInfo.Menu, ControlType.Toggle, expName, _dirtyAssets); } _dirtyAssets.SetDirty(); controller.AddObjectsToAsset(stateMachine, toggleState, anyStateTransition, exitTransition, empty); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); }
static AnimatorController BuildAnimationController(List <AnimationClip> clips, string filePath) { AnimatorController animatorController = AnimatorController.CreateAnimatorControllerAtPath(filePath + ".controller"); AnimatorControllerLayer layer = animatorController.layers[0]; AnimatorStateMachine sm = layer.stateMachine; foreach (AnimationClip newClip in clips) { AnimatorState state = sm.AddState(newClip.name); state.motion = newClip; } AssetDatabase.SaveAssets(); return(animatorController); }
private static bool FindParentStateMachine(AnimatorStateMachine stateMachine, AnimatorState targetState, LinkedList <AnimatorStateMachine> stack) { foreach (var childState in stateMachine.states) { if (childState.state == targetState) { stack.AddLast(stateMachine); return(true); } } foreach (var childST in stateMachine.stateMachines) { if (FindParentStateMachine(childST.stateMachine, targetState, stack)) { return(true); } } return(false); }
private void GetAnimState(AnimatorStateMachine aSM) { foreach (var s in aSM.states) { if (s.state.motion == null) { continue; } var clip = GetClip(s.state.motion.name); if (clip != null) { stateNames.Add(s.state); } } foreach (var sms in aSM.stateMachines) { GetAnimState(sms.stateMachine); } }
void TraverseStatemachineToReplaceClips(AnimatorStateMachine stateMachine) { foreach (ChildAnimatorState childState in stateMachine.states) { var clip = childState.state.motion as AnimationClip; if (clip != null) { childState.state.motion = GetCorrespondingClip(clip); continue; } // not a clip, must be a blend tree ReplaceAnimationClips(childState.state.motion as BlendTree); } foreach (ChildAnimatorStateMachine childStateMachine in stateMachine.stateMachines) { TraverseStatemachineToReplaceClips(childStateMachine.stateMachine); } }
static void PopulateStateTable(Dictionary <string, AnimatorState> table, AnimatorStateMachine machine) { foreach (var state in machine.states) { var name = state.state.name; if (table.ContainsKey(name)) { Debug.LogWarning("Duplicate state with name " + name + " in animator controller. Behaviour is undefined."); } else { table.Add(name, state.state); } } foreach (var subMachine in machine.stateMachines) { PopulateStateTable(table, subMachine.stateMachine); } }
private bool FindStateRecursively(AnimatorStateMachine baseSm, AnimatorStateMachine stateMachine, int nameHash) { foreach (var childState in stateMachine.states) { if (childState.state.GetFullPathHash(baseSm) == nameHash) { return(true); } } foreach (var childStateMachine in stateMachine.stateMachines) { if (FindStateRecursively(baseSm, childStateMachine.stateMachine, nameHash)) { return(true); } } return(false); }
public MecanimExplorer(AnimatorController animator, ushort layer) { mStates = new List <int>(); mStateNames = new List <string>(); mStateIndexLookup = new Dictionary <int, int>(); mStateParentLookup = new Dictionary <int, int>(); mStateIndexLookup[0] = mStates.Count; mStateNames.Add("(default)"); mStates.Add(0); if (animator != null && layer < animator.layers.Length) { AnimatorStateMachine fsm = animator.layers[layer].stateMachine; string name = fsm.name; int hash = Animator.StringToHash(name); CollectStatesFromFSM(fsm, name + ".", hash, string.Empty); } }
static AnimatorController BuildAnimationController(List <AnimationClip> clips) { if (!Directory.Exists(AnimationControllerPath)) { Directory.CreateDirectory(AnimationControllerPath); } string path = AnimationControllerPath + "/" + "root.controller"; AnimatorController animatorController = AnimatorController.CreateAnimatorControllerAtPath(path); AnimatorControllerLayer layer = animatorController.layers[0]; AnimatorStateMachine rootStateMachine = layer.stateMachine; foreach (AnimationClip newClip in clips) { AnimatorState state = rootStateMachine.AddState(newClip.name); state.motion = newClip; } AssetDatabase.SaveAssets(); return(animatorController); }
void SupportEventDispatch() { AnimatorController animatorController = target as AnimatorController; AnimatorControllerLayer[] animatorControllerLayers = animatorController.layers; AnimatorStateMachine animatorStateMachine = animatorControllerLayers[0].stateMachine; foreach (var state in animatorStateMachine.states) { if (state.state != null) { StateEventTransmitBehaviour stateEventTransmitBehaviour = state.state.AddStateBehaviour <StateEventTransmitBehaviour>(); stateEventTransmitBehaviour.StateNawme = state.state.name; } } EditorUtility.SetDirty(animatorController); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); }
public void Create() { AnimatorControllerLayer layer = new AnimatorControllerLayer(); AnimatorStateMachine stateMachine = new AnimatorStateMachine(); AnimatorState state = new AnimatorState(); stateMachine.AddEntryTransition(state); stateMachine.name = LayerName; stateMachine.hideFlags = HideFlags.HideInHierarchy; layer.stateMachine = stateMachine; AssetDatabase.AddObjectToAsset(layer.stateMachine, controller); layer.name = LayerName; Initialize(layer); controller.AddLayer(layer); }
private void CreateStateMachine() { if (!((UnityEngine.Object) this.m_Controller == (UnityEngine.Object) null)) return; this.m_Controller = new AnimatorController(); this.m_Controller.pushUndo = false; this.m_Controller.AddLayer("viz"); this.m_StateMachine = this.m_Controller.layers[0].stateMachine; this.m_StateMachine.pushUndo = false; this.CreateParameters(); this.m_State = this.m_StateMachine.AddState("viz"); this.m_State.pushUndo = false; this.m_State.motion = (Motion) this.m_BlendTree; this.m_State.iKOnFeet = false; this.m_State.hideFlags = HideFlags.HideAndDontSave; this.m_StateMachine.hideFlags = HideFlags.HideAndDontSave; this.m_Controller.hideFlags = HideFlags.HideAndDontSave; AnimatorController.SetAnimatorController(this.m_Animator, this.m_Controller); this.m_Controller.OnAnimatorControllerDirty += new System.Action(this.ControllerDirty); this.m_ControllerIsDirty = false; }
private void CreateStateMachine() { if ((this.m_Animator != null) && (this.m_Controller == null)) { this.m_Controller = new AnimatorController(); this.m_Controller.pushUndo = false; this.m_Controller.AddLayer("node"); this.m_StateMachine = this.m_Controller.layers[0].stateMachine; this.m_StateMachine.pushUndo = false; this.CreateParameters(); this.m_State = this.m_StateMachine.AddState("node", new Vector3()); this.m_State.pushUndo = false; this.m_State.motion = this.motion; this.m_State.hideFlags = HideFlags.DontSave; this.m_Controller.hideFlags = HideFlags.DontSave; this.m_StateMachine.hideFlags = HideFlags.DontSave; AnimatorController.SetAnimatorController(this.m_Animator, this.m_Controller); this.m_Animator.Update(0f); this.m_Controller.OnAnimatorControllerDirty = (Action) Delegate.Combine(this.m_Controller.OnAnimatorControllerDirty, new Action(this, (IntPtr) this.ControllerDirty)); this.m_ControllerIsDirty = false; } }
private void CreateStateMachine() { if (this.m_Controller == null) { this.m_Controller = new AnimatorController(); this.m_Controller.pushUndo = false; this.m_Controller.AddLayer("viz"); this.m_StateMachine = this.m_Controller.layers[0].stateMachine; this.m_StateMachine.pushUndo = false; this.CreateParameters(); this.m_State = this.m_StateMachine.AddState("viz"); this.m_State.pushUndo = false; this.m_State.motion = this.m_BlendTree; this.m_State.iKOnFeet = false; this.m_State.hideFlags = HideFlags.HideAndDontSave; this.m_StateMachine.hideFlags = HideFlags.HideAndDontSave; this.m_Controller.hideFlags = HideFlags.HideAndDontSave; AnimatorController.SetAnimatorController(this.m_Animator, this.m_Controller); this.m_Controller.OnAnimatorControllerDirty = (Action) Delegate.Combine(this.m_Controller.OnAnimatorControllerDirty, new Action(this, (IntPtr) this.ControllerDirty)); this.m_ControllerIsDirty = false; } }
public static int GetFullPathHash(this AnimatorState state, AnimatorStateMachine parentSM) { List<string> pathElems = new List<string>(); if (GetFullPathRecursively(parentSM, state, pathElems)) { string fullPath = string.Join(".", pathElems.ToArray()); return Animator.StringToHash(fullPath); } else { Debug.LogError("Do not find state path"); return -1; } }
private AnimatorState GetStateFromMachine (AnimatorStateMachine machine, string clipName) { foreach (var state in machine.states) { if (state.state.name == clipName) return state.state; } foreach (var cmachine in machine.stateMachines) { var state = GetStateFromMachine (cmachine.stateMachine, clipName); if (state!= null) return state; } return null; }
private static bool GetFullPathRecursively(AnimatorStateMachine parentSM, AnimatorState state, List<string> pathElems) { for (int i = 0 ;i < parentSM.states.Length; i++) { if (parentSM.states[i].state == state) { pathElems.Add(parentSM.name); pathElems.Add(state.name); return true; } } for (int i = 0; i < parentSM.stateMachines.Length; i++) { if (GetFullPathRecursively(parentSM.stateMachines[i].stateMachine, state, pathElems)) { pathElems.Insert(0, parentSM.name); return true; } } return false; }
internal extern string GetDisplayNameStateMachineSource(AnimatorStateMachine source);
private bool FindState(AnimatorStateMachine baseSm, int namehash) { return FindStateRecursively(baseSm, baseSm, namehash); }
internal static void PasteToStateMachineFromPasteboardInternal(AnimatorStateMachine sm, AnimatorController controller, int layerIndex, Vector3 position) { INTERNAL_CALL_PasteToStateMachineFromPasteboardInternal(sm, controller, layerIndex, ref position); }
public static void PasteToStateMachineFromPasteboard(AnimatorStateMachine sm, AnimatorController controller, int layerIndex, Vector3 position) { Undo.RegisterCompleteObjectUndo(sm, "Paste to StateMachine"); PasteToStateMachineFromPasteboardInternal(sm, controller, layerIndex, position); }
private static extern void INTERNAL_CALL_PasteToStateMachineFromPasteboardInternal(AnimatorStateMachine sm, AnimatorController controller, int layerIndex, ref Vector3 position);
private bool FindStateRecursively(AnimatorStateMachine baseSm, AnimatorStateMachine stateMachine, int nameHash) { foreach (ChildAnimatorState childState in stateMachine.states) { if (childState.state.GetFullPathHash(baseSm) == nameHash) { return true; } } foreach (ChildAnimatorStateMachine childStateMachine in stateMachine.stateMachines) { if (FindStateRecursively(baseSm, childStateMachine.stateMachine, nameHash)) return true; } return false; }
private void CreateStateMachine() { if (avatarPreview == null || avatarPreview.Animator == null) return; if (controller == null) { controller = new AnimatorController(); controller.hideFlags = HideFlags.DontSave; controller.AddLayer("preview"); stateMachine = controller.layers[0].stateMachine; CreateParameters(); state = stateMachine.AddState("preview"); state.motion = previewedMotion; state.iKOnFeet = this.avatarPreview.IKOnFeet; state.hideFlags = HideFlags.DontSave; stateMachine.hideFlags = HideFlags.DontSave; AnimatorController.SetAnimatorController(avatarPreview.Animator, controller); controller.AppendOnAnimatorControllerDirtyCallback(this.ControllerDitry); controllerIsDitry = false; } if (AnimatorControllerExtension.GetEffectiveAnimatorController(avatarPreview.Animator) != this.controller) { AnimatorController.SetAnimatorController(avatarPreview.Animator, this.controller); } }
private void InitController() { if (this.m_AvatarPreview != null && this.m_AvatarPreview.Animator != null) { if (this.m_Controller == null) { this.m_Controller = new UnityEditor.Animations.AnimatorController(); this.m_Controller.pushUndo = false; this.m_Controller.hideFlags = HideFlags.HideAndDontSave; this.m_Controller.AddLayer("preview"); this.m_StateMachine = this.m_Controller.layers[0].stateMachine; this.m_StateMachine.pushUndo = false; this.m_StateMachine.hideFlags = HideFlags.HideAndDontSave; if (this.m_ClipInfo != null) { this.InitMask(); UnityEditor.Animations.AnimatorControllerLayer[] layers = this.m_Controller.layers; layers[0].avatarMask = this.m_Mask; this.m_Controller.layers = layers; } } if (this.m_State == null) { this.m_State = this.m_StateMachine.AddState("preview"); this.m_State.pushUndo = false; UnityEditor.Animations.AnimatorControllerLayer[] layers2 = this.m_Controller.layers; this.m_State.motion = this.m_Clip; this.m_Controller.layers = layers2; this.m_State.iKOnFeet = this.m_AvatarPreview.IKOnFeet; this.m_State.hideFlags = HideFlags.HideAndDontSave; } UnityEditor.Animations.AnimatorController.SetAnimatorController(this.m_AvatarPreview.Animator, this.m_Controller); if (UnityEditor.Animations.AnimatorController.GetEffectiveAnimatorController(this.m_AvatarPreview.Animator) != this.m_Controller) { UnityEditor.Animations.AnimatorController.SetAnimatorController(this.m_AvatarPreview.Animator, this.m_Controller); } } }
private void ClearStateMachine() { if (avatarPreview != null && avatarPreview.Animator != null) { AnimatorController.SetAnimatorController(avatarPreview.Animator, null); } if (this.controller != null) { controller.RemoveOnAnimatorControllerDirtyCallback(this.ControllerDitry); } Object.DestroyImmediate(this.controller); //Object.DestroyImmediate(this.stateMachine); Object.DestroyImmediate(this.state); stateMachine = null; controller = null; state = null; }
private void DestroyController() { if (this.m_AvatarPreview != null && this.m_AvatarPreview.Animator != null) { UnityEditor.Animations.AnimatorController.SetAnimatorController(this.m_AvatarPreview.Animator, null); } UnityEngine.Object.DestroyImmediate(this.m_Controller); UnityEngine.Object.DestroyImmediate(this.m_State); this.m_Controller = null; this.m_StateMachine = null; this.m_State = null; }