/// <summary> /// Invoke <see cref="OnMeshFinalised"/> /// </summary> /// <param name="meshDetails"></param> protected void NotifyMeshFinalised(MeshDetails meshDetails) { if (OnMeshFinalised != null) { OnMeshFinalised(meshDetails); } }
/// <summary> /// Invoke <see cref="OnMeshAdded"/> /// </summary> /// <param name="meshEntry"></param> protected void NotifyMeshAdded(MeshDetails meshEntry) { if (OnMeshAdded != null) { OnMeshAdded(meshEntry); } }
/// <summary> /// Query details of mesh resource using <paramref name="meshId"/>. /// </summary> /// <param name="meshId">ID of the resource of interest.</param> /// <returns>The mesh details, or null when <paramref name="meshId"/> is unknown.</returns> public MeshDetails GetEntry(uint meshId) { MeshDetails meshDetails = null; _meshes.TryGetValue(meshId, out meshDetails); return(meshDetails); }
/// <summary> /// Handles <see cref="MeshDestroyMessage"/> /// </summary> /// <param name="packet"></param> /// <param name="reader"></param> /// <returns></returns> /// Emits <see cref="OnMeshRemoved"/>. protected Error DestroyMesh(PacketBuffer packet, BinaryReader reader) { MeshDestroyMessage msg = new MeshDestroyMessage(); if (!msg.Read(reader)) { return(new Error(ErrorCode.MalformedMessage, MeshDestroyMessage.MessageID)); } if (!_meshes.ContainsKey(msg.MeshID)) { return(new Error(ErrorCode.InvalidObjectID, msg.MeshID)); } MeshDetails meshEntry = _meshes[msg.MeshID]; NotifyMeshRemoved(meshEntry); _meshes.Remove(msg.MeshID); if (meshEntry.Mesh != null) { meshEntry.Mesh.ReleaseBuffers(); } return(new Error()); }
/** * Helper function to accept a gameobject which will transform the plane * approprietly before the slice occurs * See -> Slice(Mesh, Plane) for more info */ public static SlicedHull Slice(GameObject obj, Plane pl, TextureRegion crossRegion, Material crossMaterial) { MeshDetails md = GetMeshDetails(obj); if (!md.valid) { return(null); } // crossIndex - we need to find the index of the material for the cross section. // default to the end of the array // for cases where the sliced material is null, we will append the cross section to the end // of the submesh array, this is because the application may want to set/change the material // after slicing has occured, so we don't assume anything if (crossMaterial != null) { for (int i = 0; i < md.crossIndex; i++) { if (md.materials[i] == crossMaterial) { md.crossIndex = i; break; } } } return(Slice(md.mesh, pl, crossRegion, md.crossIndex)); }
/// <summary> /// Create a serialiser for the given mesh details. /// </summary> /// <param name="details">The mesh to serialise.</param> public MeshSerialiser(MeshDetails details) { Details = details; RenderMesh mesh = details.Mesh; // Migrate data fields. ID = details.ID; Maths.Matrix4 transform = Maths.Rotation.ToMatrix4(Maths.QuaternionExt.FromUnity(details.LocalRotation)); transform.Translation = Maths.Vector3Ext.FromUnity(details.LocalPosition); transform.ApplyScaling(Maths.Vector3Ext.FromUnity(details.LocalScale)); Transform = transform; Tint = Maths.ColourExt.FromUnity(details.Tint).Value; DrawType = (byte)mesh.DrawType; // TODO: (KS) track this flag. Mind you, the normals will have been calculated by now... CalculateNormals = false;//Details.Builder.CalculateNormals; MeshComponentFlag components = 0; // Copy arrays into the correct format. _vertices = Maths.Vector3Ext.FromUnity(mesh.Vertices); _indices = new int[mesh.IndexCount]; Array.Copy(mesh.Indices, _indices, _indices.Length); if (mesh.HasNormals) { _normals = Maths.Vector3Ext.FromUnity(mesh.Normals); } if (mesh.HasUVs) { _uvs = Maths.Vector2Ext.FromUnity(mesh.UVs); } if (mesh.HasColours) { _colours = new uint[mesh.VertexCount]; Array.Copy(mesh.Colours, _colours, _colours.Length); } if (mesh.VertexCount > 0) { components |= MeshComponentFlag.Vertex; } if (mesh.IndexCount > 0) { components |= MeshComponentFlag.Index; } if (_normals != null) { components |= MeshComponentFlag.Normal; } if (_uvs != null) { components |= MeshComponentFlag.UV; } if (_colours != null) { components |= MeshComponentFlag.Colour; } Components = components; }
/// <summary> /// Handles <see cref="MeshDestroyMessage"/> /// </summary> /// <param name="packet"></param> /// <param name="reader"></param> /// <returns></returns> /// Emits <see cref="OnMeshRemoved"/>. protected Error DestroyMesh(PacketBuffer packet, BinaryReader reader) { MeshDestroyMessage msg = new MeshDestroyMessage(); if (!msg.Read(reader)) { return(new Error(ErrorCode.MalformedMessage, MeshDestroyMessage.MessageID)); } if (!_meshes.ContainsKey(msg.MeshID)) { return(new Error(ErrorCode.InvalidObjectID, msg.MeshID)); } MeshDetails meshDetails = _meshes[msg.MeshID]; NotifyMeshRemoved(meshDetails); _meshes.Remove(msg.MeshID); if (meshDetails.FinalMeshes != null) { foreach (Mesh mesh in meshDetails.FinalMeshes) { Mesh.Destroy(mesh); } } return(new Error()); }
/// <summary> /// Invoke <see cref="OnMeshRemoved"/> /// </summary> /// <param name="meshDetails"></param> protected void NotifyMeshRemoved(MeshDetails meshDetails) { if (OnMeshRemoved != null) { OnMeshRemoved(meshDetails); } }
public void DisplayMesh(MeshDetails meshDetails) { meshFilter.sharedMesh = meshDetails.BuildMesh(); textureRenderer.gameObject.SetActive(false); meshFilter.gameObject.SetActive(true); }
// Helper function for edgeloop functionality public static List <Vector3> EdgeLoop(GameObject obj, Plane pl) { MeshDetails md = GetMeshDetails(obj); if (!md.valid) { return(null); } return(EdgeLoop(md.mesh, pl)); }
/// <summary> /// Serialise messages to generate <paramref name="mesh"/>. /// </summary> /// <param name="mesh">The mesh of interest.</param> /// <param name="packet">Packet buffer to compose messages in</param> /// <param name="writer">Writer to export completed message packets to.</param> /// <returns></returns> /// <remarks> /// Writes: /// <list type="bullet"> /// <item><see cref="MeshCreateMessage"/></item> /// <item><see cref="MeshComponentMessage"/> for each component type from /// <see cref="MeshMessageType"/></item> /// <item><see cref="MeshFinaliseMessage"/> only when <paramref name="mesh"/> is already /// finalised.</item> /// </list> /// </remarks> protected Error Serialise(MeshDetails mesh, PacketBuffer packet, BinaryWriter writer) { // First write a create message. MeshCreateMessage msg = new MeshCreateMessage(); packet.Reset((ushort)RoutingID, (ushort)MeshCreateMessage.MessageID); msg.MeshID = mesh.ID; msg.VertexCount = (uint)mesh.Builder.VertexCount; msg.IndexCount = (uint)(mesh.Builder.ExplicitIndices ? mesh.Builder.IndexCount : 0); msg.DrawType = mesh.DrawType; msg.Attributes.X = mesh.LocalPosition.x; msg.Attributes.Y = mesh.LocalPosition.y; msg.Attributes.Z = mesh.LocalPosition.z; msg.Attributes.RotationX = mesh.LocalRotation.x; msg.Attributes.RotationY = mesh.LocalRotation.y; msg.Attributes.RotationZ = mesh.LocalRotation.z; msg.Attributes.RotationW = mesh.LocalRotation.w; msg.Attributes.ScaleX = mesh.LocalScale.x; msg.Attributes.ScaleY = mesh.LocalScale.y; msg.Attributes.ScaleZ = mesh.LocalScale.z; msg.Attributes.Colour = ShapeComponent.ConvertColour(mesh.Tint); msg.Write(packet); if (!packet.FinalisePacket()) { return(new Error(ErrorCode.SerialisationFailure)); } packet.ExportTo(writer); // Now use the MeshResource methods to complete serialisation. MeshSerialiser serialiser = new MeshSerialiser(mesh); TransferProgress prog = new TransferProgress(); prog.Reset(); while (!prog.Complete) { serialiser.Transfer(packet, 0, ref prog); if (!packet.FinalisePacket()) { return(new Error(ErrorCode.SerialisationFailure)); } packet.ExportTo(writer); } return(new Error()); }
/// <summary> /// Handles <see cref="MeshCreateMessage"/> /// </summary> /// <param name="packet"></param> /// <param name="reader"></param> /// <returns></returns> /// <remarks> /// Emits <see cref="OnMeshAdded"/>. /// </remarks> protected Error CreateMesh(PacketBuffer packet, BinaryReader reader) { MeshCreateMessage msg = new MeshCreateMessage(); if (!msg.Read(reader)) { return(new Error(ErrorCode.MalformedMessage, MeshCreateMessage.MessageID)); } if (_meshes.ContainsKey(msg.MeshID)) { return(new Error(ErrorCode.DuplicateShape, msg.MeshID)); } MeshDetails meshDetails = new MeshDetails(); meshDetails.VertexCount = (int)msg.VertexCount; meshDetails.IndexCount = (int)msg.IndexCount; meshDetails.DrawType = msg.DrawType; switch (msg.DrawType) { case (byte)MeshDrawType.Points: // No break. case (byte)MeshDrawType.Voxels: meshDetails.Builder.Topology = MeshTopology.Points; break; case (byte)MeshDrawType.Lines: meshDetails.Builder.Topology = MeshTopology.Lines; break; case (byte)MeshDrawType.Triangles: meshDetails.Builder.Topology = MeshTopology.Triangles; break; default: return(new Error(ErrorCode.UnsupportedFeature, msg.DrawType)); } meshDetails.ID = msg.MeshID; meshDetails.LocalPosition = new Vector3(msg.Attributes.X, msg.Attributes.Y, msg.Attributes.Z); meshDetails.LocalRotation = new Quaternion(msg.Attributes.RotationX, msg.Attributes.RotationY, msg.Attributes.RotationZ, msg.Attributes.RotationW); meshDetails.LocalScale = new Vector3(msg.Attributes.ScaleX, msg.Attributes.ScaleY, msg.Attributes.ScaleZ); meshDetails.Tint = ShapeComponent.ConvertColour(msg.Attributes.Colour); meshDetails.Finalised = false; _meshes.Add(meshDetails.ID, meshDetails); NotifyMeshAdded(meshDetails); return(new Error()); }
MeshDetails TryAddActiveMesh(Mesh m) { if (m != null) { MeshDetails details = FindMeshDetails(m); if (details == null) { details = new MeshDetails(m); ActiveMeshDetails.Add(details); } return(details); } return(null); }
void AddToActiveMesh(MeshDetails td) { bool find = false; foreach (MeshDetails t in ActiveMeshDetails) { if (t == td) { find = true; break; } } if (find == false) { ActiveMeshDetails.Add(td); } }
/// <summary> /// Create a serialiser for the given mesh details. /// </summary> /// <param name="details">The mesh to serialise.</param> public MeshSerialiser(MeshDetails details) { Details = details; // Migrate data fields. ID = details.ID; Maths.Matrix4 transform = Maths.Rotation.ToMatrix4(Maths.QuaternionExt.FromUnity(details.LocalRotation)); transform.Translation = Maths.Vector3Ext.FromUnity(details.LocalPosition); transform.ApplyScaling(Maths.Vector3Ext.FromUnity(details.LocalScale)); Transform = transform; Tint = Maths.ColourExt.FromUnity(details.Tint).Value; DrawType = Details.DrawType; CalculateNormals = Details.Builder.CalculateNormals; MeshComponentFlag components = 0; // Copy arrays into the correct format. _vertices = Maths.Vector3Ext.FromUnity(details.Builder.Vertices); _normals = Maths.Vector3Ext.FromUnity(details.Builder.Normals); _uvs = Maths.Vector2Ext.FromUnity(details.Builder.UVs); _colours = Maths.ColourExt.FromUnityUInts(details.Builder.Colours); if (details.VertexCount > 0) { components |= MeshComponentFlag.Vertex; } if (details.Builder.ExplicitIndices) { components |= MeshComponentFlag.Index; } if (_colours != null && _colours.Length > 0) { components |= MeshComponentFlag.Colour; } if (_normals != null && _normals.Length > 0) { components |= MeshComponentFlag.Normal; } if (_uvs != null && _uvs.Length > 0) { components |= MeshComponentFlag.UV; } Components = components; }
public void Add(VoxelFace face, Material material) { foreach (var meshDetails in meshDetailsList) { if (meshDetails.material == material && meshDetails.faces.Count < MAX_FACES_PER_MESH) { meshDetails.faces.Add(face); meshDetails.dirty = true; return; } } var newMeshDetails = new MeshDetails(material); newMeshDetails.faces.Add(face); meshDetailsList.Add(newMeshDetails); }
/** * Return mesh details or null if the supplied gameobject cannot be processed. */ private static MeshDetails GetMeshDetails(GameObject obj) { MeshDetails md = new MeshDetails(); if (!IsGameObjectOk(obj)) { md.valid = false; return(md); } md.filter = obj.GetComponent <MeshFilter>(); md.renderer = obj.GetComponent <MeshRenderer>(); md.materials = md.renderer.sharedMaterials; md.mesh = md.filter.sharedMesh; md.submeshCount = md.mesh.subMeshCount; md.crossIndex = md.materials.Length; md.valid = true; return(md); }
/// <summary> /// Handles <see cref="MeshCreateMessage"/> /// </summary> /// <param name="packet"></param> /// <param name="reader"></param> /// <returns></returns> /// <remarks> /// Emits <see cref="OnMeshAdded"/>. /// </remarks> protected Error CreateMesh(PacketBuffer packet, BinaryReader reader) { MeshCreateMessage msg = new MeshCreateMessage(); if (!msg.Read(reader)) { return(new Error(ErrorCode.MalformedMessage, MeshCreateMessage.MessageID)); } if (_meshes.ContainsKey(msg.MeshID)) { return(new Error(ErrorCode.DuplicateShape, msg.MeshID)); } if (msg.DrawType < 0 || msg.DrawType > Enum.GetValues(typeof(MeshDrawType)).Length) { return(new Error(ErrorCode.UnsupportedFeature, msg.DrawType)); } MeshDetails meshEntry = new MeshDetails(); RenderMesh renderMesh = new RenderMesh((MeshDrawType)msg.DrawType, (int)msg.VertexCount, (int)msg.IndexCount); meshEntry.Mesh = renderMesh; meshEntry.ID = msg.MeshID; meshEntry.LocalPosition = new Vector3(msg.Attributes.X, msg.Attributes.Y, msg.Attributes.Z); meshEntry.LocalRotation = new Quaternion(msg.Attributes.RotationX, msg.Attributes.RotationY, msg.Attributes.RotationZ, msg.Attributes.RotationW); meshEntry.LocalScale = new Vector3(msg.Attributes.ScaleX, msg.Attributes.ScaleY, msg.Attributes.ScaleZ); meshEntry.Tint = Maths.ColourExt.ToUnity32(new Maths.Colour(msg.Attributes.Colour)); meshEntry.Finalised = false; _meshes.Add(meshEntry.ID, meshEntry); NotifyMeshAdded(meshEntry); return(new Error()); }
void CheckResources() { ActiveTextures.Clear(); ActiveMaterials.Clear(); ActiveMeshDetails.Clear(); Renderer[] renderers = (Renderer[])FindObjectsOfType(typeof(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); } } foreach (MaterialDetails tMaterialDetails in ActiveMaterials) { Material tMaterial = tMaterialDetails.material; 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 = (MeshFilter[])FindObjectsOfType(typeof(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 = (SkinnedMeshRenderer[])FindObjectsOfType(typeof(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); } } 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); }); }
void CheckResources() { ActiveTextures.Clear(); ActiveMaterials.Clear(); ActiveMeshDetails.Clear(); Renderer[] renderers = (Renderer[])FindObjectsOfType(typeof(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); } } foreach (MaterialDetails tMaterialDetails in ActiveMaterials) { Material tMaterial = tMaterialDetails.material; foreach (Object obj in EditorUtility.CollectDependencies(new UnityEngine.Object[] { tMaterial })) { if (obj is Texture) { Texture tTexture = obj as Texture; TextureDetails tTextureDetails = FindTextureDetails(tTexture); if (tTextureDetails == null) { tTextureDetails = new TextureDetails(); tTextureDetails.texture = tTexture; tTextureDetails.isCubeMap = tTexture is Cubemap; int memSize = CalculateTextureSizeBytes(tTexture); tTextureDetails.memSizeKB = memSize / 1024; TextureFormat tFormat = TextureFormat.RGBA32; int tMipMapCount = 1; if (tTexture is Texture2D) { tFormat = (tTexture as Texture2D).format; tMipMapCount = (tTexture as Texture2D).mipmapCount; } if (tTexture is Cubemap) { tFormat = (tTexture as Cubemap).format; } tTextureDetails.format = tFormat; tTextureDetails.mipMapCount = tMipMapCount; ActiveTextures.Add(tTextureDetails); } tTextureDetails.FoundInMaterials.Add(tMaterial); foreach (Renderer renderer in tMaterialDetails.FoundInRenderers) { if (!tTextureDetails.FoundInRenderers.Contains(renderer)) { tTextureDetails.FoundInRenderers.Add(renderer); } } } } } MeshFilter[] meshFilters = (MeshFilter[])FindObjectsOfType(typeof(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 = (SkinnedMeshRenderer[])FindObjectsOfType(typeof(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); } } 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); }); }
private void OnMeshDetailsReceived(MeshDetails meshDetails) { meshFilter.mesh = meshDetails.BuildMesh(); }
public static MeshDetails BuildMesh(float[,] noiseArea, int lod, AreaDetails areaDetails) { int lodIncrement = lod == 0 ? 1 : lod * 2; int verticesPerLine = areaDetails.verticesPerLine; Vector2 topLeftCorner = new Vector2(-1, 1) * areaDetails.resolution / 2f; MeshDetails meshDetails = new MeshDetails(verticesPerLine, areaDetails.useFlatshading, lodIncrement); int[,] vertexIndexes = new int[verticesPerLine, verticesPerLine]; int meshVertexIndex = 0; int outsideVertexIndex = -1; for (int yIndex = 0; yIndex < verticesPerLine; yIndex++) { for (int xIndex = 0; xIndex < verticesPerLine; xIndex++) { bool isOutsideVertex = yIndex == 0 || xIndex == 0 || yIndex == verticesPerLine - 1 || xIndex == verticesPerLine - 1; bool isUselessVertex = xIndex > 2 && xIndex < verticesPerLine - 3 && yIndex > 2 && yIndex < verticesPerLine - 3 && ((xIndex - 2) % lodIncrement != 0 || (yIndex - 2) % lodIncrement != 0); if (isOutsideVertex) { vertexIndexes[xIndex, yIndex] = outsideVertexIndex; outsideVertexIndex--; } else if (!isUselessVertex) { vertexIndexes[xIndex, yIndex] = meshVertexIndex; meshVertexIndex++; } } } for (int yIndex = 0; yIndex < verticesPerLine; yIndex++) { for (int xIndex = 0; xIndex < verticesPerLine; xIndex++) { bool isUselessVertex = xIndex > 2 && xIndex < verticesPerLine - 3 && yIndex > 2 && yIndex < verticesPerLine - 3 && ((xIndex - 2) % lodIncrement != 0 || (yIndex - 2) % lodIncrement != 0); if (!isUselessVertex) { bool isOutsideVertex = yIndex == 0 || xIndex == 0 || yIndex == verticesPerLine - 1 || xIndex == verticesPerLine - 1; bool isFrontierVertex = (yIndex == 1 || yIndex == verticesPerLine - 2 || xIndex == 1 || xIndex == verticesPerLine - 2) && !isOutsideVertex; bool isMainVertex = (xIndex - 2) % lodIncrement == 0 && (yIndex - 2) % lodIncrement == 0 && !isOutsideVertex && !isFrontierVertex; bool isInsideVertex = (yIndex == 2 || yIndex == verticesPerLine - 3 || xIndex == 2 || xIndex == verticesPerLine - 3) && !isOutsideVertex && !isFrontierVertex && !isMainVertex; int vertexIndex = vertexIndexes[xIndex, yIndex]; Vector2 uv = new Vector2(xIndex - 1, yIndex - 1) / (verticesPerLine - 3); Vector2 vertexCoords = topLeftCorner + new Vector2(uv.x, -uv.y) * areaDetails.resolution; float height = noiseArea[xIndex, yIndex]; if (isInsideVertex) { bool isVertical = xIndex == 2 || xIndex == verticesPerLine - 3; int distanceToMainVertexA = (isVertical ? yIndex - 2 : xIndex - 2) % lodIncrement; int distanceToMainVertexB = lodIncrement - distanceToMainVertexA; float distancePercentage = distanceToMainVertexA / (float)lodIncrement; float heightVertexA = noiseArea[isVertical ? xIndex : xIndex - distanceToMainVertexA, isVertical ? yIndex - distanceToMainVertexA : yIndex]; float heightVertexB = noiseArea[isVertical ? xIndex : xIndex + distanceToMainVertexB, isVertical ? yIndex + distanceToMainVertexB : yIndex]; height = heightVertexA * (1 - distancePercentage) + heightVertexB * distancePercentage; } meshDetails.AddVertex(new Vector3(vertexCoords.x, height, vertexCoords.y), uv, vertexIndex); bool createTriangle = xIndex < verticesPerLine - 1 && yIndex < verticesPerLine - 1 && (!isInsideVertex || (xIndex != 2 && yIndex != 2)); if (createTriangle) { int currentIncrement = (isMainVertex && xIndex != verticesPerLine - 3 && yIndex != verticesPerLine - 3) ? lodIncrement : 1; int vertexAIndex = vertexIndexes[xIndex, yIndex]; int vertexBIndex = vertexIndexes[xIndex + currentIncrement, yIndex]; int vertexCIndex = vertexIndexes[xIndex, yIndex + currentIncrement]; int vertexDIndex = vertexIndexes[xIndex + currentIncrement, yIndex + currentIncrement]; meshDetails.AddTriangle(vertexAIndex, vertexDIndex, vertexCIndex); meshDetails.AddTriangle(vertexDIndex, vertexAIndex, vertexBIndex); } } } } meshDetails.BuildLighting(); return(meshDetails); }
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; }
void CheckResources() { ActiveTextures.Clear(); ActiveMaterials.Clear(); ActiveMeshDetails.Clear(); Renderer[] renderers = (Renderer[]) FindObjectsOfType(typeof(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); } } foreach (MaterialDetails tMaterialDetails in ActiveMaterials) { Material tMaterial=tMaterialDetails.material; foreach (Object obj in EditorUtility.CollectDependencies(new UnityEngine.Object[] {tMaterial})) { if (obj is Texture) { Texture tTexture=obj as Texture; TextureDetails tTextureDetails=FindTextureDetails(tTexture); if (tTextureDetails==null) { tTextureDetails=new TextureDetails(); tTextureDetails.texture=tTexture; tTextureDetails.isCubeMap=tTexture is Cubemap; int memSize=CalculateTextureSizeBytes(tTexture); tTextureDetails.memSizeKB=memSize/1024; TextureFormat tFormat=TextureFormat.RGBA32; int tMipMapCount=1; if (tTexture is Texture2D) { tFormat=(tTexture as Texture2D).format; tMipMapCount=(tTexture as Texture2D).mipmapCount; } if (tTexture is Cubemap) { tFormat=(tTexture as Cubemap).format; } tTextureDetails.format=tFormat; tTextureDetails.mipMapCount=tMipMapCount; ActiveTextures.Add(tTextureDetails); } tTextureDetails.FoundInMaterials.Add(tMaterial); foreach (Renderer renderer in tMaterialDetails.FoundInRenderers) { if (!tTextureDetails.FoundInRenderers.Contains( renderer)) tTextureDetails.FoundInRenderers.Add(renderer); } } } } MeshFilter[] meshFilters = (MeshFilter[]) FindObjectsOfType(typeof(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 = (SkinnedMeshRenderer[]) FindObjectsOfType(typeof(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); } } 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;}); }
void CheckResources() { ActiveTextures.Clear (); ActiveMaterials.Clear (); ActiveMeshDetails.Clear (); Renderer[] renderers = (Renderer[])FindObjectsOfType ( typeof ( 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 ); } } foreach ( MaterialDetails tMaterialDetails in ActiveMaterials ) { Material tMaterial = tMaterialDetails.material; 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 ); } CheckForSpriteTextures ( tMaterialDetails ); } MeshFilter[] meshFilters = (MeshFilter[])FindObjectsOfType ( typeof ( 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 = (SkinnedMeshRenderer[])FindObjectsOfType ( typeof ( 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 ); } } 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; } ); }
// 调用时机:目录列表改变,检索模式改变 void checkResources() { string[] paths = null; // TODO: inputPathList从未被使用,一直使用的是当前的inputPath paths = GetFilePaths <Material>(inputPath, searchOption); //目前只会找到有Material引用的自定义Texture和Shader //对于被引用的默认Texture和Shader也进行显示 //找到material使用的texture和shader foreach (string path in paths) { Material material = (Material)AssetDatabase.LoadAssetAtPath(path, typeof(Material)); if (material != null) { MaterialDetails tMaterialDetails = new MaterialDetails(); tMaterialDetails.FoundInGameObjects = new List <string>(); tMaterialDetails.name = material.name; tMaterialDetails.path = path; // 对于缩略图进行深拷贝 Texture2D preview = AssetPreview.GetAssetPreview(material); tMaterialDetails.preview = new Texture2D(preview.width, preview.height); tMaterialDetails.preview.SetPixels32(preview.GetPixels32()); tMaterialDetails.preview.Apply(); AllMaterials.Add(tMaterialDetails); foreach (Object obj in EditorUtility.CollectDependencies(new UnityEngine.Object[] { material })) { string p = AssetDatabase.GetAssetPath(obj); if (p == defaultPath) { p = defaultPath + "::" + obj.name; } if (obj is Texture) { Texture tTexture = (Texture)obj; int check = 0; foreach (TextureDetails details in AllTextures) { if (details.path == p) { check = 1; details.FoundInMaterials.Add(path); break; } } if (check == 0) { TextureDetails tTextureDetails = new TextureDetails(); tTextureDetails.FoundInMaterials = new List <string>(); tTextureDetails.name = tTexture.name; tTextureDetails.path = p; tTextureDetails.preview = AssetPreview.GetMiniThumbnail(tTexture); tTextureDetails.memSizeBytes = TextureDetails.CalculateTextureSizeBytes(tTexture); tTextureDetails.width = tTexture.width; tTextureDetails.height = tTexture.height; tTextureDetails.FoundInMaterials.Add(path); AllTextures.Add(tTextureDetails); } } else if (obj is Shader) { Shader tShader = (Shader)obj; int check = 0; foreach (ShaderDetails details in AllShaders) { if (details.path == p) { check = 1; details.FoundInMaterials.Add(path); break; } } if (check == 0) { ShaderDetails tShaderDetails = new ShaderDetails(); tShaderDetails.FoundInMaterials = new List <string>(); tShaderDetails.FoundInGameObjects = new List <string>(); tShaderDetails.name = tShader.name; tShaderDetails.path = p; tShaderDetails.FoundInMaterials.Add(path); AllShaders.Add(tShaderDetails); } } } } } paths = GetFilePaths <Mesh>(inputPath, searchOption); foreach (string path in paths) { Mesh mesh = (Mesh)AssetDatabase.LoadAssetAtPath(path, typeof(Mesh)); if (mesh != null) { MeshDetails tMeshDetails = new MeshDetails(); tMeshDetails.FoundInGameObjects = new List <string>(); tMeshDetails.name = mesh.name; tMeshDetails.path = path; tMeshDetails.preview = AssetPreview.GetAssetPreview(mesh); tMeshDetails.vertexCount = mesh.vertexCount; tMeshDetails.triangles = mesh.triangles.Length; AllMeshes.Add(tMeshDetails); } } paths = GetFilePaths <AudioClip>(inputPath, searchOption); foreach (string path in paths) { AudioClip clip = (AudioClip)AssetDatabase.LoadAssetAtPath(path, typeof(AudioClip)); if (clip != null) { SoundDetails tSoundDetails = new SoundDetails(); tSoundDetails.FoundInGameObjects = new List <string>(); tSoundDetails.name = clip.name; tSoundDetails.path = path; tSoundDetails.preview = AssetPreview.GetAssetPreview(clip); AllSounds.Add(tSoundDetails); } } paths = GetFilePaths <MonoScript>(inputPath, searchOption); foreach (string path in paths) { MonoScript script = (MonoScript)AssetDatabase.LoadAssetAtPath(path, typeof(MonoScript)); if (script != null) { ScriptDetails tScriptDetails = new ScriptDetails(); tScriptDetails.FoundInGameObjects = new List <string>(); tScriptDetails.name = script.name; tScriptDetails.path = path; AllScripts.Add(tScriptDetails); } } // TODO: 优化查重算法 // TODO: 目前只能找到存为prefab的GameObject // 找到GameObject引用的内建资源也能找到 paths = GetFilePaths <GameObject>(inputPath, searchOption); foreach (string path in paths) { GameObject gameObject = (GameObject)AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)); if (gameObject != null) { foreach (Object obj in EditorUtility.CollectDependencies(new UnityEngine.Object[] { gameObject })) { string p = AssetDatabase.GetAssetPath(obj); if (p == defaultPath) { p = defaultPath + "::" + obj.name; } if (obj is Material) { Material material = (Material)obj; int check = 1; foreach (MaterialDetails details in AllMaterials) { if (details.path == p) { check = 0; details.FoundInGameObjects.Add(path); break; } } if (check == 1) { MaterialDetails tMaterialDetails = new MaterialDetails(); tMaterialDetails.FoundInGameObjects = new List <string>(); tMaterialDetails.name = material.name; tMaterialDetails.path = path; // 对于material的缩略图进行深拷贝 Texture2D preview = AssetPreview.GetAssetPreview(material); tMaterialDetails.preview = new Texture2D(preview.width, preview.height); tMaterialDetails.preview.SetPixels32(preview.GetPixels32()); tMaterialDetails.preview.Apply(); tMaterialDetails.FoundInGameObjects.Add(path); AllMaterials.Add(tMaterialDetails); } } else if (obj is Mesh) { Mesh mesh = (Mesh)obj; int check = 1; foreach (MeshDetails details in AllMeshes) { if (details.path == p) { check = 0; details.FoundInGameObjects.Add(path); break; } } if (check == 1) { MeshDetails tMeshDetails = new MeshDetails(); tMeshDetails.FoundInGameObjects = new List <string>(); tMeshDetails.name = mesh.name; tMeshDetails.path = path; tMeshDetails.preview = AssetPreview.GetAssetPreview(mesh); tMeshDetails.vertexCount = mesh.vertexCount; tMeshDetails.triangles = mesh.triangles.Length; tMeshDetails.FoundInGameObjects.Add(path); AllMeshes.Add(tMeshDetails); } } else if (obj is AudioClip) { AudioClip clip = (AudioClip)obj; int check = 1; foreach (SoundDetails details in AllSounds) { if (details.path == p) { check = 0; details.FoundInGameObjects.Add(path); break; } } if (check == 1) { SoundDetails tSoundDetails = new SoundDetails(); tSoundDetails.FoundInGameObjects = new List <string>(); tSoundDetails.name = clip.name; tSoundDetails.path = p; tSoundDetails.preview = AssetPreview.GetAssetPreview(clip); tSoundDetails.FoundInGameObjects.Add(path); AllSounds.Add(tSoundDetails); } } else if (obj is MonoScript) { MonoScript script = (MonoScript)obj; int check = 1; foreach (ScriptDetails details in AllScripts) { if (details.path == p) { check = 0; details.FoundInGameObjects.Add(path); break; } } if (check == 1) { ScriptDetails tScriptDetails = new ScriptDetails(); tScriptDetails.FoundInGameObjects = new List <string>(); tScriptDetails.name = script.name; tScriptDetails.path = p; tScriptDetails.FoundInGameObjects.Add(path); AllScripts.Add(tScriptDetails); } } } } } foreach (TextureDetails tTextureDetails in AllTextures) { TotalTextureMemory += tTextureDetails.memSizeBytes; } foreach (MeshDetails tMeshDetails in AllMeshes) { TotalMeshVertices += tMeshDetails.vertexCount; } }
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 (IncludeLightmapTextures) { LightmapData[] lightmapTextures = LightmapSettings.lightmaps; // Unity lightmaps foreach (LightmapData lightmapData in lightmapTextures) { if (lightmapData.lightmapColor != null) { var textureDetail = GetTextureDetail(lightmapData.lightmapColor); if (!ActiveTextures.Contains(textureDetail)) { ActiveTextures.Add(textureDetail); } } if (lightmapData.lightmapDir != null) { var textureDetail = GetTextureDetail(lightmapData.lightmapColor); if (!ActiveTextures.Contains(textureDetail)) { ActiveTextures.Add(textureDetail); } } if (lightmapData.shadowMask != null) { var textureDetail = GetTextureDetail(lightmapData.shadowMask); if (!ActiveTextures.Contains(textureDetail)) { ActiveTextures.Add(textureDetail); } } } } 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); } } Button[] buttons = FindObjects <Button>(); foreach (Button button in buttons) { CheckButtonSpriteState(button, button.spriteState.disabledSprite); CheckButtonSpriteState(button, button.spriteState.highlightedSprite); CheckButtonSpriteState(button, button.spriteState.pressedSprite); } } 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); if (GameObjectUtility.AreStaticEditorFlagsSet(tMeshFilter.gameObject, StaticEditorFlags.BatchingStatic)) { tMeshDetails.StaticBatchingEnabled.Add(tMeshFilter.gameObject); } } 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; } var meshRenderrer = tMeshFilter.transform.GetComponent <MeshRenderer>(); if (meshRenderrer == null || meshRenderrer.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 || UNITY_5_3_OR_NEWER 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++) { UnityEditor.Animations.AnimatorStateMachine sm = ac.layers[x].stateMachine; int cnt = sm.states.Length; for (int i = 0; i < cnt; i++) { UnityEditor.Animations.AnimatorState state = sm.states[i].state; Motion m = state.motion; 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; // Sort by render queue ActiveMaterials.Sort(MaterialSorter); }
MeshDetails TryAddActiveMesh( Mesh m ) { if ( m != null ) { MeshDetails details = FindMeshDetails( m ); if ( details == null ) { details = new MeshDetails( m ); ActiveMeshDetails.Add( details ); } return details; } return null; }
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; }
void CheckResources() { //Debug.Log("CheckResources"); ActiveTextures.Clear(); ActiveMaterials.Clear(); ActiveMeshDetails.Clear(); ActiveShaderDetails.Clear(); ActiveSoundDetails.Clear(); foreach (LightmapData lightmap in LightmapSettings.lightmaps) { TryAddActiveTextures(lightmap.lightmapNear); TryAddActiveTextures(lightmap.lightmapFar); } Renderer[] renderers = (Renderer[])FindObjectsOfType(typeof(Renderer)); foreach (Renderer renderer in renderers) { //Debug.Log("Renderer is "+renderer.name); foreach (Material material in renderer.sharedMaterials) { MaterialDetails tMaterialDetails = TryAddActiveMaterial(material); if (tMaterialDetails != null) { tMaterialDetails.FoundInGameObjects.Add(renderer.gameObject); } ShaderDetails tShaderDetails = TryAddActiveShader(material.shader); if (tShaderDetails != null) { if (!tShaderDetails.FoundInGameObjects.Contains(renderer.gameObject)) { tShaderDetails.FoundInGameObjects.Add(renderer.gameObject); } } } // add the lightmap reference to the renderer if (renderer.lightmapIndex >= 0 && renderer.lightmapIndex < LightmapSettings.lightmaps.Length) { LightmapData lightmap = LightmapSettings.lightmaps[renderer.lightmapIndex]; TextureDetails lmNear = FindTextureDetails(lightmap.lightmapNear); if (lmNear != null && !lmNear.FoundInRenderers.Contains(renderer)) { lmNear.FoundInRenderers.Add(renderer); } TextureDetails lmFar = FindTextureDetails(lightmap.lightmapFar); if (lmFar != null && !lmFar.FoundInRenderers.Contains(renderer)) { lmFar.FoundInRenderers.Add(renderer); } } } foreach (MaterialDetails tMaterialDetails in ActiveMaterials) { Material tMaterial = tMaterialDetails.material; foreach (Object obj in EditorUtility.CollectDependencies(new UnityEngine.Object[] { tMaterial })) { if (obj is Texture) { Texture tTexture = obj as Texture; TextureDetails tTextureDetails = TryAddActiveTextures(tTexture); tTextureDetails.FoundInMaterials.Add(tMaterial); } if (obj is Shader) { Shader shader = obj as Shader; ShaderDetails shaderDetails = TryAddActiveShader(shader); if (!shaderDetails.FoundInMaterials.Contains(tMaterial)) { shaderDetails.FoundInMaterials.Add(tMaterial); } } } } MeshFilter[] meshFilters = (MeshFilter[])FindObjectsOfType(typeof(MeshFilter)); foreach (MeshFilter tMeshFilter in meshFilters) { Mesh tMesh = tMeshFilter.sharedMesh; if (tMesh != null) { MeshDetails details = TryAddActiveMesh(tMesh); if (!details.FoundInGameObjects.Contains(tMeshFilter.gameObject)) { details.FoundInGameObjects.Add(tMeshFilter.gameObject); } } } Light[] lights = (Light[])FindObjectsOfType(typeof(Light)); foreach (Light light in lights) { if (light.cookie) { TextureDetails details = TryAddActiveTextures(light.cookie); if (!details.FoundInLights.Contains(light)) { details.FoundInLights.Add(light); } } } GameObject[] gameObjs = (GameObject[])FindObjectsOfType(typeof(GameObject)); foreach (GameObject obj in gameObjs) { foreach (Object o in EditorUtility.CollectDependencies(new UnityEngine.Object[] { obj })) { if (o is AudioClip) { AudioClip clip = o as AudioClip; SoundDetails details = TryAddAudioClip(clip); if (!details.FoundInGameObjects.Contains(obj)) { details.FoundInGameObjects.Add(obj); } } if (o is MonoScript) { MonoScript script = o as MonoScript; ScriptDetails details = TryAddScript(script); if (!details.FoundInGameObjects.Contains(obj)) { details.FoundInGameObjects.Add(obj); } } } } TotalTextureMemory = 0; foreach (TextureDetails tTextureDetails in ActiveTextures) { TotalTextureMemory += tTextureDetails.memSizeBytes; } TotalMeshVertices = 0; foreach (MeshDetails tMeshDetails in ActiveMeshDetails) { TotalMeshVertices += tMeshDetails.mesh.vertexCount; } // Sort by size, descending ActiveTextures.Sort(delegate(TextureDetails details1, TextureDetails details2) { return(details2.memSizeBytes - details1.memSizeBytes); }); ActiveMeshDetails.Sort(delegate(MeshDetails details1, MeshDetails details2) { return(details2.mesh.vertexCount - details1.mesh.vertexCount); }); }