List <AnimationClip> GetAnimationLengths() { List <AnimationClip> animationClips = new List <AnimationClip>(); RuntimeAnimatorController controller = transform.GetComponent <Animator>().runtimeAnimatorController; if (controller is UnityEditorInternal.AnimatorController) { UnityEditorInternal.StateMachine m = ((UnityEditorInternal.AnimatorController)controller).GetLayer(0).stateMachine; for (int i = 0; i < m.stateCount; i++) { AnimationClip clip = new AnimationClip(); // Obviously loading it depends on where/how clip is stored, best case its a resource, worse case you have to search asset database. if (m.GetState(i).GetMotion()) { string path = AssetDatabase.GetAssetPath(m.GetState(i).GetMotion()); clip = (AnimationClip)Resources.LoadAssetAtPath(path, typeof(AnimationClip)); //clip = (AnimationClip)Resources.LoadAssetAtPath("Assets/Puppet2D/Animation/" + m.GetState(i).GetMotion().name + ".anim", typeof(AnimationClip)); animationClips.Add(clip); } if (clip) { Debug.Log(clip.name + ", length is " + clip.length); } } } return(animationClips); }
public static IEnumerable <UnityEditorInternal.State> EnumerateStates(this UnityEditorInternal.StateMachine sm) { for (int i = 0; i < sm.stateCount; ++i) { yield return(sm.GetState(i)); } }
public static void ProcessAllTransitions(Animator animator, ProcessAnimatorTransition callback) { AnimatorController controller = GetInternalAnimatorController(animator); int layerCount = controller.layerCount; for (int layer = 0; layer < layerCount; layer++) { string layerName = controller.GetLayer(layer).name; UnityEditorInternal.StateMachine sm = controller.GetLayer(layer).stateMachine; //Handle anyState cases. see UnityEditorInternal.StateMachine.transitions { Transition[] anyTransitions = sm.GetTransitionsFromState(null); foreach (var t in anyTransitions) { TransitionInfo info = new TransitionInfo(t.uniqueNameHash, t.uniqueName, layer, layerName, 0, t.dstState.uniqueNameHash, t.atomic, t.duration, t.mute, t.offset, t.solo); callback(info); } } for (int i = 0; i < sm.stateCount; i++) { UnityEditorInternal.State state = sm.GetState(i); Transition[] transitions = sm.GetTransitionsFromState(state); foreach (var t in transitions) { // Debug.Log (state.uniqueName + ", transition: " + t.uniqueName + " ---" + " dest = " + t.dstState + " (" + (Animator.StringToHash (state.uniqueName) == Animator.StringToHash (layerName + "." + t.dstState)) + ") " + " src = " + t.srcState); TransitionInfo info = new TransitionInfo(t.uniqueNameHash, t.uniqueName, layer, layerName, t.srcState.uniqueNameHash, t.dstState.uniqueNameHash, t.atomic, t.duration, t.mute, t.offset, t.solo); callback(info); } } } }
static void FindAllAniInControlMachine(UnityEditorInternal.StateMachine machine, List <AnimationClip> list) { for (int i = 0; i < machine.stateCount; i++) { var s = machine.GetState(i); var m = s.GetMotion(); if (m != null) { if (list.Contains(m as AnimationClip) == false) { list.Add(m as AnimationClip); if (m.name != s.name) { //Debug.LogWarning("发现一个问题,clipname 和 state name 不相等 " + m.name + "=>" + s.name); } mapClip2State[m.name] = s.name; //建立一张转换表,把clip的名字转换为state的名字,以正常clone } } } for (int i = 0; i < machine.stateMachineCount; i++) { var m = machine.GetStateMachine(i); FindAllAniInControlMachine(m, list); } }
private List <AnimationCustomCurves> GetCurvesFromStateMachine(UnityEditorInternal.StateMachine stateMachine, string namePrefix) { var animCurvesList = new List <AnimationCustomCurves>(); // Por cada estado de la maquina for (int stateIndex = 0; stateIndex < stateMachine.stateCount; stateIndex++) { //Extrae el estado y el animation clip var state = stateMachine.GetState(stateIndex); var clip = state.GetMotion() as AnimationClip; // Si no esta el clip ya en la lista lo agrega if (!animCurvesList.Exists(x => x.name == clip.name)) { var deltaX = new AnimationCurve(); var deltaY = new AnimationCurve(); var deltaZ = new AnimationCurve(); // Extrae las curvas #pragma warning disable 612, 618 foreach (var curveData in AnimationUtility.GetAllCurves(clip)) #pragma warning restore 612, 618 { if (curveData.propertyName == "deltaX") { deltaX = NormalizeCurveLength(curveData.curve, 1f); } else if (curveData.propertyName == "deltaY") { deltaY = NormalizeCurveLength(curveData.curve, 1f); } else if (curveData.propertyName == "deltaZ") { deltaZ = NormalizeCurveLength(curveData.curve, 1f); } } // Agrega el nodo animCurvesList.Add(new AnimationCustomCurves(namePrefix + state.name, deltaX, deltaY, deltaZ)); } } // Para cada sub maquina de estado for (int index = 0; index < stateMachine.stateMachineCount; index++) { // Extrae la maquina de estados var subStateMachine = stateMachine.GetStateMachine(index); // Agrega al prefijo el nombre de la sub maquina var subNamePrefix = subStateMachine.name + ("."); // Extrae las curvas de la submaquina animCurvesList.AddRange(GetCurvesFromStateMachine(subStateMachine, subNamePrefix)); } return(animCurvesList); }
float getAnimClip() { float length; UnityEditorInternal.AnimatorController ac = anim.runtimeAnimatorController as UnityEditorInternal.AnimatorController; UnityEditorInternal.StateMachine sm = ac.GetLayer(0).stateMachine; UnityEditorInternal.State state = sm.GetState(0); AnimationClip clip = state.GetMotion() as AnimationClip; length = clip.length; return(length); }
public static IEnumerable <KeyValuePair <string, string> > EnumerateStateNamesRecursive(this UnityEditorInternal.StateMachine sm) { for (int i = 0; i < sm.stateCount; ++i) { yield return(new KeyValuePair <string, string>(sm.name + "." + sm.GetState(i).name, sm.GetState(i).name)); } for (int i = 0; i < sm.stateMachineCount; ++i) { var ssm = sm.GetStateMachine(i); foreach (var j in EnumerateStateNamesRecursive(ssm)) { yield return(j); } } }
/// <summary> /// Gets the count of transitions in a layer. /// </summary> /// <returns> /// The transition count. /// </returns> /// <param name='animator'> /// Animator. /// </param> /// <param name='layer'> /// Layer. /// </param> public static int GetTransitionsCount(Animator animator, int layer) { UnityEditorInternal.AnimatorController ac = animator.runtimeAnimatorController as UnityEditorInternal.AnimatorController; UnityEditorInternal.StateMachine stateMachine = ac.GetLayer(layer).stateMachine; int counter = 0; for (int i = 0; i < stateMachine.stateCount; i++) { Transition[] trans = stateMachine.GetTransitionsFromState(stateMachine.GetState(i)); counter += trans.Length; } return(counter); // return stateMachine.transitionCount; }
/// <summary> /// Adds convenience methods to determine the current Animator state as boolean methods to the class code /// element (e.g. IsIdle ()). NOTE that this method relies on classes from namespace UnityEditorInternal /// which can be subject to changes in future releases. /// </summary> public static void ProcessAllAnimatorStates(Animator animator, ProcessAnimatorState callback) { AnimatorController controller = GetInternalAnimatorController(animator); int layerCount = controller.layerCount; for (int layer = 0; layer < layerCount; layer++) { string layerName = controller.GetLayer(layer).name; UnityEditorInternal.StateMachine sm = controller.GetLayer(layer).stateMachine; for (int i = 0; i < sm.stateCount; i++) { UnityEditorInternal.State state = sm.GetState(i); Motion motion = state.GetMotion(); float duration = (motion != null ? motion.averageDuration : 0f); string motionName = (motion != null ? motion.name : ""); StateInfo info = new StateInfo(state.uniqueNameHash, layer, layerName, state.uniqueName, state.tag, state.speed, state.iKOnFeet, state.mirror, motionName, duration); callback(info); } } }
void OnGUI() { if (GUI.Button(new Rect(0, 100, 100, 40), new GUIContent("获取State"))) { UnityEditorInternal.AnimatorController animController = Animator.runtimeAnimatorController as UnityEditorInternal.AnimatorController; int layerCount = animController.layerCount; Debug.Log(string.Format("动画层数量: {0}", layerCount)); // 动画层名称 for (int layer = 0; layer < layerCount; layer++) { Debug.Log(string.Format("Layer {0}: {1}", layer, animController.GetLayer(layer).name)); } // 动画层0上面的State UnityEditorInternal.StateMachine sm = animController.GetLayer(0).stateMachine; for (int i = 0; i < sm.stateCount; i++) { UnityEditorInternal.State state = sm.GetState(i); Debug.Log(string.Format("State: {0}, 唯一名称: {1}", state.name, state.uniqueName)); } } }
//找到animator中的animationclip private static AnimationClip FindAnimation(GameObject obj) { string name = null; AnimationClip clip = new AnimationClip(); Animator anim = obj.GetComponent <Animator>(); if (anim != null) { UnityEditorInternal.AnimatorController ac = anim.runtimeAnimatorController as UnityEditorInternal.AnimatorController; if (null == ac) { return(null); } if (null == ac.GetLayer(0)) { return(null); } UnityEditorInternal.StateMachine sm = ac.GetLayer(0).stateMachine; for (int i = 0; i < sm.stateCount; i++) { UnityEditorInternal.State state = sm.GetState(i); clip = state.GetMotion() as AnimationClip; if (clip != null) { name = clip.name; return(clip); } } } return(null); }
void OnGUI() { EditorGUILayout.BeginVertical(); GameObject modeling = EditorGUILayout.ObjectField(go, typeof(GameObject), true) as GameObject; if (go != modeling) { go = modeling; isPlaying = false; if (sample != null) { GameObject.DestroyImmediate(sample); } } if (go != null) { if (GUILayout.Button("생성")) { if (sample == null) { sample = (GameObject)GameObject.Instantiate(go); } } if (GUILayout.Button("삭제")) { if (sample != null) { GameObject.DestroyImmediate(sample); } } if (GUILayout.Button("재생")) { if (sample != null) { GameObject.DestroyImmediate(sample); } sample = (GameObject)GameObject.Instantiate(go); if (sample != null) { anim = sample.GetComponent <Animator>(); if (anim == null) { Animator[] ats = sample.GetComponentsInChildren <Animator>(); if (ats != null && ats.Length > 0) { anim = ats[0]; } } aniTarget = null; if (anim != null) { aniTarget = anim.gameObject; UnityEditorInternal.AnimatorController act = anim.runtimeAnimatorController as UnityEditorInternal.AnimatorController; UnityEditorInternal.StateMachine sm = act.GetLayer(0).stateMachine; for (int i = 0; i < sm.stateCount; i++) { UnityEditorInternal.State state = sm.GetState(i); ac = state.GetMotion() as AnimationClip; } } if (sample.animation != null && sample.animation.clip != null) { ani = sample.animation; } else { ani = sample.GetComponentInChildren <Animation>(); } if (ani != null && ani.clip != null) { ac = ani.clip; aniTarget = ani.gameObject; } if (ac != null) { aniLength = ac.length; wrapMode = ac.wrapMode; } Selection.activeGameObject = sample; if (sample.particleSystem != null) { sample.particleSystem.Play(true); } ParticleSystem[] pss = sample.GetComponentsInChildren <ParticleSystem>(); if (pss != null && pss.Length > 0) { ps = pss[0]; } else { ps = null; } Selection.activeGameObject = null; currentPlayTime = 0; isPlaying = true; } } } EditorGUILayout.EndVertical(); }
private static void SearchMotion(AnimatorController animator, StateMachine stateMachine, AssetData assetData) { var stateCount = 0; if (isUnity41) { stateCount = (int)stateMachine.GetType().GetMethod("GetStateCount").Invoke(stateMachine, new object[0]); } else { stateCount = (int)stateMachine.GetType().GetProperty("stateCount").GetValue(stateMachine, new object[0]); } for (var k = 0; k < stateCount; k++) { var state = stateMachine.GetState(k); var parameters = new object[0]; var types = new Type[0]; if (isUnity41) { ArrayUtility.Add(ref parameters, 0); ArrayUtility.Add(ref types, typeof(int)); } var motion = state.GetType().GetMethod("GetMotion", types).Invoke(state, parameters) as Motion; if (motion) SearchBlendTreeMotion(animator, motion, assetData); } }