Created
April 17, 2025 02:46
-
-
Save MizoTake/bb2f4d17bd83631e4e580dc8ad1ece32 to your computer and use it in GitHub Desktop.
UnityのAssetをサイズ順に並べてくれるEditor拡張
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEditor; | |
| using UnityEngine; | |
| using System.IO; | |
| using System.Linq; | |
| using System.Collections.Generic; | |
| public class AssetSizeViewer : EditorWindow | |
| { | |
| private Vector2 scroll; | |
| private List<(string path, long size)> assetSizes = new(); | |
| [MenuItem("Tools/Asset Size Viewer")] | |
| static void Open() | |
| { | |
| GetWindow<AssetSizeViewer>("Asset Size Viewer"); | |
| } | |
| private void OnGUI() | |
| { | |
| EditorGUILayout.Space(); | |
| EditorGUILayout.LabelField("選択中のフォルダ配下のアセットをサイズ順に表示", EditorStyles.boldLabel); | |
| if (GUILayout.Button("🔄 リフレッシュ")) | |
| { | |
| RefreshAssetList(); | |
| } | |
| EditorGUILayout.Space(); | |
| scroll = EditorGUILayout.BeginScrollView(scroll); | |
| if (assetSizes.Count == 0) | |
| { | |
| EditorGUILayout.HelpBox("リストが空です。Projectでフォルダを選択して「リフレッシュ」してください。", MessageType.Info); | |
| } | |
| else | |
| { | |
| foreach (var (path, size) in assetSizes) | |
| { | |
| string formattedSize = FormatBytes(size); | |
| EditorGUILayout.BeginVertical("box"); | |
| EditorGUILayout.BeginHorizontal(); | |
| if (GUILayout.Button("選択", GUILayout.Width(60))) | |
| { | |
| var obj = AssetDatabase.LoadAssetAtPath<Object>(path); | |
| Selection.activeObject = obj; | |
| EditorGUIUtility.PingObject(obj); | |
| } | |
| EditorGUILayout.LabelField($"{formattedSize}", GUILayout.Width(80)); | |
| EditorGUILayout.LabelField(path); | |
| EditorGUILayout.EndHorizontal(); | |
| EditorGUILayout.EndVertical(); | |
| } | |
| } | |
| EditorGUILayout.EndScrollView(); | |
| } | |
| private void RefreshAssetList() | |
| { | |
| assetSizes.Clear(); | |
| string[] selectedFolders = Selection.GetFiltered<Object>(SelectionMode.Assets) | |
| .Select(AssetDatabase.GetAssetPath) | |
| .Where(AssetDatabase.IsValidFolder) | |
| .ToArray(); | |
| if (selectedFolders.Length == 0) | |
| { | |
| Debug.LogWarning("フォルダを選択してください。"); | |
| return; | |
| } | |
| HashSet<string> includedPaths = new(); | |
| foreach (var folder in selectedFolders) | |
| { | |
| string[] guids = AssetDatabase.FindAssets("", new[] { folder }); | |
| foreach (var guid in guids) | |
| { | |
| string path = AssetDatabase.GUIDToAssetPath(guid); | |
| if (File.Exists(path)) | |
| includedPaths.Add(path); | |
| } | |
| } | |
| assetSizes = includedPaths | |
| .Select(path => (path, new FileInfo(path).Length)) | |
| .OrderByDescending(x => x.Item2) | |
| .ToList(); | |
| } | |
| private string FormatBytes(long bytes) { | |
| return bytes switch { | |
| >= 1L << 30 => $"{(bytes / (1f * (1L << 30))):F2} GB", | |
| >= 1L << 20 => $"{(bytes / (1f * (1L << 20))):F2} MB", | |
| >= 1L << 10 => $"{(bytes / (1f * (1L << 10))):F2} KB", | |
| _ => $"{bytes} B" | |
| }; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment