12 changed files with 190 additions and 26 deletions
@ -1,8 +0,0 @@ |
|||||
fileFormatVersion: 2 |
|
||||
guid: 344d15cb1e8bb3d4f938bacdd1ca85b9 |
|
||||
folderAsset: yes |
|
||||
DefaultImporter: |
|
||||
externalObjects: {} |
|
||||
userData: |
|
||||
assetBundleName: |
|
||||
assetBundleVariant: |
|
||||
@ -1,7 +0,0 @@ |
|||||
fileFormatVersion: 2 |
|
||||
guid: 146af9f28785df94e96dd4051f3c1f0e |
|
||||
DefaultImporter: |
|
||||
externalObjects: {} |
|
||||
userData: |
|
||||
assetBundleName: |
|
||||
assetBundleVariant: |
|
||||
@ -1,7 +0,0 @@ |
|||||
fileFormatVersion: 2 |
|
||||
guid: 63abef4a9c33fec46880ba4a9d4a2499 |
|
||||
DefaultImporter: |
|
||||
externalObjects: {} |
|
||||
userData: |
|
||||
assetBundleName: |
|
||||
assetBundleVariant: |
|
||||
@ -0,0 +1,178 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.IO; |
||||
|
using System.Linq; |
||||
|
using System.Text.RegularExpressions; |
||||
|
using UnityEditor; |
||||
|
using UnityEditor.SceneManagement; |
||||
|
using UnityEngine; |
||||
|
using UnityEngine.SceneManagement; |
||||
|
|
||||
|
[InitializeOnLoad] |
||||
|
public static class UnusedAssetsMarker |
||||
|
{ |
||||
|
private struct FolderInfo |
||||
|
{ |
||||
|
public int usedCount; |
||||
|
public int unusedCount; |
||||
|
} |
||||
|
|
||||
|
private static List<string> _usedAssets = new List<string>(); |
||||
|
private static List<string> _allAssets = new List<string>(); |
||||
|
|
||||
|
private static Dictionary<string, FolderInfo> _foldersInfo = new Dictionary<string, FolderInfo>(); |
||||
|
private static Dictionary<string, bool> _assetsInfo = new Dictionary<string, bool>(); |
||||
|
|
||||
|
static UnusedAssetsMarker() |
||||
|
{ |
||||
|
ResearchDependecies(); |
||||
|
|
||||
|
EditorApplication.projectWindowItemOnGUI += DrawAssetDetails; |
||||
|
} |
||||
|
|
||||
|
[MenuItem("Core/Research unused assets", false, 0)] |
||||
|
private static void ResearchDependecies() |
||||
|
{ |
||||
|
_usedAssets = new List<string>(); |
||||
|
_allAssets = AssetDatabase.GetAllAssetPaths().ToList(); |
||||
|
_foldersInfo = new Dictionary<string, FolderInfo>(); |
||||
|
|
||||
|
for (int i = 0; i < EditorBuildSettings.scenes.Length; i++) |
||||
|
{ |
||||
|
string scenePath = EditorBuildSettings.scenes[i].path; |
||||
|
string[] dependencies = AssetDatabase.GetDependencies(scenePath); |
||||
|
_usedAssets.AddRange(dependencies); |
||||
|
} |
||||
|
|
||||
|
Object[] resources = Resources.LoadAll(""); |
||||
|
|
||||
|
for (int i = 0; i < resources.Length; i++) |
||||
|
{ |
||||
|
_usedAssets.AddRange(AssetDatabase.GetDependencies(AssetDatabase.GetAssetPath(resources[i]))); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private static void DrawAssetDetails(string guid, Rect rect) |
||||
|
{ |
||||
|
string assetPath = AssetDatabase.GUIDToAssetPath(guid); |
||||
|
|
||||
|
if (Application.isPlaying || Event.current.type != EventType.Repaint || IsInvalidFile(assetPath) || assetPath == "Assets") |
||||
|
{ |
||||
|
GUI.backgroundColor = Color.white; |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
if (AssetDatabase.IsValidFolder(assetPath)) |
||||
|
{ |
||||
|
if (IsMainListAsset(rect)) |
||||
|
{ |
||||
|
int usedCount = 0; |
||||
|
int unusedCount = 0; |
||||
|
|
||||
|
if (_foldersInfo.ContainsKey(assetPath)) |
||||
|
{ |
||||
|
usedCount = _foldersInfo[assetPath].usedCount; |
||||
|
unusedCount = _foldersInfo[assetPath].unusedCount; |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
string[] assetsInFolder = _allAssets.Where(path => path.Contains(assetPath) && !AssetDatabase.IsValidFolder(path)).ToArray(); |
||||
|
|
||||
|
for (int i = 0; i < assetsInFolder.Length; i++) |
||||
|
{ |
||||
|
if (IsAssetsUsed(assetsInFolder[i]) || IsInvalidFile(assetsInFolder[i])) |
||||
|
usedCount++; |
||||
|
else |
||||
|
unusedCount++; |
||||
|
} |
||||
|
|
||||
|
_foldersInfo.Add(assetPath, new FolderInfo { usedCount = usedCount, unusedCount = unusedCount }); |
||||
|
} |
||||
|
|
||||
|
GUIStyle style = new GUIStyle(EditorStyles.label); |
||||
|
style.alignment = TextAnchor.MiddleRight; |
||||
|
|
||||
|
Rect newRect = rect; |
||||
|
newRect.x += rect.width - 305; |
||||
|
newRect.width = 200; |
||||
|
|
||||
|
GUI.Label(newRect, $"Used assets count: {usedCount}"); |
||||
|
|
||||
|
newRect = rect; |
||||
|
newRect.x += rect.width - 155; |
||||
|
newRect.width = 200; |
||||
|
|
||||
|
GUI.Label(newRect, $"Unused assets count: {unusedCount}"); |
||||
|
} |
||||
|
|
||||
|
GUI.backgroundColor = Color.white; |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
if (AssetDatabase.LoadAssetAtPath<Object>(assetPath) != null) { |
||||
|
|
||||
|
bool isUsed = false; |
||||
|
|
||||
|
if (_assetsInfo.ContainsKey(assetPath)) |
||||
|
isUsed = _assetsInfo[assetPath]; |
||||
|
else |
||||
|
{ |
||||
|
isUsed = IsAssetsUsed(assetPath); |
||||
|
_assetsInfo.Add(assetPath, isUsed); |
||||
|
} |
||||
|
|
||||
|
if (isUsed) |
||||
|
{ |
||||
|
GUI.backgroundColor = Color.white; |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
if (IsMainListAsset(rect)) |
||||
|
{ |
||||
|
Rect newRect = rect; |
||||
|
newRect.x += rect.width - 55; |
||||
|
newRect.width = 100; |
||||
|
|
||||
|
GUIStyle style = new GUIStyle(EditorStyles.label); |
||||
|
style.alignment = TextAnchor.MiddleRight; |
||||
|
|
||||
|
GUI.Label(newRect, "Unused"); |
||||
|
} |
||||
|
|
||||
|
if (isUsed) |
||||
|
GUI.color = Color.clear; |
||||
|
else |
||||
|
GUI.color = Color.red; |
||||
|
|
||||
|
GUI.Box(rect, ""); |
||||
|
|
||||
|
GUI.color = Color.white; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private static bool IsMainListAsset(Rect rect) |
||||
|
{ |
||||
|
if (rect.height > 20) |
||||
|
return false; |
||||
|
|
||||
|
if (rect.x > 16) |
||||
|
return false; |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
private static bool IsInvalidFile(string path) |
||||
|
{ |
||||
|
return |
||||
|
Path.GetExtension(path) == ".cs" || |
||||
|
Path.GetExtension(path) == ".dll" || |
||||
|
Path.GetExtension(path) == ".unity" || |
||||
|
Regex.IsMatch(path, "[\\/\\\\]Editor[\\/\\\\]") == true || |
||||
|
Regex.IsMatch(path, "[\\/\\\\]Resources[\\/\\\\]") == true || |
||||
|
Regex.IsMatch(path, "[\\/\\\\]Plugins[\\/\\\\]") == true; |
||||
|
} |
||||
|
|
||||
|
private static bool IsAssetsUsed(string path) |
||||
|
{ |
||||
|
return _usedAssets.Contains(path); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
fileFormatVersion: 2 |
||||
|
guid: 58c6268c557aa8c4fa61e6db42d2af44 |
||||
|
MonoImporter: |
||||
|
externalObjects: {} |
||||
|
serializedVersion: 2 |
||||
|
defaultReferences: [] |
||||
|
executionOrder: 0 |
||||
|
icon: {instanceID: 0} |
||||
|
userData: |
||||
|
assetBundleName: |
||||
|
assetBundleVariant: |
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue