Advertisement
Guest User

Untitled

a guest
Jun 8th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.24 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.IO;
  5. using System;
  6.  
  7. public class MemoryWindow : EditorWindow
  8. {
  9.     [MenuItem("Window/Get used assets window")]
  10.     public static void ShowWindow()
  11.     {
  12.         if(Comparer == null)
  13.             Comparer = new AssetComparer();
  14.  
  15.         MemoryWindow window = EditorWindow.GetWindow<MemoryWindow>();
  16.         window.Show();
  17.     }
  18.  
  19.  
  20.     private Vector2 scroll;
  21.  
  22.     public static string [] GetResourcesPath()
  23.     {
  24.         string assetsFolder = Application.dataPath;
  25.  
  26.         // 1 - Find resources folders from Assets folder recursively
  27.         string[] resourcesFolders = Directory.GetDirectories(assetsFolder, "Resources", SearchOption.AllDirectories);
  28.  
  29.         // 2 - resources folders are now relative to Assets' parent folder.
  30.         for (int i = 0; i < resourcesFolders.Length; i++)
  31.             resourcesFolders[i] = resourcesFolders[i].Replace(assetsFolder, "Assets");
  32.  
  33.         // 3 - Extract all resources from resources folders
  34.         string[] resourcesGuid = AssetDatabase.FindAssets("*", resourcesFolders);
  35.  
  36.         // 4 - Get resources path
  37.         string[] resourcesPath = new string[resourcesGuid.Length];
  38.         for (int i = 0; i < resourcesGuid.Length; i++)
  39.         {
  40.             resourcesPath[i] = AssetDatabase.GUIDToAssetPath(resourcesGuid[i]);
  41.         }
  42.  
  43.         return resourcesPath;
  44.     }
  45.  
  46.     public class AssetComparer : IComparer<AssetData>
  47.     {
  48.  
  49.  
  50.         int IComparer<AssetData>.Compare(AssetData x, AssetData y)
  51.         {
  52.             int c = y.Size.CompareTo(x.Size); // decr.
  53.             if (c == 0)
  54.                 c = x.Path.CompareTo(y.Path); // cr.
  55.             return c;
  56.         }
  57.     }
  58.  
  59.     class AssetData : IEquatable<string>, IEquatable<AssetData>
  60.     {
  61.         public readonly string Path;
  62.         public string Guid { get; private set; }
  63.  
  64.         public long Size { get; private set; }
  65.         public string SizeStr { get; private set; }
  66.  
  67.         public AssetData(string path, bool fetchData = true)
  68.         {
  69.             this.Path = path;
  70.  
  71.             if (fetchData)
  72.                 Refresh();
  73.         }
  74.  
  75.         public void Refresh()
  76.         {
  77.             FileInfo info = new FileInfo(Application.dataPath.Replace("Assets", "") + this.Path);
  78.  
  79.             this.Guid = AssetDatabase.AssetPathToGUID(this.Path);
  80.             string libraryCacheAssetPath = Application.dataPath + "../../Library/metadata/" + this.Guid.Substring(0, 2) + "/" + this.Guid;
  81.             if (File.Exists(libraryCacheAssetPath))
  82.             {
  83.                 FileInfo libraryCache = new FileInfo(libraryCacheAssetPath);
  84.                 this.Size = libraryCache.Length;
  85.             }
  86.             else
  87.             {
  88.                 //this.Size = info.Length;
  89.             }
  90.  
  91.             this.SizeStr = GetSizeStr(this.Size);
  92.         }
  93.  
  94.         public static string GetSizeStr(long size)
  95.         {
  96.             float f_size = size;
  97.  
  98.             const long k = 1024;
  99.             const long m = 1024 * 1024;
  100.             const long g = 1024 * 1024 * 1024;
  101.             const float ik = 1f / k;
  102.             const float im = 1f / m;
  103.             const float ig = 1f / g;
  104.  
  105.             if (f_size >= g)
  106.             {
  107.                 f_size = size * ig;
  108.                 return f_size.ToString("F2") + " Gio";
  109.             }
  110.  
  111.             if (f_size >= m)
  112.             {
  113.                 f_size = size * im;
  114.                 return f_size.ToString("F2") + " Mio";
  115.             }
  116.  
  117.             if (size >= k)
  118.             {
  119.                 f_size = size * ik;
  120.                 return f_size.ToString("F2") + " kio";
  121.             }
  122.  
  123.             return size + " o";
  124.         }
  125.  
  126.         public override string ToString()
  127.         {
  128.             return Path;
  129.         }
  130.  
  131.         bool IEquatable<string>.Equals(string path)
  132.         {
  133.             return this.Path.Equals(path);
  134.         }
  135.  
  136.         bool IEquatable<AssetData>.Equals(AssetData other)
  137.         {
  138.             return this.Path.Equals(other.Path);
  139.         }
  140.  
  141.         public override bool Equals(object obj)
  142.         {
  143.             if (obj is string)
  144.                 return ((IEquatable<string>)this).Equals(obj as string);
  145.  
  146.             if (obj is AssetData)
  147.                 return ((IEquatable<AssetData>)this).Equals(obj as AssetData);
  148.  
  149.             return false;
  150.         }
  151.  
  152.         public override int GetHashCode()
  153.         {
  154.             return this.Path.GetHashCode();
  155.         }
  156.     }
  157.  
  158.     static List<AssetData> Assets;
  159.  
  160.     private static AssetComparer comparer;
  161.     public static AssetComparer Comparer
  162.     {
  163.         get
  164.         {
  165.             return comparer;
  166.         }
  167.         set
  168.         {
  169.             if (comparer == value)
  170.                 return;
  171.  
  172.             if(value == null)
  173.             {
  174.                 comparer = null;
  175.             }
  176.             else
  177.             {
  178.                 comparer = value;
  179.                 if (Assets != null)
  180.                     Assets.Sort(comparer);
  181.             }
  182.         }
  183.     }
  184.  
  185.     void Refresh()
  186.     {
  187.         List<string> rootAssets = new List<string>();
  188.  
  189.         // 1 - Get resources paths
  190.         rootAssets.AddRange(GetResourcesPath());
  191.  
  192.         // 2 - Get scenes paths
  193.         foreach(EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
  194.             rootAssets.Add(scene.path);
  195.  
  196.         // 3 - Get Assets dependencies
  197.         string[] dependencies = AssetDatabase.GetDependencies(rootAssets.ToArray(), true);
  198.  
  199.         // 3 - Add rootAssets and its dependencies into assets 
  200.         List <AssetData> assets = new List<AssetData>(rootAssets.Count + dependencies.Length);
  201.        
  202.         FillAssetsList(rootAssets, assets);
  203.         FillAssetsList(dependencies, assets);
  204.  
  205.         if (Comparer == null)
  206.             Comparer = new AssetComparer();
  207.  
  208.         assets.Sort(Comparer);
  209.  
  210.         // Set the assets object
  211.         Assets = assets;
  212.     }
  213.  
  214.     private static void FillAssetsList(IEnumerable<string> assetPaths, List<AssetData> assets)
  215.     {
  216.         string dataPath = Application.dataPath.Replace("Assets", "");
  217.  
  218.         AssetData asset;
  219.         foreach (string assetPath in assetPaths)
  220.         {
  221.             // If asset is a directory...
  222.             if ((File.GetAttributes(dataPath + assetPath) & FileAttributes.Directory) != 0)
  223.                 continue;
  224.  
  225.             asset = new AssetData(assetPath, false);
  226.             if (!assets.Contains(asset))
  227.             {
  228.                 asset.Refresh();
  229.                 assets.Add(asset);
  230.             }
  231.         }
  232.     }
  233.  
  234.     void OnGUI()
  235.     {
  236.         if (EditorApplication.isPlaying)
  237.         {
  238.             EditorGUILayout.LabelField("Doesn't work while playing");
  239.             return;
  240.         }
  241.  
  242.         EditorGUILayout.BeginHorizontal();
  243.  
  244.         if(GUILayout.Button("Refresh"))
  245.         {
  246.             Refresh();
  247.         }
  248.  
  249.         if(Assets != null)
  250.         {
  251.             if(GUILayout.Button("Clear"))
  252.             {
  253.                 Assets = null;
  254.             }
  255.         }
  256.  
  257.         EditorGUILayout.EndHorizontal();
  258.  
  259.  
  260.         if(Assets != null)
  261.         {
  262.             long totalSize = 0;
  263.  
  264.             foreach (AssetData asset in Assets)
  265.                 totalSize += asset.Size;
  266.  
  267.             EditorGUILayout.LabelField(AssetData.GetSizeStr(totalSize), "Total");
  268.  
  269.             scroll = EditorGUILayout.BeginScrollView(scroll);
  270.             foreach (AssetData asset in Assets)
  271.             {
  272.                 EditorGUILayout.BeginHorizontal();
  273.                 EditorGUILayout.LabelField(asset.SizeStr, asset.Path);
  274.                 if (GUILayout.Button("Ping", GUILayout.Width(50)))
  275.                     EditorGUIUtility.PingObject(AssetDatabase.LoadMainAssetAtPath(asset.Path));
  276.                 EditorGUILayout.EndHorizontal();
  277.             }
  278.             EditorGUILayout.EndScrollView();
  279.         }
  280.  
  281.     }
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement