Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- using System.IO;
- using System;
- public class MemoryWindow : EditorWindow
- {
- [MenuItem("Window/Get used assets window")]
- public static void ShowWindow()
- {
- if(Comparer == null)
- Comparer = new AssetComparer();
- MemoryWindow window = EditorWindow.GetWindow<MemoryWindow>();
- window.Show();
- }
- private Vector2 scroll;
- public static string [] GetResourcesPath()
- {
- string assetsFolder = Application.dataPath;
- // 1 - Find resources folders from Assets folder recursively
- string[] resourcesFolders = Directory.GetDirectories(assetsFolder, "Resources", SearchOption.AllDirectories);
- // 2 - resources folders are now relative to Assets' parent folder.
- for (int i = 0; i < resourcesFolders.Length; i++)
- resourcesFolders[i] = resourcesFolders[i].Replace(assetsFolder, "Assets");
- // 3 - Extract all resources from resources folders
- string[] resourcesGuid = AssetDatabase.FindAssets("*", resourcesFolders);
- // 4 - Get resources path
- string[] resourcesPath = new string[resourcesGuid.Length];
- for (int i = 0; i < resourcesGuid.Length; i++)
- {
- resourcesPath[i] = AssetDatabase.GUIDToAssetPath(resourcesGuid[i]);
- }
- return resourcesPath;
- }
- public class AssetComparer : IComparer<AssetData>
- {
- int IComparer<AssetData>.Compare(AssetData x, AssetData y)
- {
- int c = y.Size.CompareTo(x.Size); // decr.
- if (c == 0)
- c = x.Path.CompareTo(y.Path); // cr.
- return c;
- }
- }
- class AssetData : IEquatable<string>, IEquatable<AssetData>
- {
- public readonly string Path;
- public string Guid { get; private set; }
- public long Size { get; private set; }
- public string SizeStr { get; private set; }
- public AssetData(string path, bool fetchData = true)
- {
- this.Path = path;
- if (fetchData)
- Refresh();
- }
- public void Refresh()
- {
- FileInfo info = new FileInfo(Application.dataPath.Replace("Assets", "") + this.Path);
- this.Guid = AssetDatabase.AssetPathToGUID(this.Path);
- string libraryCacheAssetPath = Application.dataPath + "../../Library/metadata/" + this.Guid.Substring(0, 2) + "/" + this.Guid;
- if (File.Exists(libraryCacheAssetPath))
- {
- FileInfo libraryCache = new FileInfo(libraryCacheAssetPath);
- this.Size = libraryCache.Length;
- }
- else
- {
- //this.Size = info.Length;
- }
- this.SizeStr = GetSizeStr(this.Size);
- }
- public static string GetSizeStr(long size)
- {
- float f_size = size;
- const long k = 1024;
- const long m = 1024 * 1024;
- const long g = 1024 * 1024 * 1024;
- const float ik = 1f / k;
- const float im = 1f / m;
- const float ig = 1f / g;
- if (f_size >= g)
- {
- f_size = size * ig;
- return f_size.ToString("F2") + " Gio";
- }
- if (f_size >= m)
- {
- f_size = size * im;
- return f_size.ToString("F2") + " Mio";
- }
- if (size >= k)
- {
- f_size = size * ik;
- return f_size.ToString("F2") + " kio";
- }
- return size + " o";
- }
- public override string ToString()
- {
- return Path;
- }
- bool IEquatable<string>.Equals(string path)
- {
- return this.Path.Equals(path);
- }
- bool IEquatable<AssetData>.Equals(AssetData other)
- {
- return this.Path.Equals(other.Path);
- }
- public override bool Equals(object obj)
- {
- if (obj is string)
- return ((IEquatable<string>)this).Equals(obj as string);
- if (obj is AssetData)
- return ((IEquatable<AssetData>)this).Equals(obj as AssetData);
- return false;
- }
- public override int GetHashCode()
- {
- return this.Path.GetHashCode();
- }
- }
- static List<AssetData> Assets;
- private static AssetComparer comparer;
- public static AssetComparer Comparer
- {
- get
- {
- return comparer;
- }
- set
- {
- if (comparer == value)
- return;
- if(value == null)
- {
- comparer = null;
- }
- else
- {
- comparer = value;
- if (Assets != null)
- Assets.Sort(comparer);
- }
- }
- }
- void Refresh()
- {
- List<string> rootAssets = new List<string>();
- // 1 - Get resources paths
- rootAssets.AddRange(GetResourcesPath());
- // 2 - Get scenes paths
- foreach(EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
- rootAssets.Add(scene.path);
- // 3 - Get Assets dependencies
- string[] dependencies = AssetDatabase.GetDependencies(rootAssets.ToArray(), true);
- // 3 - Add rootAssets and its dependencies into assets
- List <AssetData> assets = new List<AssetData>(rootAssets.Count + dependencies.Length);
- FillAssetsList(rootAssets, assets);
- FillAssetsList(dependencies, assets);
- if (Comparer == null)
- Comparer = new AssetComparer();
- assets.Sort(Comparer);
- // Set the assets object
- Assets = assets;
- }
- private static void FillAssetsList(IEnumerable<string> assetPaths, List<AssetData> assets)
- {
- string dataPath = Application.dataPath.Replace("Assets", "");
- AssetData asset;
- foreach (string assetPath in assetPaths)
- {
- // If asset is a directory...
- if ((File.GetAttributes(dataPath + assetPath) & FileAttributes.Directory) != 0)
- continue;
- asset = new AssetData(assetPath, false);
- if (!assets.Contains(asset))
- {
- asset.Refresh();
- assets.Add(asset);
- }
- }
- }
- void OnGUI()
- {
- if (EditorApplication.isPlaying)
- {
- EditorGUILayout.LabelField("Doesn't work while playing");
- return;
- }
- EditorGUILayout.BeginHorizontal();
- if(GUILayout.Button("Refresh"))
- {
- Refresh();
- }
- if(Assets != null)
- {
- if(GUILayout.Button("Clear"))
- {
- Assets = null;
- }
- }
- EditorGUILayout.EndHorizontal();
- if(Assets != null)
- {
- long totalSize = 0;
- foreach (AssetData asset in Assets)
- totalSize += asset.Size;
- EditorGUILayout.LabelField(AssetData.GetSizeStr(totalSize), "Total");
- scroll = EditorGUILayout.BeginScrollView(scroll);
- foreach (AssetData asset in Assets)
- {
- EditorGUILayout.BeginHorizontal();
- EditorGUILayout.LabelField(asset.SizeStr, asset.Path);
- if (GUILayout.Button("Ping", GUILayout.Width(50)))
- EditorGUIUtility.PingObject(AssetDatabase.LoadMainAssetAtPath(asset.Path));
- EditorGUILayout.EndHorizontal();
- }
- EditorGUILayout.EndScrollView();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement