public static IEnumerable <UnityEditorInternal.State> EnumerateStates(this UnityEditorInternal.StateMachine sm) { for (int i = 0; i < sm.stateCount; ++i) { yield return(sm.GetState(i)); } }
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); } }
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); } } } }
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); }
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); }
static ControllerStateMachine ConverStateMachine(UnityEditorInternal.StateMachine stateMachine, ControllerState[] states) { return(new ControllerStateMachine() { name = stateMachine.name, hash = Animator.StringToHash(stateMachine.name), states = states.Where(i => stateMachine.EnumerateStates().Any(j => j.uniqueNameHash == i.uniqueNameHash)).ToArray(), subStateMachines = Enumerable.Range(0, stateMachine.stateMachineCount).Select(i => ConverStateMachine(stateMachine.GetStateMachine(i), states)).ToArray() }); }
private static string[] GetTransitionNames(Animator animator, int layer) { List <string> transitionNames = new List <string>(); UnityEditorInternal.AnimatorController ac = animator.runtimeAnimatorController as UnityEditorInternal.AnimatorController; UnityEditorInternal.StateMachine stateMachine = ac.GetLayer(layer).stateMachine; FromStateMachineToTransitionName(stateMachine, transitionNames); return(transitionNames.ToArray()); }
private static int[] GetStateKeys(Animator animator, int layer) { List <int> stateKeys = new List <int>(); UnityEditorInternal.AnimatorController ac = animator.runtimeAnimatorController as UnityEditorInternal.AnimatorController; UnityEditorInternal.StateMachine stateMachine = ac.GetLayer(layer).stateMachine; FromStateMachineToStateKey(stateMachine, stateKeys); return(stateKeys.ToArray()); }
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 <UnityEditorInternal.State> EnumerateStatesRecursive(this UnityEditorInternal.StateMachine sm) { foreach (var i in EnumerateStates(sm)) { yield return(i); } for (int i = 0; i < sm.stateMachineCount; ++i) { foreach (var j in EnumerateStatesRecursive(sm.GetStateMachine(i))) { yield return(j); } } }
public override void OnEnable() { base.OnEnable(); machine = (StateMachine)target; machineObject = machine.CachedGameObject; machine.SetScriptIcon(AssetDatabaseUtility.LoadAssetInFolder<Texture2D>("statemachine.png", "StateMachine")); HideMachineComponents(); if (machine.GetComponents<StateMachine>().Length > 1) { Debug.LogError("There can be only one StateMachine per GameObject."); machine.Destroy(); } }
public override void OnEnable() { base.OnEnable(); _machine = (StateMachine)target; _machineObject = _machine.gameObject; _machine.SetScriptIcon(HelperFunctions.LoadAssetInFolder<Texture2D>("statemachine.png", "StateMachine")); HideMachineComponents(); if (_machine.GetComponents<StateMachine>().Length > 1) { Debug.LogError("There can be only one StateMachine per GameObject."); _machine.Destroy(); } }
static AnimatorController BuildAnimationController(List <AnimationClip> clips, string name) { AnimatorController animatorController = AnimatorController.CreateAnimatorControllerAtPath(AnimationControllerPath + "/" + name + ".controller"); AnimatorControllerLayer layer = animatorController.GetLayer(0); UnityEditorInternal.StateMachine sm = layer.stateMachine; foreach (AnimationClip newClip in clips) { State state = sm.AddState(newClip.name); state.SetAnimationClip(newClip, layer); Transition trans = sm.AddAnyStateTransition(state); trans.RemoveCondition(0); } AssetDatabase.SaveAssets(); return(animatorController); }
/// <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); } } }
static void DoCreateAnimationAssets() { DirectoryInfo raw = new DirectoryInfo(ModelPath); string appendString = ""; foreach (DirectoryInfo dictory in raw.GetDirectories()) { appendString += ":" + dictory.Name; State lastState = null; AnimatorController animatorController = AnimatorController.CreateAnimatorControllerAtPath(animationControllerPath + "/" + dictory.Name + ".controller"); //得到它的Layer, 默认layer为base 你可以去拓展 AnimatorControllerLayer layer = animatorController.GetLayer(0); List <State> stateList = new List <State>(); foreach (FileInfo file in dictory.GetFiles()) { //每个文件夹就是一组帧动画,这里把每个文件夹下的所有图片生成出一个动画文件 //创建animationController文件,保存在Assets路径下 if (file.Name.Contains("@") && !file.Name.Contains(".meta")) { //把动画文件保存在我们创建的AnimationController中 string fbxPath = "Assets/Model/" + dictory.Name + "/" + file.Name; // Debug.Log (fbxPath); lastState = AddStateTransition(fbxPath, layer, lastState); stateList.Add(lastState); } } UnityEditorInternal.StateMachine sm = layer.stateMachine; sm.AddTransition(stateList[stateList.Count - 1], stateList[0]); BuildPrefab(dictory, animatorController); } Debug.Log(appendString + "==========="); AppendString(appendString); // DrawPrefabToSceen (); }
private static State AddStateTransition(string path, AnimatorControllerLayer layer, State lastState) { UnityEditorInternal.StateMachine sm = layer.stateMachine; //根据动画文件读取它的AnimationClip对象 AnimationClip newClip = AssetDatabase.LoadAssetAtPath(path, typeof(AnimationClip)) as AnimationClip; AnimationUtility.SetAnimationType(newClip, ModelImporterAnimationType.Generic); //取出动画名子 添加到state里面 State state = sm.AddState(newClip.name); state.SetAnimationClip(newClip, layer); //把state添加在layer里面 if (lastState == null) { // sm.AddAnyStateTransition (state); } else { sm.AddTransition(lastState, state); } return(state); }
//找到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() { 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)); } } }
void CheckResources() { ActiveTextures.Clear(); ActiveMaterials.Clear(); ActiveMeshDetails.Clear(); Renderer[] renderers = FindObjects <Renderer>(); //Debug.Log("Total renderers "+renderers.Length); foreach (Renderer renderer in renderers) { //Debug.Log("Renderer is "+renderer.name); foreach (Material material in renderer.sharedMaterials) { MaterialDetails tMaterialDetails = FindMaterialDetails(material); if (tMaterialDetails == null) { tMaterialDetails = new MaterialDetails(); tMaterialDetails.material = material; ActiveMaterials.Add(tMaterialDetails); } tMaterialDetails.FoundInRenderers.Add(renderer); } if (renderer is SpriteRenderer) { SpriteRenderer tSpriteRenderer = (SpriteRenderer)renderer; if (tSpriteRenderer.sprite != null) { var tSpriteTextureDetail = GetTextureDetail(tSpriteRenderer.sprite.texture, renderer); if (!ActiveTextures.Contains(tSpriteTextureDetail)) { ActiveTextures.Add(tSpriteTextureDetail); } } } } if (IncludeGuiElements) { Graphic[] graphics = FindObjects <Graphic>(); foreach (Graphic graphic in graphics) { if (graphic.mainTexture) { var tSpriteTextureDetail = GetTextureDetail(graphic.mainTexture, graphic); if (!ActiveTextures.Contains(tSpriteTextureDetail)) { ActiveTextures.Add(tSpriteTextureDetail); } } if (graphic.materialForRendering) { MaterialDetails tMaterialDetails = FindMaterialDetails(graphic.materialForRendering); if (tMaterialDetails == null) { tMaterialDetails = new MaterialDetails(); tMaterialDetails.material = graphic.materialForRendering; ActiveMaterials.Add(tMaterialDetails); } tMaterialDetails.FoundInGraphics.Add(graphic); } } } foreach (MaterialDetails tMaterialDetails in ActiveMaterials) { Material tMaterial = tMaterialDetails.material; if (tMaterial != null) { var dependencies = EditorUtility.CollectDependencies(new UnityEngine.Object[] { tMaterial }); foreach (Object obj in dependencies) { if (obj is Texture) { Texture tTexture = obj as Texture; var tTextureDetail = GetTextureDetail(tTexture, tMaterial, tMaterialDetails); ActiveTextures.Add(tTextureDetail); } } //if the texture was downloaded, it won't be included in the editor dependencies if (tMaterial.mainTexture != null && !dependencies.Contains(tMaterial.mainTexture)) { var tTextureDetail = GetTextureDetail(tMaterial.mainTexture, tMaterial, tMaterialDetails); ActiveTextures.Add(tTextureDetail); } } } MeshFilter[] meshFilters = FindObjects <MeshFilter>(); foreach (MeshFilter tMeshFilter in meshFilters) { Mesh tMesh = tMeshFilter.sharedMesh; if (tMesh != null) { MeshDetails tMeshDetails = FindMeshDetails(tMesh); if (tMeshDetails == null) { tMeshDetails = new MeshDetails(); tMeshDetails.mesh = tMesh; ActiveMeshDetails.Add(tMeshDetails); } tMeshDetails.FoundInMeshFilters.Add(tMeshFilter); } } SkinnedMeshRenderer[] skinnedMeshRenderers = FindObjects <SkinnedMeshRenderer>(); foreach (SkinnedMeshRenderer tSkinnedMeshRenderer in skinnedMeshRenderers) { Mesh tMesh = tSkinnedMeshRenderer.sharedMesh; if (tMesh != null) { MeshDetails tMeshDetails = FindMeshDetails(tMesh); if (tMeshDetails == null) { tMeshDetails = new MeshDetails(); tMeshDetails.mesh = tMesh; ActiveMeshDetails.Add(tMeshDetails); } tMeshDetails.FoundInSkinnedMeshRenderer.Add(tSkinnedMeshRenderer); } } if (IncludeSpriteAnimations) { Animator[] animators = FindObjects <Animator>(); foreach (Animator anim in animators) { #if UNITY_4_6 || UNITY_4_5 || UNITY_4_4 || UNITY_4_3 UnityEditorInternal.AnimatorController ac = anim.runtimeAnimatorController as UnityEditorInternal.AnimatorController; #elif UNITY_5 UnityEditor.Animations.AnimatorController ac = anim.runtimeAnimatorController as UnityEditor.Animations.AnimatorController; #endif //Skip animators without layers, this can happen if they don't have an animator controller. if (!ac || ac.layers == null || ac.layers.Length == 0) { continue; } for (int x = 0; x < anim.layerCount; x++) { #if UNITY_4_6 || UNITY_4_5 || UNITY_4_4 || UNITY_4_3 UnityEditorInternal.StateMachine sm = ac.GetLayer(x).stateMachine; int cnt = sm.stateCount; #elif UNITY_5 UnityEditor.Animations.AnimatorStateMachine sm = ac.layers[x].stateMachine; int cnt = sm.states.Length; #endif for (int i = 0; i < cnt; i++) { #if UNITY_4_6 || UNITY_4_5 || UNITY_4_4 || UNITY_4_3 UnityEditorInternal.State state = sm.GetState(i); Motion m = state.GetMotion(); #elif UNITY_5 UnityEditor.Animations.AnimatorState state = sm.states[i].state; Motion m = state.motion; #endif if (m != null) { AnimationClip clip = m as AnimationClip; EditorCurveBinding[] ecbs = AnimationUtility.GetObjectReferenceCurveBindings(clip); foreach (EditorCurveBinding ecb in ecbs) { if (ecb.propertyName == "m_Sprite") { foreach (ObjectReferenceKeyframe keyframe in AnimationUtility.GetObjectReferenceCurve(clip, ecb)) { Sprite tSprite = keyframe.value as Sprite; if (tSprite != null) { var tTextureDetail = GetTextureDetail(tSprite.texture, anim); if (!ActiveTextures.Contains(tTextureDetail)) { ActiveTextures.Add(tTextureDetail); } } } } } } } } } } if (IncludeScriptReferences) { MonoBehaviour[] scripts = FindObjects <MonoBehaviour>(); foreach (MonoBehaviour script in scripts) { BindingFlags flags = BindingFlags.Public | BindingFlags.Instance; // only public non-static fields are bound to by Unity. FieldInfo[] fields = script.GetType().GetFields(flags); foreach (FieldInfo field in fields) { System.Type fieldType = field.FieldType; if (fieldType == typeof(Sprite)) { Sprite tSprite = field.GetValue(script) as Sprite; if (tSprite != null) { var tSpriteTextureDetail = GetTextureDetail(tSprite.texture, script); if (!ActiveTextures.Contains(tSpriteTextureDetail)) { ActiveTextures.Add(tSpriteTextureDetail); } } } } } } TotalTextureMemory = 0; foreach (TextureDetails tTextureDetails in ActiveTextures) { TotalTextureMemory += tTextureDetails.memSizeKB; } TotalMeshVertices = 0; foreach (MeshDetails tMeshDetails in ActiveMeshDetails) { TotalMeshVertices += tMeshDetails.mesh.vertexCount; } // Sort by size, descending ActiveTextures.Sort(delegate(TextureDetails details1, TextureDetails details2) { return(details2.memSizeKB - details1.memSizeKB); }); ActiveMeshDetails.Sort(delegate(MeshDetails details1, MeshDetails details2) { return(details2.mesh.vertexCount - details1.mesh.vertexCount); }); collectedInPlayingMode = Application.isPlaying; }
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); } } }
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); } }
void ListTextures() { textureListScrollPos = EditorGUILayout.BeginScrollView(textureListScrollPos); foreach (TextureDetails tDetails in ActiveTextures) { GUILayout.BeginHorizontal(); Texture tex = new Texture(); tex = tDetails.texture; #if !UNITY_EDITOR_OSX if (tDetails.texture.GetType() == typeof(Texture2DArray) || tDetails.texture.GetType() == typeof(Cubemap)) { #else if (tDetails.texture.GetType() == typeof(Cubemap)) { #endif tex = AssetPreview.GetMiniThumbnail(tDetails.texture); } GUILayout.Box(tex, GUILayout.Width(ThumbnailWidth), GUILayout.Height(ThumbnailHeight)); if (tDetails.instance == true) { GUI.color = new Color(0.8f, 0.8f, defColor.b, 1.0f); } if (tDetails.isgui == true) { GUI.color = new Color(defColor.r, 0.95f, 0.8f, 1.0f); } if (tDetails.isSky) { GUI.color = new Color(0.9f, defColor.g, defColor.b, 1.0f); } if (GUILayout.Button(tDetails.texture.name, GUILayout.Width(150))) { SelectObject(tDetails.texture, ctrlPressed); } GUI.color = defColor; string sizeLabel = "" + tDetails.texture.width + "x" + tDetails.texture.height; if (tDetails.isCubeMap) { sizeLabel += "x6"; } #if !UNITY_EDITOR_OSX if (tDetails.texture.GetType() == typeof(Texture2DArray)) { sizeLabel += "[]\n" + ((Texture2DArray)tDetails.texture).depth + "depths"; } #endif sizeLabel += " - " + tDetails.mipMapCount + "mip\n" + FormatSizeString(tDetails.memSizeKB) + " - " + tDetails.format; GUILayout.Label(sizeLabel, GUILayout.Width(120)); if (GUILayout.Button(tDetails.FoundInMaterials.Count + " Mat", GUILayout.Width(50))) { SelectObjects(tDetails.FoundInMaterials, ctrlPressed); } HashSet <Object> FoundObjects = new HashSet <Object>(); foreach (Renderer renderer in tDetails.FoundInRenderers) { FoundObjects.Add(renderer.gameObject); } foreach (Animator animator in tDetails.FoundInAnimators) { FoundObjects.Add(animator.gameObject); } foreach (Graphic graphic in tDetails.FoundInGraphics) { FoundObjects.Add(graphic.gameObject); } foreach (MonoBehaviour script in tDetails.FoundInScripts) { FoundObjects.Add(script.gameObject); } if (GUILayout.Button(FoundObjects.Count + " GO", GUILayout.Width(50))) { SelectObjects(new List <Object>(FoundObjects), ctrlPressed); } GUILayout.EndHorizontal(); } if (ActiveTextures.Count > 0) { EditorGUILayout.Space(); GUILayout.BeginHorizontal(); //GUILayout.Box(" ",GUILayout.Width(ThumbnailWidth),GUILayout.Height(ThumbnailHeight)); if (GUILayout.Button("Select \n All", GUILayout.Width(ThumbnailWidth * 2))) { List <Object> AllTextures = new List <Object>(); foreach (TextureDetails tDetails in ActiveTextures) { AllTextures.Add(tDetails.texture); } SelectObjects(AllTextures, ctrlPressed); } EditorGUILayout.EndHorizontal(); } EditorGUILayout.EndScrollView(); } void ListMaterials() { materialListScrollPos = EditorGUILayout.BeginScrollView(materialListScrollPos); foreach (MaterialDetails tDetails in ActiveMaterials) { if (tDetails.material != null) { GUILayout.BeginHorizontal(); GUILayout.Box(AssetPreview.GetAssetPreview(tDetails.material), GUILayout.Width(ThumbnailWidth), GUILayout.Height(ThumbnailHeight)); if (tDetails.instance == true) { GUI.color = new Color(0.8f, 0.8f, defColor.b, 1.0f); } if (tDetails.isgui == true) { GUI.color = new Color(defColor.r, 0.95f, 0.8f, 1.0f); } if (tDetails.isSky) { GUI.color = new Color(0.9f, defColor.g, defColor.b, 1.0f); } if (GUILayout.Button(tDetails.material.name, GUILayout.Width(150))) { SelectObject(tDetails.material, ctrlPressed); } GUI.color = defColor; string shaderLabel = tDetails.material.shader != null ? tDetails.material.shader.name : "no shader"; GUILayout.Label(shaderLabel, GUILayout.Width(200)); if (GUILayout.Button((tDetails.FoundInRenderers.Count + tDetails.FoundInGraphics.Count) + " GO", GUILayout.Width(50))) { List <Object> FoundObjects = new List <Object>(); foreach (Renderer renderer in tDetails.FoundInRenderers) { FoundObjects.Add(renderer.gameObject); } foreach (Graphic graphic in tDetails.FoundInGraphics) { FoundObjects.Add(graphic.gameObject); } SelectObjects(FoundObjects, ctrlPressed); } GUILayout.EndHorizontal(); } } EditorGUILayout.EndScrollView(); } void ListMeshes() { meshListScrollPos = EditorGUILayout.BeginScrollView(meshListScrollPos); foreach (MeshDetails tDetails in ActiveMeshDetails) { if (tDetails.mesh != null) { GUILayout.BeginHorizontal(); string name = tDetails.mesh.name; if (name == null || name.Count() < 1) { name = tDetails.FoundInMeshFilters[0].gameObject.name; } if (tDetails.instance == true) { GUI.color = new Color(0.8f, 0.8f, defColor.b, 1.0f); } if (GUILayout.Button(name, GUILayout.Width(150))) { SelectObject(tDetails.mesh, ctrlPressed); } GUI.color = defColor; string sizeLabel = "" + tDetails.mesh.vertexCount + " vert"; GUILayout.Label(sizeLabel, GUILayout.Width(100)); if (GUILayout.Button(tDetails.FoundInMeshFilters.Count + " GO", GUILayout.Width(50))) { List <Object> FoundObjects = new List <Object>(); foreach (MeshFilter meshFilter in tDetails.FoundInMeshFilters) { FoundObjects.Add(meshFilter.gameObject); } SelectObjects(FoundObjects, ctrlPressed); } if (tDetails.FoundInSkinnedMeshRenderer.Count > 0) { if (GUILayout.Button(tDetails.FoundInSkinnedMeshRenderer.Count + " skinned mesh GO", GUILayout.Width(140))) { List <Object> FoundObjects = new List <Object> (); foreach (SkinnedMeshRenderer skinnedMeshRenderer in tDetails.FoundInSkinnedMeshRenderer) { FoundObjects.Add(skinnedMeshRenderer.gameObject); } SelectObjects(FoundObjects, ctrlPressed); } } else { GUI.color = new Color(defColor.r, defColor.g, defColor.b, 0.5f); GUILayout.Label(" 0 skinned mesh"); GUI.color = defColor; } GUILayout.EndHorizontal(); } } EditorGUILayout.EndScrollView(); } void ListMissing() { missingListScrollPos = EditorGUILayout.BeginScrollView(missingListScrollPos); foreach (MissingGraphic dMissing in MissingObjects) { GUILayout.BeginHorizontal(); if (GUILayout.Button(dMissing.name, GUILayout.Width(150))) { SelectObject(dMissing.Object, ctrlPressed); } GUILayout.Label("missing ", GUILayout.Width(48)); switch (dMissing.type) { case "mesh": GUI.color = new Color(0.8f, 0.8f, defColor.b, 1.0f); break; case "sprite": GUI.color = new Color(defColor.r, 0.8f, 0.8f, 1.0f); break; case "material": GUI.color = new Color(0.8f, defColor.g, 0.8f, 1.0f); break; } GUILayout.Label(dMissing.type); GUI.color = defColor; GUILayout.EndHorizontal(); } EditorGUILayout.EndScrollView(); } string FormatSizeString(int memSizeKB) { if (memSizeKB < 1024) { return("" + memSizeKB + "k"); } else { float memSizeMB = ((float)memSizeKB) / 1024.0f; return(memSizeMB.ToString("0.00") + "Mb"); } } TextureDetails FindTextureDetails(Texture tTexture) { foreach (TextureDetails tTextureDetails in ActiveTextures) { if (tTextureDetails.texture == tTexture) { return(tTextureDetails); } } return(null); } MaterialDetails FindMaterialDetails(Material tMaterial) { foreach (MaterialDetails tMaterialDetails in ActiveMaterials) { if (tMaterialDetails.material == tMaterial) { return(tMaterialDetails); } } return(null); } MeshDetails FindMeshDetails(Mesh tMesh) { foreach (MeshDetails tMeshDetails in ActiveMeshDetails) { if (tMeshDetails.mesh == tMesh) { return(tMeshDetails); } } return(null); } void CheckResources() { ActiveTextures.Clear(); ActiveMaterials.Clear(); ActiveMeshDetails.Clear(); MissingObjects.Clear(); thingsMissing = false; Renderer[] renderers = FindObjects <Renderer>(); MaterialDetails skyMat = new MaterialDetails(); skyMat.material = RenderSettings.skybox; skyMat.isSky = true; ActiveMaterials.Add(skyMat); //Debug.Log("Total renderers "+renderers.Length); foreach (Renderer renderer in renderers) { //Debug.Log("Renderer is "+renderer.name); foreach (Material material in renderer.sharedMaterials) { MaterialDetails tMaterialDetails = FindMaterialDetails(material); if (tMaterialDetails == null) { tMaterialDetails = new MaterialDetails(); tMaterialDetails.material = material; ActiveMaterials.Add(tMaterialDetails); } tMaterialDetails.FoundInRenderers.Add(renderer); } if (renderer is SpriteRenderer) { SpriteRenderer tSpriteRenderer = (SpriteRenderer)renderer; if (tSpriteRenderer.sprite != null) { var tSpriteTextureDetail = GetTextureDetail(tSpriteRenderer.sprite.texture, renderer); if (!ActiveTextures.Contains(tSpriteTextureDetail)) { ActiveTextures.Add(tSpriteTextureDetail); } } else if (tSpriteRenderer.sprite == null) { MissingGraphic tMissing = new MissingGraphic(); tMissing.Object = tSpriteRenderer.transform; tMissing.type = "sprite"; tMissing.name = tSpriteRenderer.transform.name; MissingObjects.Add(tMissing); thingsMissing = true; } } } if (IncludeGuiElements) { Graphic[] graphics = FindObjects <Graphic>(); foreach (Graphic graphic in graphics) { if (graphic.mainTexture) { var tSpriteTextureDetail = GetTextureDetail(graphic.mainTexture, graphic); if (!ActiveTextures.Contains(tSpriteTextureDetail)) { ActiveTextures.Add(tSpriteTextureDetail); } } if (graphic.materialForRendering) { MaterialDetails tMaterialDetails = FindMaterialDetails(graphic.materialForRendering); if (tMaterialDetails == null) { tMaterialDetails = new MaterialDetails(); tMaterialDetails.material = graphic.materialForRendering; tMaterialDetails.isgui = true; ActiveMaterials.Add(tMaterialDetails); } tMaterialDetails.FoundInGraphics.Add(graphic); } } } foreach (MaterialDetails tMaterialDetails in ActiveMaterials) { Material tMaterial = tMaterialDetails.material; if (tMaterial != null) { var dependencies = EditorUtility.CollectDependencies(new UnityEngine.Object[] { tMaterial }); foreach (Object obj in dependencies) { if (obj is Texture) { Texture tTexture = obj as Texture; var tTextureDetail = GetTextureDetail(tTexture, tMaterial, tMaterialDetails); tTextureDetail.isSky = tMaterialDetails.isSky; tTextureDetail.instance = tMaterialDetails.instance; tTextureDetail.isgui = tMaterialDetails.isgui; ActiveTextures.Add(tTextureDetail); } } //if the texture was downloaded, it won't be included in the editor dependencies if (tMaterial.HasProperty("_MainTex")) { if (tMaterial.mainTexture != null && !dependencies.Contains(tMaterial.mainTexture)) { var tTextureDetail = GetTextureDetail(tMaterial.mainTexture, tMaterial, tMaterialDetails); ActiveTextures.Add(tTextureDetail); } } } } MeshFilter[] meshFilters = FindObjects <MeshFilter>(); foreach (MeshFilter tMeshFilter in meshFilters) { Mesh tMesh = tMeshFilter.sharedMesh; if (tMesh != null) { MeshDetails tMeshDetails = FindMeshDetails(tMesh); if (tMeshDetails == null) { tMeshDetails = new MeshDetails(); tMeshDetails.mesh = tMesh; ActiveMeshDetails.Add(tMeshDetails); } tMeshDetails.FoundInMeshFilters.Add(tMeshFilter); } else if (tMesh == null && tMeshFilter.transform.GetComponent("TextContainer") == null) { MissingGraphic tMissing = new MissingGraphic(); tMissing.Object = tMeshFilter.transform; tMissing.type = "mesh"; tMissing.name = tMeshFilter.transform.name; MissingObjects.Add(tMissing); thingsMissing = true; } if (tMeshFilter.transform.GetComponent <MeshRenderer>().sharedMaterial == null) { MissingGraphic tMissing = new MissingGraphic(); tMissing.Object = tMeshFilter.transform; tMissing.type = "material"; tMissing.name = tMeshFilter.transform.name; MissingObjects.Add(tMissing); thingsMissing = true; } } SkinnedMeshRenderer[] skinnedMeshRenderers = FindObjects <SkinnedMeshRenderer>(); foreach (SkinnedMeshRenderer tSkinnedMeshRenderer in skinnedMeshRenderers) { Mesh tMesh = tSkinnedMeshRenderer.sharedMesh; if (tMesh != null) { MeshDetails tMeshDetails = FindMeshDetails(tMesh); if (tMeshDetails == null) { tMeshDetails = new MeshDetails(); tMeshDetails.mesh = tMesh; ActiveMeshDetails.Add(tMeshDetails); } tMeshDetails.FoundInSkinnedMeshRenderer.Add(tSkinnedMeshRenderer); } else if (tMesh == null) { MissingGraphic tMissing = new MissingGraphic(); tMissing.Object = tSkinnedMeshRenderer.transform; tMissing.type = "mesh"; tMissing.name = tSkinnedMeshRenderer.transform.name; MissingObjects.Add(tMissing); thingsMissing = true; } if (tSkinnedMeshRenderer.sharedMaterial == null) { MissingGraphic tMissing = new MissingGraphic(); tMissing.Object = tSkinnedMeshRenderer.transform; tMissing.type = "material"; tMissing.name = tSkinnedMeshRenderer.transform.name; MissingObjects.Add(tMissing); thingsMissing = true; } } if (IncludeSpriteAnimations) { Animator[] animators = FindObjects <Animator>(); foreach (Animator anim in animators) { #if UNITY_4_6 || UNITY_4_5 || UNITY_4_4 || UNITY_4_3 UnityEditorInternal.AnimatorController ac = anim.runtimeAnimatorController as UnityEditorInternal.AnimatorController; #elif UNITY_5 UnityEditor.Animations.AnimatorController ac = anim.runtimeAnimatorController as UnityEditor.Animations.AnimatorController; #endif //Skip animators without layers, this can happen if they don't have an animator controller. if (!ac || ac.layers == null || ac.layers.Length == 0) { continue; } for (int x = 0; x < anim.layerCount; x++) { #if UNITY_4_6 || UNITY_4_5 || UNITY_4_4 || UNITY_4_3 UnityEditorInternal.StateMachine sm = ac.GetLayer(x).stateMachine; int cnt = sm.stateCount; #elif UNITY_5 UnityEditor.Animations.AnimatorStateMachine sm = ac.layers[x].stateMachine; int cnt = sm.states.Length; #endif for (int i = 0; i < cnt; i++) { #if UNITY_4_6 || UNITY_4_5 || UNITY_4_4 || UNITY_4_3 UnityEditorInternal.State state = sm.GetState(i); Motion m = state.GetMotion(); #elif UNITY_5 UnityEditor.Animations.AnimatorState state = sm.states[i].state; Motion m = state.motion; #endif if (m != null) { AnimationClip clip = m as AnimationClip; if (clip != null) { EditorCurveBinding[] ecbs = AnimationUtility.GetObjectReferenceCurveBindings(clip); foreach (EditorCurveBinding ecb in ecbs) { if (ecb.propertyName == "m_Sprite") { foreach (ObjectReferenceKeyframe keyframe in AnimationUtility.GetObjectReferenceCurve(clip, ecb)) { Sprite tSprite = keyframe.value as Sprite; if (tSprite != null) { var tTextureDetail = GetTextureDetail(tSprite.texture, anim); if (!ActiveTextures.Contains(tTextureDetail)) { ActiveTextures.Add(tTextureDetail); } } } } } } } } } } } if (IncludeScriptReferences) { MonoBehaviour[] scripts = FindObjects <MonoBehaviour>(); foreach (MonoBehaviour script in scripts) { BindingFlags flags = BindingFlags.Public | BindingFlags.Instance; // only public non-static fields are bound to by Unity. FieldInfo[] fields = script.GetType().GetFields(flags); foreach (FieldInfo field in fields) { System.Type fieldType = field.FieldType; if (fieldType == typeof(Sprite)) { Sprite tSprite = field.GetValue(script) as Sprite; if (tSprite != null) { var tSpriteTextureDetail = GetTextureDetail(tSprite.texture, script); if (!ActiveTextures.Contains(tSpriteTextureDetail)) { ActiveTextures.Add(tSpriteTextureDetail); } } } if (fieldType == typeof(Mesh)) { Mesh tMesh = field.GetValue(script) as Mesh; if (tMesh != null) { MeshDetails tMeshDetails = FindMeshDetails(tMesh); if (tMeshDetails == null) { tMeshDetails = new MeshDetails(); tMeshDetails.mesh = tMesh; tMeshDetails.instance = true; ActiveMeshDetails.Add(tMeshDetails); } } } if (fieldType == typeof(Material)) { Material tMaterial = field.GetValue(script) as Material; if (tMaterial != null) { MaterialDetails tMatDetails = FindMaterialDetails(tMaterial); if (tMatDetails == null) { tMatDetails = new MaterialDetails(); tMatDetails.instance = true; tMatDetails.material = tMaterial; if (!ActiveMaterials.Contains(tMatDetails)) { ActiveMaterials.Add(tMatDetails); } } if (tMaterial.mainTexture) { var tSpriteTextureDetail = GetTextureDetail(tMaterial.mainTexture); if (!ActiveTextures.Contains(tSpriteTextureDetail)) { ActiveTextures.Add(tSpriteTextureDetail); } } var dependencies = EditorUtility.CollectDependencies(new UnityEngine.Object[] { tMaterial }); foreach (Object obj in dependencies) { if (obj is Texture) { Texture tTexture = obj as Texture; var tTextureDetail = GetTextureDetail(tTexture, tMaterial, tMatDetails); if (!ActiveTextures.Contains(tTextureDetail)) { ActiveTextures.Add(tTextureDetail); } } } } } } } } TotalTextureMemory = 0; foreach (TextureDetails tTextureDetails in ActiveTextures) { TotalTextureMemory += tTextureDetails.memSizeKB; } TotalMeshVertices = 0; foreach (MeshDetails tMeshDetails in ActiveMeshDetails) { TotalMeshVertices += tMeshDetails.mesh.vertexCount; } // Sort by size, descending ActiveTextures.Sort(delegate(TextureDetails details1, TextureDetails details2) { return(details2.memSizeKB - details1.memSizeKB); }); ActiveTextures = ActiveTextures.Distinct().ToList(); ActiveMeshDetails.Sort(delegate(MeshDetails details1, MeshDetails details2) { return(details2.mesh.vertexCount - details1.mesh.vertexCount); }); collectedInPlayingMode = Application.isPlaying; }