随着时间的堆积,项目中Assets文件夹下的资源会变得越来越繁杂,有些贴图、材质啥的可能压根没有使用过,但是又不敢轻易去删除。

这里分享两个插件,用于管理这些资源。

 

一、ResourceChecker

 

这个插件的强大之处就在于它能够查找当前场景中的所有引用个资源,并快速定位,然后把未定位到的资源手动删掉就行了。

代码

 

   1 // Resource Checker
   2 // (c) 2012 Simon Oliver / HandCircus / hello@handcircus.com
   3 // (c) 2015 Brice Clocher / Mangatome / hello@mangatome.net
   4 // Public domain, do with whatever you like, commercial or not
   5 // This comes with no warranty, use at your own risk!
   6 // https://github.com/handcircus/Unity-Resource-Checker
   7 
   8 using System;
   9 using System.Linq;
  10 using UnityEngine;
  11 using UnityEngine.UI;
  12 using UnityEditor;
  13 using System.Collections.Generic;
  14 using System.Reflection;
  15 using Object = UnityEngine.Object;
  16 
  17 public class TextureDetails : IEquatable<TextureDetails>
  18 {
  19     public bool isCubeMap;
  20     public int memSizeKB;
  21     public Texture texture;
  22     public TextureFormat format;
  23     public int mipMapCount;
  24     public List<Object> FoundInMaterials=new List<Object>();
  25     public List<Object> FoundInRenderers=new List<Object>();
  26     public List<Object> FoundInAnimators = new List<Object>();
  27     public List<Object> FoundInScripts = new List<Object>();
  28     public List<Object> FoundInGraphics = new List<Object>();
  29     public bool isSky;
  30     public bool instance;
  31     public bool isgui;
  32     public TextureDetails()
  33     {
  34 
  35     }
  36 
  37     public bool Equals(TextureDetails other)
  38     {
  39         return texture != null && other.texture != null &&
  40             texture.GetNativeTexturePtr() == other.texture.GetNativeTexturePtr();
  41     }
  42 
  43     public override int GetHashCode()
  44     {
  45         return (int)texture.GetNativeTexturePtr();
  46     }
  47 
  48     public override bool Equals(object obj)
  49     {
  50         return Equals(obj as TextureDetails);
  51     }
  52 };
  53 
  54 public class MaterialDetails
  55 {
  56 
  57     public Material material;
  58 
  59     public List<Renderer> FoundInRenderers=new List<Renderer>();
  60     public List<Graphic> FoundInGraphics=new List<Graphic>();
  61     public bool instance;
  62     public bool isgui;
  63     public bool isSky;
  64 
  65     public MaterialDetails()
  66     {
  67         instance = false;
  68         isgui = false;
  69         isSky = false;
  70     }
  71 };
  72 
  73 public class MeshDetails
  74 {
  75 
  76     public Mesh mesh;
  77 
  78     public List<MeshFilter> FoundInMeshFilters=new List<MeshFilter>();
  79     public List<SkinnedMeshRenderer> FoundInSkinnedMeshRenderer=new List<SkinnedMeshRenderer>();
  80     public bool instance;
  81 
  82     public MeshDetails()
  83     {
  84         instance = false;
  85     }
  86 };
  87 
  88 public class MissingGraphic{
  89     public Transform Object;
  90     public string type;
  91     public string name;
  92 }
  93 
  94 public class ResourceChecker : EditorWindow {
  95 
  96 
  97     string[] inspectToolbarStrings = {"Textures", "Materials","Meshes"};
  98     string[] inspectToolbarStrings2 = {"Textures", "Materials","Meshes", "Missing"};
  99 
 100     enum InspectType 
 101     {
 102         Textures,Materials,Meshes,Missing
 103     };
 104 
 105     bool IncludeDisabledObjects=true;
 106     bool IncludeSpriteAnimations=true;
 107     bool IncludeScriptReferences=true;
 108     bool IncludeGuiElements=true;
 109     bool thingsMissing = false;
 110 
 111     InspectType ActiveInspectType=InspectType.Textures;
 112 
 113     float ThumbnailWidth=40;
 114     float ThumbnailHeight=40;
 115 
 116     List<TextureDetails> ActiveTextures=new List<TextureDetails>();
 117     List<MaterialDetails> ActiveMaterials=new List<MaterialDetails>();
 118     List<MeshDetails> ActiveMeshDetails=new List<MeshDetails>();
 119     List<MissingGraphic> MissingObjects = new List<MissingGraphic> ();
 120 
 121     Vector2 textureListScrollPos=new Vector2(0,0);
 122     Vector2 materialListScrollPos=new Vector2(0,0);
 123     Vector2 meshListScrollPos=new Vector2(0,0);
 124     Vector2 missingListScrollPos = new Vector2 (0,0);
 125 
 126     int TotalTextureMemory=0;
 127     int TotalMeshVertices=0;
 128 
 129     bool ctrlPressed=false;
 130 
 131     static int MinWidth=475;
 132     Color defColor;
 133 
 134     bool collectedInPlayingMode;
 135 
 136     [MenuItem ("Window/Resource Checker")]
 137     static void Init ()
 138     {  
 139         ResourceChecker window = (ResourceChecker) EditorWindow.GetWindow (typeof (ResourceChecker));
 140         window.CheckResources();
 141         window.minSize=new Vector2(MinWidth,475);
 142     }
 143 
 144     void OnGUI ()
 145     {
 146         defColor = GUI.color;
 147         IncludeDisabledObjects = GUILayout.Toggle(IncludeDisabledObjects, "Include disabled objects", GUILayout.Width(300));
 148         IncludeSpriteAnimations = GUILayout.Toggle(IncludeSpriteAnimations, "Look in sprite animations", GUILayout.Width(300));
 149         GUI.color = new Color (0.8f, 0.8f, 1.0f, 1.0f);
 150         IncludeScriptReferences = GUILayout.Toggle(IncludeScriptReferences, "Look in behavior fields", GUILayout.Width(300));
 151         GUI.color = new Color (1.0f, 0.95f, 0.8f, 1.0f);
 152         IncludeGuiElements = GUILayout.Toggle(IncludeGuiElements, "Look in GUI elements", GUILayout.Width(300));
 153         GUI.color = defColor;
 154         GUILayout.BeginArea(new Rect(position.width-85,5,100,65));
 155         if (GUILayout.Button("Calculate",GUILayout.Width(80), GUILayout.Height(40)))
 156             CheckResources();
 157         if (GUILayout.Button("CleanUp",GUILayout.Width(80), GUILayout.Height(20)))
 158             Resources.UnloadUnusedAssets();
 159         GUILayout.EndArea();
 160         RemoveDestroyedResources();
 161 
 162         GUILayout.Space(30);
 163         if (thingsMissing == true) {
 164             EditorGUI.HelpBox (new Rect(8,75,300,25),"Some GameObjects are missing graphical elements.", MessageType.Error);
 165         }
 166         GUILayout.BeginHorizontal();
 167         GUILayout.Label("Textures "+ActiveTextures.Count+" - "+FormatSizeString(TotalTextureMemory));
 168         GUILayout.Label("Materials "+ActiveMaterials.Count);
 169         GUILayout.Label("Meshes "+ActiveMeshDetails.Count+" - "+TotalMeshVertices+" verts");
 170         GUILayout.EndHorizontal();
 171         if (thingsMissing == true) {
 172             ActiveInspectType = (InspectType)GUILayout.Toolbar ((int)ActiveInspectType, inspectToolbarStrings2);
 173         } else {
 174             ActiveInspectType = (InspectType)GUILayout.Toolbar ((int)ActiveInspectType, inspectToolbarStrings);
 175         }
 176 
 177         ctrlPressed=Event.current.control || Event.current.command;
 178 
 179         switch (ActiveInspectType)
 180         {
 181         case InspectType.Textures:
 182             ListTextures();
 183             break;
 184         case InspectType.Materials:
 185             ListMaterials();
 186             break;
 187         case InspectType.Meshes:
 188             ListMeshes();
 189             break;
 190         case InspectType.Missing:
 191             ListMissing();
 192             break;
 193         }
 194     }
 195 
 196     private void RemoveDestroyedResources()
 197     {
 198         if (collectedInPlayingMode != Application.isPlaying)
 199         {
 200             ActiveTextures.Clear();
 201             ActiveMaterials.Clear();
 202             ActiveMeshDetails.Clear();
 203             MissingObjects.Clear ();
 204             thingsMissing = false;
 205             collectedInPlayingMode = Application.isPlaying;
 206         }
 207         
 208         ActiveTextures.RemoveAll(x => !x.texture);
 209         ActiveTextures.ForEach(delegate(TextureDetails obj) {
 210             obj.FoundInAnimators.RemoveAll(x => !x);
 211             obj.FoundInMaterials.RemoveAll(x => !x);
 212             obj.FoundInRenderers.RemoveAll(x => !x);
 213             obj.FoundInScripts.RemoveAll(x => !x);
 214             obj.FoundInGraphics.RemoveAll(x => !x);
 215         });
 216 
 217         ActiveMaterials.RemoveAll(x => !x.material);
 218         ActiveMaterials.ForEach(delegate(MaterialDetails obj) {
 219             obj.FoundInRenderers.RemoveAll(x => !x);
 220             obj.FoundInGraphics.RemoveAll(x => !x);
 221         });
 222 
 223         ActiveMeshDetails.RemoveAll(x => !x.mesh);
 224         ActiveMeshDetails.ForEach(delegate(MeshDetails obj) {
 225             obj.FoundInMeshFilters.RemoveAll(x => !x);
 226             obj.FoundInSkinnedMeshRenderer.RemoveAll(x => !x);
 227         });
 228 
 229         TotalTextureMemory = 0;
 230         foreach (TextureDetails tTextureDetails in ActiveTextures) TotalTextureMemory += tTextureDetails.memSizeKB;
 231 
 232         TotalMeshVertices = 0;
 233         foreach (MeshDetails tMeshDetails in ActiveMeshDetails) TotalMeshVertices += tMeshDetails.mesh.vertexCount;
 234     }
 235 
 236     int GetBitsPerPixel(TextureFormat format)
 237     {
 238         switch (format)
 239         {
 240         case TextureFormat.Alpha8: //     Alpha-only texture format.
 241             return 8;
 242         case TextureFormat.ARGB4444: //     A 16 bits/pixel texture format. Texture stores color with an alpha channel.
 243             return 16;
 244         case TextureFormat.RGBA4444: //     A 16 bits/pixel texture format.
 245             return 16;
 246         case TextureFormat.RGB24:    // A color texture format.
 247             return 24;
 248         case TextureFormat.RGBA32:    //Color with an alpha channel texture format.
 249             return 32;
 250         case TextureFormat.ARGB32:    //Color with an alpha channel texture format.
 251             return 32;
 252         case TextureFormat.RGB565:    //     A 16 bit color texture format.
 253             return 16;
 254         case TextureFormat.DXT1:    // Compressed color texture format.
 255             return 4;
 256         case TextureFormat.DXT5:    // Compressed color with alpha channel texture format.
 257             return 8;
 258             /*
 259             case TextureFormat.WiiI4:    // Wii texture format.
 260             case TextureFormat.WiiI8:    // Wii texture format. Intensity 8 bit.
 261             case TextureFormat.WiiIA4:    // Wii texture format. Intensity + Alpha 8 bit (4 + 4).
 262             case TextureFormat.WiiIA8:    // Wii texture format. Intensity + Alpha 16 bit (8 + 8).
 263             case TextureFormat.WiiRGB565:    // Wii texture format. RGB 16 bit (565).
 264             case TextureFormat.WiiRGB5A3:    // Wii texture format. RGBA 16 bit (4443).
 265             case TextureFormat.WiiRGBA8:    // Wii texture format. RGBA 32 bit (8888).
 266             case TextureFormat.WiiCMPR:    //     Compressed Wii texture format. 4 bits/texel, ~RGB8A1 (Outline alpha is not currently supported).
 267                 return 0;  //Not supported yet
 268             */
 269         case TextureFormat.PVRTC_RGB2://     PowerVR (iOS) 2 bits/pixel compressed color texture format.
 270             return 2;
 271         case TextureFormat.PVRTC_RGBA2://     PowerVR (iOS) 2 bits/pixel compressed with alpha channel texture format
 272             return 2;
 273         case TextureFormat.PVRTC_RGB4://     PowerVR (iOS) 4 bits/pixel compressed color texture format.
 274             return 4;
 275         case TextureFormat.PVRTC_RGBA4://     PowerVR (iOS) 4 bits/pixel compressed with alpha channel texture format
 276             return 4;
 277         case TextureFormat.ETC_RGB4://     ETC (GLES2.0) 4 bits/pixel compressed RGB texture format.
 278             return 4;
 279         case TextureFormat.ATC_RGB4://     ATC (ATITC) 4 bits/pixel compressed RGB texture format.
 280             return 4;
 281         case TextureFormat.ATC_RGBA8://     ATC (ATITC) 8 bits/pixel compressed RGB texture format.
 282             return 8;
 283         case TextureFormat.BGRA32://     Format returned by iPhone camera
 284             return 32;
 285             #if !UNITY_5 && !UNITY_5_3_OR_NEWER
 286             case TextureFormat.ATF_RGB_DXT1://     Flash-specific RGB DXT1 compressed color texture format.
 287             case TextureFormat.ATF_RGBA_JPG://     Flash-specific RGBA JPG-compressed color texture format.
 288             case TextureFormat.ATF_RGB_JPG://     Flash-specific RGB JPG-compressed color texture format.
 289             return 0; //Not supported yet  
 290             #endif
 291         }
 292         return 0;
 293     }
 294 
 295     int CalculateTextureSizeBytes(Texture tTexture)
 296     {
 297 
 298         int tWidth=tTexture.width;
 299         int tHeight=tTexture.height;
 300         if (tTexture is Texture2D)
 301         {
 302             Texture2D tTex2D=tTexture as Texture2D;
 303             int bitsPerPixel=GetBitsPerPixel(tTex2D.format);
 304             int mipMapCount=tTex2D.mipmapCount;
 305             int mipLevel=1;
 306             int tSize=0;
 307             while (mipLevel<=mipMapCount)
 308             {
 309                 tSize+=tWidth*tHeight*bitsPerPixel/8;
 310                 tWidth=tWidth/2;
 311                 tHeight=tHeight/2;
 312                 mipLevel++;
 313             }
 314             return tSize;
 315         }
 316         if (tTexture is Texture2DArray)
 317         {
 318             Texture2DArray tTex2D=tTexture as Texture2DArray;
 319             int bitsPerPixel=GetBitsPerPixel(tTex2D.format);
 320             int mipMapCount=10;
 321             int mipLevel=1;
 322             int tSize=0;
 323             while (mipLevel<=mipMapCount)
 324             {
 325                 tSize+=tWidth*tHeight*bitsPerPixel/8;
 326                 tWidth=tWidth/2;
 327                 tHeight=tHeight/2;
 328                 mipLevel++;
 329             }
 330             return tSize*((Texture2DArray)tTex2D).depth;
 331         }
 332         if (tTexture is Cubemap) {
 333             Cubemap tCubemap = tTexture as Cubemap;
 334             int bitsPerPixel = GetBitsPerPixel (tCubemap.format);
 335             return tWidth * tHeight * 6 * bitsPerPixel / 8;
 336         }
 337         return 0;
 338     }
 339 
 340 
 341     void SelectObject(Object selectedObject,bool append)
 342     {
 343         if (append)
 344         {
 345             List<Object> currentSelection=new List<Object>(Selection.objects);
 346             // Allow toggle selection
 347             if (currentSelection.Contains(selectedObject)) currentSelection.Remove(selectedObject);
 348             else currentSelection.Add(selectedObject);
 349 
 350             Selection.objects=currentSelection.ToArray();
 351         }
 352         else Selection.activeObject=selectedObject;
 353     }
 354 
 355     void SelectObjects(List<Object> selectedObjects,bool append)
 356     {
 357         if (append)
 358         {
 359             List<Object> currentSelection=new List<Object>(Selection.objects);
 360             currentSelection.AddRange(selectedObjects);
 361             Selection.objects=currentSelection.ToArray();
 362         }
 363         else Selection.objects=selectedObjects.ToArray();
 364     }
 365 
 366     void ListTextures()
 367     {
 368         textureListScrollPos = EditorGUILayout.BeginScrollView(textureListScrollPos);
 369 
 370         foreach (TextureDetails tDetails in ActiveTextures)
 371         {            
 372 
 373             GUILayout.BeginHorizontal ();
 374             Texture tex = new Texture();
 375             tex = tDetails.texture;
 376             if(tDetails.texture.GetType() == typeof(Texture2DArray) || tDetails.texture.GetType() == typeof(Cubemap)){
 377                 tex = AssetPreview.GetMiniThumbnail(tDetails.texture);
 378             }
 379             GUILayout.Box(tex, GUILayout.Width(ThumbnailWidth), GUILayout.Height(ThumbnailHeight));
 380 
 381             if (tDetails.instance == true)
 382                 GUI.color = new Color (0.8f, 0.8f, defColor.b, 1.0f);
 383             if (tDetails.isgui == true)
 384                 GUI.color = new Color (defColor.r, 0.95f, 0.8f, 1.0f);
 385             if (tDetails.isSky)
 386                 GUI.color = new Color (0.9f, defColor.g, defColor.b, 1.0f);
 387             if(GUILayout.Button(tDetails.texture.name,GUILayout.Width(150)))
 388             {
 389                 SelectObject(tDetails.texture,ctrlPressed);
 390             }
 391             GUI.color = defColor;
 392 
 393             string sizeLabel=""+tDetails.texture.width+"x"+tDetails.texture.height;
 394             if (tDetails.isCubeMap) sizeLabel+="x6";
 395             if (tDetails.texture.GetType () == typeof(Texture2DArray))
 396                 sizeLabel+= "[]\n" + ((Texture2DArray)tDetails.texture).depth+"depths";
 397             sizeLabel+=" - "+tDetails.mipMapCount+"mip\n"+FormatSizeString(tDetails.memSizeKB)+" - "+tDetails.format;
 398 
 399             GUILayout.Label (sizeLabel,GUILayout.Width(120));
 400 
 401             if(GUILayout.Button(tDetails.FoundInMaterials.Count+" Mat",GUILayout.Width(50)))
 402             {
 403                 SelectObjects(tDetails.FoundInMaterials,ctrlPressed);
 404             }
 405 
 406             HashSet<Object> FoundObjects = new HashSet<Object>();
 407             foreach (Renderer renderer in tDetails.FoundInRenderers) FoundObjects.Add(renderer.gameObject);
 408             foreach (Animator animator in tDetails.FoundInAnimators) FoundObjects.Add(animator.gameObject);
 409             foreach (Graphic graphic in tDetails.FoundInGraphics) FoundObjects.Add(graphic.gameObject);
 410             foreach (MonoBehaviour script in tDetails.FoundInScripts) FoundObjects.Add(script.gameObject);
 411             if (GUILayout.Button(FoundObjects.Count+" GO",GUILayout.Width(50)))
 412             {
 413                 SelectObjects(new List<Object>(FoundObjects),ctrlPressed);
 414             }
 415 
 416             GUILayout.EndHorizontal();    
 417         }
 418         if (ActiveTextures.Count>0)
 419         {
 420             EditorGUILayout.Space();
 421             GUILayout.BeginHorizontal ();
 422             //GUILayout.Box(" ",GUILayout.Width(ThumbnailWidth),GUILayout.Height(ThumbnailHeight));
 423             if(GUILayout.Button("Select \n All",GUILayout.Width(ThumbnailWidth*2)))
 424             {
 425                 List<Object> AllTextures=new List<Object>();
 426                 foreach (TextureDetails tDetails in ActiveTextures) AllTextures.Add(tDetails.texture);
 427                 SelectObjects(AllTextures,ctrlPressed);
 428             }
 429             EditorGUILayout.EndHorizontal();
 430         }
 431         EditorGUILayout.EndScrollView();
 432     }
 433 
 434     void ListMaterials()
 435     {
 436         materialListScrollPos = EditorGUILayout.BeginScrollView(materialListScrollPos);
 437 
 438         foreach (MaterialDetails tDetails in ActiveMaterials)
 439         {            
 440             if (tDetails.material!=null)
 441             {
 442                 GUILayout.BeginHorizontal ();
 443 
 444                 GUILayout.Box(AssetPreview.GetAssetPreview(tDetails.material), GUILayout.Width(ThumbnailWidth), GUILayout.Height(ThumbnailHeight));
 445 
 446                 if (tDetails.instance == true)
 447                     GUI.color = new Color (0.8f, 0.8f, defColor.b, 1.0f);
 448                 if (tDetails.isgui == true)
 449                     GUI.color = new Color (defColor.r, 0.95f, 0.8f, 1.0f);
 450                 if (tDetails.isSky)
 451                     GUI.color = new Color (0.9f, defColor.g, defColor.b, 1.0f);
 452                 if(GUILayout.Button(tDetails.material.name,GUILayout.Width(150)))
 453                 {
 454                     SelectObject(tDetails.material,ctrlPressed);
 455                 }
 456                 GUI.color = defColor;
 457 
 458                 string shaderLabel = tDetails.material.shader != null ? tDetails.material.shader.name : "no shader";
 459                 GUILayout.Label (shaderLabel, GUILayout.Width(200));
 460 
 461                 if(GUILayout.Button((tDetails.FoundInRenderers.Count + tDetails.FoundInGraphics.Count) +" GO",GUILayout.Width(50)))
 462                 {
 463                     List<Object> FoundObjects=new List<Object>();
 464                     foreach (Renderer renderer in tDetails.FoundInRenderers) FoundObjects.Add(renderer.gameObject);
 465                     foreach (Graphic graphic in tDetails.FoundInGraphics) FoundObjects.Add(graphic.gameObject);
 466                     SelectObjects(FoundObjects,ctrlPressed);
 467                 }
 468 
 469 
 470                 GUILayout.EndHorizontal();    
 471             }
 472         }
 473         EditorGUILayout.EndScrollView();        
 474     }
 475 
 476     void ListMeshes()
 477     {
 478         meshListScrollPos = EditorGUILayout.BeginScrollView(meshListScrollPos);
 479 
 480         foreach (MeshDetails tDetails in ActiveMeshDetails)
 481         {            
 482             if (tDetails.mesh!=null)
 483             {
 484                 GUILayout.BeginHorizontal ();
 485                 string name = tDetails.mesh.name;
 486                 if (name == null || name.Count() < 1)
 487                     name = tDetails.FoundInMeshFilters[0].gameObject.name;
 488                 if (tDetails.instance == true)
 489                     GUI.color = new Color (0.8f, 0.8f, defColor.b, 1.0f);
 490                 if(GUILayout.Button(name,GUILayout.Width(150)))
 491                 {
 492                     SelectObject(tDetails.mesh,ctrlPressed);
 493                 }
 494                 GUI.color = defColor;
 495                 string sizeLabel=""+tDetails.mesh.vertexCount+" vert";
 496 
 497                 GUILayout.Label (sizeLabel,GUILayout.Width(100));
 498 
 499 
 500                 if(GUILayout.Button(tDetails.FoundInMeshFilters.Count + " GO",GUILayout.Width(50)))
 501                 {
 502                     List<Object> FoundObjects=new List<Object>();
 503                     foreach (MeshFilter meshFilter in tDetails.FoundInMeshFilters) FoundObjects.Add(meshFilter.gameObject);
 504                     SelectObjects(FoundObjects,ctrlPressed);
 505                 }
 506                 if (tDetails.FoundInSkinnedMeshRenderer.Count > 0) {
 507                     if (GUILayout.Button (tDetails.FoundInSkinnedMeshRenderer.Count + " skinned mesh GO", GUILayout.Width (140))) {
 508                         List<Object> FoundObjects = new List<Object> ();
 509                         foreach (SkinnedMeshRenderer skinnedMeshRenderer in tDetails.FoundInSkinnedMeshRenderer)
 510                             FoundObjects.Add (skinnedMeshRenderer.gameObject);
 511                         SelectObjects (FoundObjects, ctrlPressed);
 512                     }
 513                 } else {
 514                     GUI.color = new Color (defColor.r, defColor.g, defColor.b, 0.5f);
 515                     GUILayout.Label("   0 skinned mesh");
 516                     GUI.color = defColor;
 517                 }
 518 
 519 
 520                 GUILayout.EndHorizontal();    
 521             }
 522         }
 523         EditorGUILayout.EndScrollView();        
 524     }
 525 
 526     void ListMissing(){
 527         missingListScrollPos = EditorGUILayout.BeginScrollView(missingListScrollPos);
 528         foreach (MissingGraphic dMissing in MissingObjects) {
 529             GUILayout.BeginHorizontal ();
 530             if (GUILayout.Button (dMissing.name, GUILayout.Width (150)))
 531                 SelectObject (dMissing.Object, ctrlPressed);
 532             GUILayout.Label ("missing ", GUILayout.Width(48));
 533             switch (dMissing.type) {
 534             case "mesh":
 535                 GUI.color = new Color (0.8f, 0.8f, defColor.b, 1.0f);
 536                 break;
 537             case "sprite":
 538                 GUI.color = new Color (defColor.r, 0.8f, 0.8f, 1.0f);
 539                 break;
 540             case "material":
 541                 GUI.color = new Color (0.8f, defColor.g, 0.8f, 1.0f);
 542                 break;
 543             }
 544             GUILayout.Label (dMissing.type);
 545             GUI.color = defColor;
 546             GUILayout.EndHorizontal ();
 547         }
 548         EditorGUILayout.EndScrollView();
 549     }
 550 
 551     string FormatSizeString(int memSizeKB)
 552     {
 553         if (memSizeKB<1024) return ""+memSizeKB+"k";
 554         else
 555         {
 556             float memSizeMB=((float)memSizeKB)/1024.0f;
 557             return memSizeMB.ToString("0.00")+"Mb";
 558         }
 559     }
 560 
 561 
 562     TextureDetails FindTextureDetails(Texture tTexture)
 563     {
 564         foreach (TextureDetails tTextureDetails in ActiveTextures)
 565         {
 566             if (tTextureDetails.texture==tTexture) return tTextureDetails;
 567         }
 568         return null;
 569 
 570     }
 571 
 572     MaterialDetails FindMaterialDetails(Material tMaterial)
 573     {
 574         foreach (MaterialDetails tMaterialDetails in ActiveMaterials)
 575         {
 576             if (tMaterialDetails.material==tMaterial) return tMaterialDetails;
 577         }
 578         return null;
 579 
 580     }
 581 
 582     MeshDetails FindMeshDetails(Mesh tMesh)
 583     {
 584         foreach (MeshDetails tMeshDetails in ActiveMeshDetails)
 585         {
 586             if (tMeshDetails.mesh==tMesh) return tMeshDetails;
 587         }
 588         return null;
 589 
 590     }
 591 
 592 
 593     void CheckResources()
 594     {
 595         ActiveTextures.Clear();
 596         ActiveMaterials.Clear();
 597         ActiveMeshDetails.Clear();
 598         MissingObjects.Clear ();
 599         thingsMissing = false;
 600 
 601         Renderer[] renderers = FindObjects<Renderer>();
 602 
 603         MaterialDetails skyMat = new MaterialDetails ();
 604         skyMat.material = RenderSettings.skybox;
 605         skyMat.isSky = true;
 606         ActiveMaterials.Add (skyMat);
 607 
 608         //Debug.Log("Total renderers "+renderers.Length);
 609         foreach (Renderer renderer in renderers)
 610         {
 611             //Debug.Log("Renderer is "+renderer.name);
 612             foreach (Material material in renderer.sharedMaterials)
 613             {
 614 
 615                 MaterialDetails tMaterialDetails = FindMaterialDetails(material);
 616                 if (tMaterialDetails == null)
 617                 {
 618                     tMaterialDetails = new MaterialDetails();
 619                     tMaterialDetails.material = material;
 620                     ActiveMaterials.Add(tMaterialDetails);
 621                 }
 622                 tMaterialDetails.FoundInRenderers.Add(renderer);
 623             }
 624 
 625             if (renderer is SpriteRenderer)
 626             {
 627                 SpriteRenderer tSpriteRenderer = (SpriteRenderer)renderer;
 628 
 629                 if (tSpriteRenderer.sprite != null) {
 630                     var tSpriteTextureDetail = GetTextureDetail (tSpriteRenderer.sprite.texture, renderer);
 631                     if (!ActiveTextures.Contains (tSpriteTextureDetail)) {
 632                         ActiveTextures.Add (tSpriteTextureDetail);
 633                     }
 634                 } else if (tSpriteRenderer.sprite == null) {
 635                     MissingGraphic tMissing = new MissingGraphic ();
 636                     tMissing.Object = tSpriteRenderer.transform;
 637                     tMissing.type = "sprite";
 638                     tMissing.name = tSpriteRenderer.transform.name;
 639                     MissingObjects.Add (tMissing);
 640                     thingsMissing = true;
 641                 }
 642             }
 643         }
 644 
 645         if (IncludeGuiElements)
 646         {
 647             Graphic[] graphics = FindObjects<Graphic>();
 648 
 649             foreach(Graphic graphic in graphics)
 650             {
 651                 if (graphic.mainTexture)
 652                 {
 653                     var tSpriteTextureDetail = GetTextureDetail(graphic.mainTexture, graphic);
 654                     if (!ActiveTextures.Contains(tSpriteTextureDetail))
 655                     {
 656                         ActiveTextures.Add(tSpriteTextureDetail);
 657                     }
 658                 }
 659 
 660                 if (graphic.materialForRendering)
 661                 {
 662                     MaterialDetails tMaterialDetails = FindMaterialDetails(graphic.materialForRendering);
 663                     if (tMaterialDetails == null)
 664                     {
 665                         tMaterialDetails = new MaterialDetails();
 666                         tMaterialDetails.material = graphic.materialForRendering;
 667                         tMaterialDetails.isgui = true;
 668                         ActiveMaterials.Add(tMaterialDetails);
 669                     }
 670                     tMaterialDetails.FoundInGraphics.Add(graphic);
 671                 }
 672             }
 673         }
 674 
 675         foreach (MaterialDetails tMaterialDetails in ActiveMaterials)
 676         {
 677             Material tMaterial = tMaterialDetails.material;
 678             if (tMaterial != null)
 679             {
 680                 var dependencies = EditorUtility.CollectDependencies(new UnityEngine.Object[] { tMaterial });
 681                 foreach (Object obj in dependencies)
 682                 {
 683                     if (obj is Texture)
 684                     {
 685                         Texture tTexture = obj as Texture;
 686                         var tTextureDetail = GetTextureDetail(tTexture, tMaterial, tMaterialDetails);
 687                         tTextureDetail.isSky = tMaterialDetails.isSky;
 688                         tTextureDetail.instance = tMaterialDetails.instance;
 689                         tTextureDetail.isgui = tMaterialDetails.isgui;
 690                         ActiveTextures.Add(tTextureDetail);
 691                     }
 692                 }
 693 
 694                 //if the texture was downloaded, it won't be included in the editor dependencies
 695                 if (tMaterial.HasProperty ("_MainTex")) {
 696                     if (tMaterial.mainTexture != null && !dependencies.Contains (tMaterial.mainTexture)) {
 697                         var tTextureDetail = GetTextureDetail (tMaterial.mainTexture, tMaterial, tMaterialDetails);
 698                         ActiveTextures.Add (tTextureDetail);
 699                     }
 700                 }
 701             }
 702         }
 703 
 704 
 705         MeshFilter[] meshFilters = FindObjects<MeshFilter>();
 706 
 707         foreach (MeshFilter tMeshFilter in meshFilters)
 708         {
 709             Mesh tMesh = tMeshFilter.sharedMesh;
 710             if (tMesh != null)
 711             {
 712                 MeshDetails tMeshDetails = FindMeshDetails(tMesh);
 713                 if (tMeshDetails == null)
 714                 {
 715                     tMeshDetails = new MeshDetails();
 716                     tMeshDetails.mesh = tMesh;
 717                     ActiveMeshDetails.Add(tMeshDetails);
 718                 }
 719                 tMeshDetails.FoundInMeshFilters.Add(tMeshFilter);
 720             } else if (tMesh == null && tMeshFilter.transform.GetComponent("TextContainer")== null) {
 721                 MissingGraphic tMissing = new MissingGraphic ();
 722                 tMissing.Object = tMeshFilter.transform;
 723                 tMissing.type = "mesh";
 724                 tMissing.name = tMeshFilter.transform.name;
 725                 MissingObjects.Add (tMissing);
 726                 thingsMissing = true;
 727             }
 728 
 729             var meshRenderrer = tMeshFilter.transform.GetComponent<MeshRenderer>();
 730                 
 731             if (meshRenderrer == null || meshRenderrer.sharedMaterial == null) {
 732                 MissingGraphic tMissing = new MissingGraphic ();
 733                 tMissing.Object = tMeshFilter.transform;
 734                 tMissing.type = "material";
 735                 tMissing.name = tMeshFilter.transform.name;
 736                 MissingObjects.Add (tMissing);
 737                 thingsMissing = true;
 738             }
 739         }
 740 
 741         SkinnedMeshRenderer[] skinnedMeshRenderers = FindObjects<SkinnedMeshRenderer>();
 742 
 743         foreach (SkinnedMeshRenderer tSkinnedMeshRenderer in skinnedMeshRenderers)
 744         {
 745             Mesh tMesh = tSkinnedMeshRenderer.sharedMesh;
 746             if (tMesh != null)
 747             {
 748                 MeshDetails tMeshDetails = FindMeshDetails(tMesh);
 749                 if (tMeshDetails == null)
 750                 {
 751                     tMeshDetails = new MeshDetails();
 752                     tMeshDetails.mesh = tMesh;
 753                     ActiveMeshDetails.Add(tMeshDetails);
 754                 }
 755                 tMeshDetails.FoundInSkinnedMeshRenderer.Add(tSkinnedMeshRenderer);
 756             } else if (tMesh == null) {
 757                 MissingGraphic tMissing = new MissingGraphic ();
 758                 tMissing.Object = tSkinnedMeshRenderer.transform;
 759                 tMissing.type = "mesh";
 760                 tMissing.name = tSkinnedMeshRenderer.transform.name;
 761                 MissingObjects.Add (tMissing);
 762                 thingsMissing = true;
 763             }
 764             if (tSkinnedMeshRenderer.sharedMaterial == null) {
 765                 MissingGraphic tMissing = new MissingGraphic ();
 766                 tMissing.Object = tSkinnedMeshRenderer.transform;
 767                 tMissing.type = "material";
 768                 tMissing.name = tSkinnedMeshRenderer.transform.name;
 769                 MissingObjects.Add (tMissing);
 770                 thingsMissing = true;
 771             }
 772         }
 773 
 774         if (IncludeSpriteAnimations)
 775         {
 776             Animator[] animators = FindObjects<Animator>();
 777             foreach (Animator anim in animators)
 778             {
 779                 #if UNITY_4_6 || UNITY_4_5 || UNITY_4_4 || UNITY_4_3
 780                 UnityEditorInternal.AnimatorController ac = anim.runtimeAnimatorController as UnityEditorInternal.AnimatorController;
 781                 #elif UNITY_5 || UNITY_5_3_OR_NEWER
 782                 UnityEditor.Animations.AnimatorController ac = anim.runtimeAnimatorController as UnityEditor.Animations.AnimatorController;
 783                 #endif
 784 
 785                 //Skip animators without layers, this can happen if they don't have an animator controller.
 786                 if (!ac || ac.layers == null || ac.layers.Length == 0)
 787                     continue;
 788 
 789                 for (int x = 0; x < anim.layerCount; x++)
 790                 {
 791                     #if UNITY_4_6 || UNITY_4_5 || UNITY_4_4 || UNITY_4_3
 792                     UnityEditorInternal.StateMachine sm = ac.GetLayer(x).stateMachine;
 793                     int cnt = sm.stateCount;
 794                     #elif UNITY_5 || UNITY_5_3_OR_NEWER
 795                     UnityEditor.Animations.AnimatorStateMachine sm = ac.layers[x].stateMachine;
 796                     int cnt = sm.states.Length;
 797                     #endif
 798 
 799                     for (int i = 0; i < cnt; i++)
 800                     {
 801                         #if UNITY_4_6 || UNITY_4_5 || UNITY_4_4 || UNITY_4_3
 802                         UnityEditorInternal.State state = sm.GetState(i);
 803                         Motion m = state.GetMotion();
 804                         #elif UNITY_5 || UNITY_5_3_OR_NEWER
 805                         UnityEditor.Animations.AnimatorState state = sm.states[i].state;
 806                         Motion m = state.motion;
 807                         #endif
 808                         if (m != null)
 809                         {
 810                             AnimationClip clip = m as AnimationClip;
 811 
 812                             if (clip != null)
 813                             {
 814                                 EditorCurveBinding[] ecbs = AnimationUtility.GetObjectReferenceCurveBindings(clip);
 815 
 816                                 foreach (EditorCurveBinding ecb in ecbs)
 817                                 {
 818                                     if (ecb.propertyName == "m_Sprite")
 819                                     {
 820                                         foreach (ObjectReferenceKeyframe keyframe in AnimationUtility.GetObjectReferenceCurve(clip, ecb))
 821                                         {
 822                                             Sprite tSprite = keyframe.value as Sprite;
 823 
 824                                             if (tSprite != null)
 825                                             {
 826                                                 var tTextureDetail = GetTextureDetail(tSprite.texture, anim);
 827                                                 if (!ActiveTextures.Contains(tTextureDetail))
 828                                                 {
 829                                                     ActiveTextures.Add(tTextureDetail);
 830                                                 }
 831                                             }
 832                                         }
 833                                     }
 834                                 }
 835                             }
 836                         }
 837                     }
 838                 }
 839 
 840             }
 841         }
 842 
 843         if (IncludeScriptReferences)
 844         {
 845             MonoBehaviour[] scripts = FindObjects<MonoBehaviour>();
 846             foreach (MonoBehaviour script in scripts)
 847             {
 848                 BindingFlags flags = BindingFlags.Public | BindingFlags.Instance; // only public non-static fields are bound to by Unity.
 849                 FieldInfo[] fields = script.GetType().GetFields(flags);
 850 
 851                 foreach (FieldInfo field in fields)
 852                 {
 853                     System.Type fieldType = field.FieldType;
 854                     if (fieldType == typeof(Sprite))
 855                     {
 856                         Sprite tSprite = field.GetValue(script) as Sprite;
 857                         if (tSprite != null)
 858                         {
 859                             var tSpriteTextureDetail = GetTextureDetail(tSprite.texture, script);
 860                             if (!ActiveTextures.Contains(tSpriteTextureDetail))
 861                             {
 862                                 ActiveTextures.Add(tSpriteTextureDetail);
 863                             }
 864                         }
 865                     }if (fieldType == typeof(Mesh))
 866                     {
 867                         Mesh tMesh = field.GetValue(script) as Mesh;
 868                         if (tMesh != null)
 869                         {
 870                             MeshDetails tMeshDetails = FindMeshDetails(tMesh);
 871                             if (tMeshDetails == null)
 872                             {
 873                                 tMeshDetails = new MeshDetails();
 874                                 tMeshDetails.mesh = tMesh;
 875                                 tMeshDetails.instance = true;
 876                                 ActiveMeshDetails.Add(tMeshDetails);
 877                             }
 878                         }
 879                     }if (fieldType == typeof(Material))
 880                     {
 881                         Material tMaterial = field.GetValue(script) as Material;
 882                         if (tMaterial != null)
 883                         {
 884                             MaterialDetails tMatDetails = FindMaterialDetails(tMaterial);
 885                             if (tMatDetails == null)
 886                             {
 887                                 tMatDetails = new MaterialDetails();
 888                                 tMatDetails.instance = true;
 889                                 tMatDetails.material = tMaterial;
 890                                 if(!ActiveMaterials.Contains(tMatDetails))
 891                                     ActiveMaterials.Add(tMatDetails);
 892                             }
 893                             if (tMaterial.mainTexture)
 894                             {
 895                                 var tSpriteTextureDetail = GetTextureDetail(tMaterial.mainTexture);
 896                                 if (!ActiveTextures.Contains(tSpriteTextureDetail))
 897                                 {
 898                                     ActiveTextures.Add(tSpriteTextureDetail);
 899                                 }
 900                             }
 901                             var dependencies = EditorUtility.CollectDependencies(new UnityEngine.Object[] { tMaterial });
 902                             foreach (Object obj in dependencies)
 903                             {
 904                                 if (obj is Texture)
 905                                 {
 906                                     Texture tTexture = obj as Texture;
 907                                     var tTextureDetail = GetTextureDetail(tTexture, tMaterial, tMatDetails);
 908                                     if(!ActiveTextures.Contains(tTextureDetail))
 909                                         ActiveTextures.Add(tTextureDetail);
 910                                 }
 911                             }
 912                         }
 913                     }
 914                 }
 915             }
 916         }
 917 
 918         TotalTextureMemory = 0;
 919         foreach (TextureDetails tTextureDetails in ActiveTextures) TotalTextureMemory += tTextureDetails.memSizeKB;
 920 
 921         TotalMeshVertices = 0;
 922         foreach (MeshDetails tMeshDetails in ActiveMeshDetails) TotalMeshVertices += tMeshDetails.mesh.vertexCount;
 923 
 924         // Sort by size, descending
 925         ActiveTextures.Sort(delegate(TextureDetails details1, TextureDetails details2) { return details2.memSizeKB - details1.memSizeKB; });
 926         ActiveTextures = ActiveTextures.Distinct().ToList();
 927         ActiveMeshDetails.Sort(delegate(MeshDetails details1, MeshDetails details2) { return details2.mesh.vertexCount - details1.mesh.vertexCount; });
 928 
 929         collectedInPlayingMode = Application.isPlaying;
 930     }
 931 
 932     private static GameObject[] GetAllRootGameObjects()
 933     {
 934 #if !UNITY_5 && !UNITY_5_3_OR_NEWER
 935         return UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects().ToArray();
 936 #else
 937         List<GameObject> allGo = new List<GameObject>();
 938         for (int sceneIdx = 0; sceneIdx < UnityEngine.SceneManagement.SceneManager.sceneCount; ++sceneIdx){
 939             allGo.AddRange( UnityEngine.SceneManagement.SceneManager.GetSceneAt(sceneIdx).GetRootGameObjects().ToArray() );
 940         }
 941         return allGo.ToArray();
 942 #endif
 943     }
 944 
 945     private T[] FindObjects<T>() where T : Object
 946     {
 947         if (IncludeDisabledObjects) {
 948             List<T> meshfilters = new List<T> ();
 949             GameObject[] allGo = GetAllRootGameObjects();
 950             foreach (GameObject go in allGo) {
 951                 Transform[] tgo = go.GetComponentsInChildren<Transform> (true).ToArray ();
 952                 foreach (Transform tr in tgo) {
 953                     if (tr.GetComponent<T> ())
 954                         meshfilters.Add (tr.GetComponent<T> ());
 955                 }
 956             }
 957             return (T[])meshfilters.ToArray ();
 958         }
 959         else
 960             return (T[])FindObjectsOfType(typeof(T));
 961     }
 962 
 963     private TextureDetails GetTextureDetail(Texture tTexture, Material tMaterial, MaterialDetails tMaterialDetails)
 964     {
 965         TextureDetails tTextureDetails = GetTextureDetail(tTexture);
 966 
 967         tTextureDetails.FoundInMaterials.Add(tMaterial);
 968         foreach (Renderer renderer in tMaterialDetails.FoundInRenderers)
 969         {
 970             if (!tTextureDetails.FoundInRenderers.Contains(renderer)) tTextureDetails.FoundInRenderers.Add(renderer);
 971         }
 972         return tTextureDetails;
 973     }
 974 
 975     private TextureDetails GetTextureDetail(Texture tTexture, Renderer renderer)
 976     {
 977         TextureDetails tTextureDetails = GetTextureDetail(tTexture);
 978 
 979         tTextureDetails.FoundInRenderers.Add(renderer);
 980         return tTextureDetails;
 981     }
 982 
 983     private TextureDetails GetTextureDetail(Texture tTexture, Animator animator)
 984     {
 985         TextureDetails tTextureDetails = GetTextureDetail(tTexture);
 986 
 987         tTextureDetails.FoundInAnimators.Add(animator);
 988         return tTextureDetails;
 989     }
 990 
 991     private TextureDetails GetTextureDetail(Texture tTexture, Graphic graphic)
 992     {
 993         TextureDetails tTextureDetails = GetTextureDetail(tTexture);
 994 
 995         tTextureDetails.FoundInGraphics.Add(graphic);
 996         return tTextureDetails;
 997     }
 998 
 999     private TextureDetails GetTextureDetail(Texture tTexture, MonoBehaviour script)
1000     {
1001         TextureDetails tTextureDetails = GetTextureDetail(tTexture);
1002 
1003         tTextureDetails.FoundInScripts.Add(script);
1004         return tTextureDetails;
1005     }
1006 
1007     private TextureDetails GetTextureDetail(Texture tTexture)
1008     {
1009         TextureDetails tTextureDetails = FindTextureDetails(tTexture);
1010         if (tTextureDetails == null)
1011         {
1012             tTextureDetails = new TextureDetails();
1013             tTextureDetails.texture = tTexture;
1014             tTextureDetails.isCubeMap = tTexture is Cubemap;
1015 
1016             int memSize = CalculateTextureSizeBytes(tTexture);
1017 
1018             TextureFormat tFormat = TextureFormat.RGBA32;
1019             int tMipMapCount = 1;
1020             if (tTexture is Texture2D)
1021             {
1022                 tFormat = (tTexture as Texture2D).format;
1023                 tMipMapCount = (tTexture as Texture2D).mipmapCount;
1024             }
1025             if (tTexture is Cubemap)
1026             {
1027                 tFormat = (tTexture as Cubemap).format;
1028                 memSize = 8 * tTexture.height * tTexture.width;
1029             }
1030             if(tTexture is Texture2DArray){
1031                 tFormat = (tTexture as Texture2DArray).format;
1032                 tMipMapCount = 10;
1033             }
1034 
1035             tTextureDetails.memSizeKB = memSize / 1024;
1036             tTextureDetails.format = tFormat;
1037             tTextureDetails.mipMapCount = tMipMapCount;
1038 
1039         }
1040 
1041         return tTextureDetails;
1042     }
1043 
1044 }
ResourceChecker

相关文章:

  • 2021-08-12
  • 2022-12-23
  • 2021-04-01
  • 2022-12-23
  • 2021-11-29
  • 2022-02-22
  • 2021-06-04
猜你喜欢
  • 2022-12-23
  • 2021-12-12
  • 2022-12-23
  • 2021-08-26
  • 2021-05-21
  • 2021-08-12
  • 2022-12-23
相关资源
相似解决方案