private static Dictionary <int, int> GetStatesFromStatemachine(UnityEditor.Animations.AnimatorStateMachine stateMachine) { Dictionary <int, int> dict = new Dictionary <int, int>(); for (int i = 0; i != stateMachine.states.Length; ++i) { UnityEditor.Animations.ChildAnimatorState state = stateMachine.states[i]; if (state.state.motion is UnityEditor.Animations.BlendTree) { //UnityEditor.Animations.BlendTree blendTree = state.state.motion as UnityEditor.Animations.BlendTree; //ChildMotion[] childMotion = blendTree.children; //for (int j = 0; j != childMotion.Length; ++j) //{ // list.Add(childMotion[j].motion as AnimationClip); //} } else if (state.state.motion != null) { AnimationClip clip = state.state.motion as AnimationClip; dict.Add(state.state.nameHash, Animator.StringToHash(clip.name)); } } for (int i = 0; i != stateMachine.stateMachines.Length; ++i) { Dictionary <int, int> childDict = GetStatesFromStatemachine(stateMachine.stateMachines[i].stateMachine); foreach (var d in childDict) { dict.Add(d.Key, d.Value); } } // var distinctClips = list.Select(q => (AnimationClip)q).Distinct().ToList(); return(dict); }
private List<AnimationClip> GetClipsFromStatemachine(UnityEditor.Animations.AnimatorStateMachine stateMachine) { List<AnimationClip> list = new List<AnimationClip>(); for (int i = 0; i != stateMachine.states.Length; ++i) { UnityEditor.Animations.ChildAnimatorState state = stateMachine.states[i]; if (state.state.motion is UnityEditor.Animations.BlendTree) { UnityEditor.Animations.BlendTree blendTree = state.state.motion as UnityEditor.Animations.BlendTree; ChildMotion[] childMotion = blendTree.children; for(int j = 0; j != childMotion.Length; ++j) { list.Add(childMotion[j].motion as AnimationClip); } } else if (state.state.motion != null) list.Add(state.state.motion as AnimationClip); } for (int i = 0; i != stateMachine.stateMachines.Length; ++i) { list.AddRange(GetClipsFromStatemachine(stateMachine.stateMachines[i].stateMachine)); } var distinctClips = list.Select(q => (AnimationClip)q).Distinct().ToList(); for (int i = 0; i < distinctClips.Count; i++) { if (distinctClips[i] && generateAnims.ContainsKey(distinctClips[i].name) == false) generateAnims.Add(distinctClips[i].name, true); } return list; }
private List <AnimationClip> GetClipsFromStatemachine(UnityEditor.Animations.AnimatorStateMachine stateMachine) { List <AnimationClip> list = new List <AnimationClip>(); for (int i = 0; i != stateMachine.states.Length; ++i) { UnityEditor.Animations.ChildAnimatorState state = stateMachine.states[i]; list.Add(state.state.motion as AnimationClip); } for (int i = 0; i != stateMachine.stateMachines.Length; ++i) { list.AddRange(GetClipsFromStatemachine(stateMachine.stateMachines[i].stateMachine)); } var distinctClips = list.Select(q => (AnimationClip)q).Distinct().ToList(); for (int i = 0; i < distinctClips.Count; i++) { if (distinctClips[i] && generateAnims.ContainsKey(distinctClips[i].name) == false) { generateAnims.Add(distinctClips[i].name, true); } } return(list); }
private void FixPositions(GameObject gameObj, float deltaScaleX, float deltaScaleY) { Vector3 newPos = gameObj.transform.localPosition; newPos.x *= deltaScaleX; newPos.y *= deltaScaleY; gameObj.transform.localPosition = newPos; // fix Animation components Animation[] animations = gameObj.GetComponents <Animation>(); foreach (Animation animation in animations) { foreach (AnimationState animState in animation) { if (animState.clip != null) { this.FixAnimationClip(animState.clip, deltaScaleX, deltaScaleY); } } } // fix Animator components Animator[] animators = gameObj.GetComponents <Animator>(); foreach (Animator animator in animators) { UnityEditor.Animations.AnimatorController animController = animator.runtimeAnimatorController as UnityEditor.Animations.AnimatorController; #if UNITY_5_PLUS for (int i = 0; i < animController.layers.Length; i++) #else for (int i = 0; i < animator.layerCount; i++) #endif { #if UNITY_5_PLUS UnityEditor.Animations.AnimatorStateMachine stateMachine = animController.layers[i].stateMachine; for (int j = 0; j < stateMachine.states.Length; j++) #else UnityEditor.Animations.AnimatorStateMachine stateMachine = animController.GetLayer(i).stateMachine; for (int j = 0; j < stateMachine.stateCount; j++) #endif { #if UNITY_5_PLUS UnityEditor.Animations.ChildAnimatorState state = stateMachine.states[j]; Motion mtn = state.state.motion; #else UnityEditor.Animations.AnimatorState state = stateMachine.GetState(j); Motion mtn = state.GetMotion(); #endif if (mtn != null) { AnimationClip clip = mtn as AnimationClip; this.FixAnimationClip(clip, deltaScaleX, deltaScaleY); } } } } }
public void AddState(AnimatorState state, Vector3 position) { this.undoHandler.DoUndo(this, "State added"); ChildAnimatorState item = default(ChildAnimatorState); item.state = state; item.position = position; ChildAnimatorState[] states = this.states; ArrayUtility.Add <ChildAnimatorState>(ref states, item); this.states = states; }
/// <summary> /// <para>Utility function to add a state to the state machine.</para> /// </summary> /// <param name="state">The state to add.</param> /// <param name="position">The position of the state node.</param> public void AddState(AnimatorState state, Vector3 position) { this.undoHandler.DoUndo((UnityEngine.Object) this, "State added"); ChildAnimatorState childAnimatorState = new ChildAnimatorState(); childAnimatorState.state = state; childAnimatorState.position = position; ChildAnimatorState[] states = this.states; ArrayUtility.Add <ChildAnimatorState>(ref states, childAnimatorState); this.states = states; }
public static void AddTransitionToState(Animator animator, string[] nodeNames, AnimatorStateTransition transition) { // iterate over the animator graph's node, checking if the node is one we'll be triggering as a power animation // and stick a UnityEditor.Animations.AnimatorController ac = animator.runtimeAnimatorController as UnityEditor.Animations.AnimatorController; for (int i = 0; i < animator.layerCount; i++) { ChildAnimatorState[] aStates = ac.layers[i].stateMachine.states; for (int j = 0; j < aStates.Length; j++) { UnityEditor.Animations.ChildAnimatorState aState = aStates[j]; // check if its a power anim, by comparing the node name with known power anim names bool onList = false; for (int k = 0; k < nodeNames.Length; k++) { if (aState.state.name == nodeNames[k]) { onList = true; } } if (onList) { // check if we already go to the state in the required transition bool alreadySet = false; for (int k = 0; k < aState.state.transitions.Length; k++) { if (aState.state.transitions[k].destinationState == transition.destinationState) { alreadySet = true; } } // if not, add the required transition if (!alreadySet) { aState.state.AddTransition(transition); } else { // make sure we've got all the conditions? } } } } }
public void AddState(AnimatorState state, Vector3 position) { ChildAnimatorState[] childStates = states; if (System.Array.Exists(childStates, childState => childState.state == state)) { Debug.LogWarning(System.String.Format("State '{0}' already exists in state machine '{1}', discarding new state.", state.name, name)); return; } undoHandler.DoUndo(this, "State added"); ChildAnimatorState newState = new ChildAnimatorState(); newState.state = state; newState.position = position; ArrayUtility.Add(ref childStates, newState); states = childStates; }
private static List <AnimClipData> GetClipsFromStatemachine(UnityEditor.Animations.AnimatorStateMachine stateMachine) { List <AnimClipData> list = new List <AnimClipData>(); for (int i = 0; i != stateMachine.states.Length; ++i) { UnityEditor.Animations.ChildAnimatorState state = stateMachine.states[i]; if (state.state.motion is UnityEditor.Animations.BlendTree) { UnityEditor.Animations.BlendTree blendTree = state.state.motion as UnityEditor.Animations.BlendTree; ChildMotion[] childMotion = blendTree.children; for (int j = 0; j != childMotion.Length; ++j) { AnimClipData clipData = new AnimClipData(); clipData.clip = childMotion[j].motion as AnimationClip; clipData.speed = 1; list.Add(clipData); } } else if (state.state.motion != null) { AnimClipData clipData = new AnimClipData(); clipData.clip = state.state.motion as AnimationClip; clipData.speed = state.state.speed; list.Add(clipData); } } for (int i = 0; i != stateMachine.stateMachines.Length; ++i) { list.AddRange(GetClipsFromStatemachine(stateMachine.stateMachines[i].stateMachine)); } list = list.Select(q => (AnimClipData)q).Distinct().ToList(); return(list); }
private static int FixState(int scnt, UnityEditor.Animations.ChildAnimatorState curState) { if (curState.state.hideFlags != (HideFlags)1) { curState.state.hideFlags = (HideFlags)1; scnt++; } if (curState.state.motion != null) { if (curState.state.motion.hideFlags != (HideFlags)1) { curState.state.motion.hideFlags = (HideFlags)1; } } if (curState.state.transitions != null) { foreach (UnityEditor.Animations.AnimatorStateTransition curTrans in curState.state.transitions) { if (curTrans.hideFlags != (HideFlags)1) { curTrans.hideFlags = (HideFlags)1; } } } if (curState.state.behaviours != null) { foreach (StateMachineBehaviour behaviour in curState.state.behaviours) { if (behaviour.hideFlags != (HideFlags)1) { behaviour.hideFlags = (HideFlags)1; } } } return(scnt); }
// void Start () // { // Animator animator = GetComponent<Animator>(); // // if (animator && animationName != string.Empty) // { // animator.Play(animationName); // } // } /// <summary> /// Sets the animation clip as default animator state and plays it. /// </summary> /// <returns><c>true</c> on success, <c>false</c> otherwise.</returns> /// <param name="modelFilePath">Fbx-model file path.</param> /// <param name="animClipName">Animation clip name.</param> public bool PlayAnimationClip(string modelFilePath, string animClipName) { this.animationName = animClipName; Animator animator = GetComponent <Animator>(); #if UNITY_EDITOR if (animator) { RuntimeAnimatorController animatorCtrlRT = animator.runtimeAnimatorController; string animatorPath = animatorCtrlRT ? UnityEditor.AssetDatabase.GetAssetPath(animatorCtrlRT) : string.Empty; Object[] fbxObjects = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(modelFilePath); if (fbxObjects != null && animatorPath != string.Empty) { foreach (Object fbxObject in fbxObjects) { if (fbxObject is AnimationClip && fbxObject.name == animClipName) { AnimationClip fbxAnimClip = (AnimationClip)fbxObject; if (fbxAnimClip) { UnityEditor.Animations.AnimatorController animatorCtrl = animatorCtrlRT as UnityEditor.Animations.AnimatorController; UnityEditor.Animations.ChildAnimatorState[] animStates = animatorCtrl.layers[0].stateMachine.states; bool bStateFound = false; for (int i = 0; i < animStates.Length; i++) { UnityEditor.Animations.ChildAnimatorState animState = animStates[i]; if (animState.state.name == animClipName) { animatorCtrl.layers[0].stateMachine.states[i].state.motion = fbxAnimClip; animatorCtrl.layers[0].stateMachine.defaultState = animatorCtrl.layers[0].stateMachine.states[i].state; bStateFound = true; break; } } if (!bStateFound) { UnityEditor.Animations.AnimatorState animState = animatorCtrl.layers[0].stateMachine.AddState(animClipName); animState.motion = fbxAnimClip; animatorCtrl.layers[0].stateMachine.defaultState = animState; } } break; } } } } #endif if (animator && animClipName != string.Empty) { animator.Play(animClipName); return(true); } return(false); }
private void ProcessAnimationState(string animFolderStr, string layer, string stateMachine, UnityEditor.Animations.ChildAnimatorState state) { var originalAnimation = state.state.motion as AnimationClip; // Some states have no animation clips attached if (originalAnimation != null) { AnimationClip newAnimation = null; try { newAnimation = Instantiate(originalAnimation); AssetDatabase.CreateAsset(newAnimation, ProcessFolder(animFolderStr + "/" + layer + "/" + stateMachine) + "/" + originalAnimation.name + ".anim"); state.state.motion = newAnimation; currentAnim++; } catch (Exception e) { Debug.LogError("Could not duplicate animation clip " + state.state.ToString() + " // " + e.Message); } } }