Commits

Tenebrous  committed cabf9b6

Added experimental "highlight unused assets" (enable in prefs / enhancements / project window)

  • Participants
  • Parent commits 1d3bd37

Comments (0)

Files changed (5)

File EditorEnhancements/Editor/Common.cs

 			return value;
 		}
 
+		public static bool Modifier(bool pEnabled, bool pRequireShift, bool pRequireCtrl, bool pRequireAlt)
+		{
+			return pEnabled
+				&& ( !pRequireShift || ( Event.current.modifiers & EventModifiers.Shift   ) != 0 )
+				&& ( !pRequireCtrl  || ( Event.current.modifiers & EventModifiers.Control ) != 0 )
+				&& ( !pRequireAlt   || ( Event.current.modifiers & EventModifiers.Alt     ) != 0 );
+		}
+
+
 		public static Texture GetMiniThumbnail( UnityEngine.Object obj )
 		{
 #if UNITY_4

File EditorEnhancements/Editor/Defaults.cs

 		public const bool ProjectWindowHoverTooltipShift   = false;
 		public const bool ProjectWindowHoverTooltipCtrl    = false;
 		public const bool ProjectWindowHoverTooltipAlt     = false;
+
+		public const bool ProjectWindowUseDependencyChceker = false;
 	}
 }

File EditorEnhancements/Editor/DependencyChecker.cs

+/*
+ * Copyright (c) 2014 Tenebrous
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * Latest version: http://hg.tenebrous.co.uk/unityeditorenhancements/wiki/Home
+*/
+
+using UnityEditor;
+using UnityEngine;
+using System.Collections.Generic;
+using System.IO;
+
+namespace Tenebrous.EditorEnhancements
+{
+	public static class DependencyChecker
+	{
+		private static HashSet<string> _used = new HashSet<string>();
+		private static IEnumerator<string> _assetEnumerator;
+
+		public static void Refresh()
+		{
+			_used.Clear();
+			_assetEnumerator = EnumerateProjectFiles();
+		}
+
+		public static bool Running
+		{
+			get { return _assetEnumerator != null; }
+		}
+
+		public static void Continue()
+		{
+			if (_assetEnumerator != null)
+			{
+				if (_assetEnumerator.MoveNext())
+				{
+					string assetpath = _assetEnumerator.Current;
+					string[] deps = AssetDatabase.GetDependencies(new string[] { assetpath });
+
+					foreach (string dep in deps)
+						if( dep != assetpath )
+							_used.Add( dep );
+
+					Common.ProjectWindow.Repaint();
+				}
+				else
+				{
+					_assetEnumerator = null;
+				}
+			}			
+		}
+
+		static IEnumerator<string> EnumerateProjectFiles()
+		{
+			Queue<DirectoryInfo> folders = new Queue<DirectoryInfo>();
+
+			folders.Enqueue(new DirectoryInfo(Application.dataPath));
+
+			while (folders.Count > 0)
+			{
+				DirectoryInfo current = folders.Dequeue();
+
+				foreach (FileInfo file in current.GetFiles())
+				{
+					if( !CanFileBeDependant( file.Name ) )
+						continue;
+
+					yield return "Assets"
+								 + file.FullName
+								   .Substring(Application.dataPath.Length)
+								   .Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
+				}
+
+				foreach (DirectoryInfo folder in current.GetDirectories())
+				{
+					if( !CanFolderContainDependants(folder.Name) )
+						continue;
+
+					folders.Enqueue(folder);
+				}
+			}
+		}
+
+		public static bool IsUsed( string pPath )
+		{
+			return !CanFileBeDependant(pPath) 
+				|| !CanPathContainDependants(pPath) 
+				|| !CanFileBeDependency( pPath )
+				|| _used.Contains( pPath );
+		}
+
+		static bool CanFolderContainDependants(string pPath)
+		{
+			return !pPath.StartsWith( "." ) 
+				&& pPath != "Editor";
+		}
+
+		static bool CanPathContainDependants(string pPath)
+		{
+			return !pPath.Contains( "/Editor/" );
+		}
+
+		static bool CanFileBeDependant( string pPath )
+		{
+			return !pPath.StartsWith( "." )
+			    && !pPath.EndsWith( ".meta" );
+		}
+
+		static bool CanFileBeDependency( string pPath )
+		{
+			return !pPath.EndsWith( ".unity" );
+		}
+	}
+}

File EditorEnhancements/Editor/HierarchyWindow.cs

 
 			bool currentLock = ( gameObject.hideFlags & HideFlags.NotEditable ) != 0;
 
-		    bool doPreview = _setting_showHoverPreview
-		                     && ( !_setting_showHoverPreviewShift || ( Event.current.modifiers & EventModifiers.Shift   ) != 0 )
-		                     && ( !_setting_showHoverPreviewCtrl  || ( Event.current.modifiers & EventModifiers.Control ) != 0 )
-		                     && ( !_setting_showHoverPreviewAlt   || ( Event.current.modifiers & EventModifiers.Alt     ) != 0 );
-
-			bool doTooltip = _setting_showHoverTooltip
-							 && ( !_setting_showHoverTooltipShift || ( Event.current.modifiers & EventModifiers.Shift   ) != 0 )
-							 && ( !_setting_showHoverTooltipCtrl  || ( Event.current.modifiers & EventModifiers.Control ) != 0 )
-							 && ( !_setting_showHoverTooltipAlt   || ( Event.current.modifiers & EventModifiers.Alt     ) != 0 );
-
-			bool doComponents = _setting_showComponents
-							 && ( !_setting_showComponentsShift || ( Event.current.modifiers & EventModifiers.Shift ) != 0 )
-							 && ( !_setting_showComponentsCtrl || ( Event.current.modifiers & EventModifiers.Control ) != 0 )
-							 && ( !_setting_showComponentsAlt || ( Event.current.modifiers & EventModifiers.Alt ) != 0 );
-
-			bool doLockIcon = _setting_showLock
-							 && ( !_setting_showLockShift  || ( Event.current.modifiers & EventModifiers.Shift   ) != 0 )
-							 && ( !_setting_showLockCtrl   || ( Event.current.modifiers & EventModifiers.Control ) != 0 )
-							 && ( !_setting_showLockAlt    || ( Event.current.modifiers & EventModifiers.Alt     ) != 0 );
-
+			bool doPreview    = Common.Modifier( _setting_showHoverPreview, _setting_showHoverPreviewShift, _setting_showHoverPreviewCtrl, _setting_showHoverPreviewAlt );
+			bool doTooltip    = Common.Modifier( _setting_showHoverTooltip, _setting_showHoverTooltipShift, _setting_showHoverTooltipCtrl, _setting_showHoverTooltipAlt );
+			bool doComponents = Common.Modifier( _setting_showComponents,   _setting_showComponentsShift,   _setting_showComponentsCtrl,   _setting_showComponentsAlt   );
+			bool doLockIcon   = Common.Modifier( _setting_showLock,         _setting_showLockShift,         _setting_showLockCtrl,         _setting_showLockAlt         );
 			doLockIcon |= _setting_showLock && _setting_showLockLocked && currentLock;
 
 

File EditorEnhancements/Editor/ProjectWindow.cs

  * Latest version: http://hg.tenebrous.co.uk/unityeditorenhancements/wiki/Home
 */
 
+#if UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6
+#define UNITY_4
+#endif
+
 using System;
 using System.IO;
 using UnityEngine;
         private bool _setting_showHoverTooltipAlt;
 
         //private bool _setting_showFoldersFirst;
+		private bool _setting_useDependencyChecker;
 
 		private string _lastGUID;
 		private string _currentGUID;
             //SetProjectWindowFoldersFirst( _setting_showFoldersFirst );
 
             if( Common.ProjectWindow != null ) Common.ProjectWindow.Repaint();
+
+			if( _setting_useDependencyChecker )
+				DependencyChecker.Refresh();
         }
 
         public override void OnDisable()
 
         public override string Name
         {
-            get
-            {
-                return "Project Window";
-            }
+            get { return "Project Window"; }
         }
 
         public override string Prefix
         {
-            get
-            {
-                return "TeneProjectWindow";
-            }
+            get { return "TeneProjectWindow"; }
         }
 
 		private void CheckScriptInfo( string pLines )
 			//}
 		}
 
+		public void RefreshDependencies()
+		{
+			if( _setting_useDependencyChecker )
+				DependencyChecker.Refresh();
+		}
+
 		private TeneEnhPreviewWindow _window;
 		private void Update()
 		{
+			if (_setting_useDependencyChecker)
+				if (DependencyChecker.Running)
+					DependencyChecker.Continue();
+
 			if( !_setting_showHoverPreview )
 				return;
 
 
 			string path = Path.GetDirectoryName( assetpath );
 
-            bool doPreview = _setting_showHoverPreview
-                          && ( !_setting_showHoverPreviewShift || ( Event.current.modifiers & EventModifiers.Shift   ) != 0 )
-                          && ( !_setting_showHoverPreviewCtrl  || ( Event.current.modifiers & EventModifiers.Control ) != 0 )
-                          && ( !_setting_showHoverPreviewAlt   || ( Event.current.modifiers & EventModifiers.Alt     ) != 0 );
+			isFolder = (GetFileAttr(assetpath) & FileAttributes.Directory) != 0;
 
-            bool doTooltip = _setting_showHoverTooltip
-                          && ( !_setting_showHoverTooltipShift || ( Event.current.modifiers & EventModifiers.Shift   ) != 0 )
-                          && ( !_setting_showHoverTooltipCtrl  || ( Event.current.modifiers & EventModifiers.Control ) != 0 )
-                          && ( !_setting_showHoverTooltipAlt   || ( Event.current.modifiers & EventModifiers.Alt     ) != 0 );
+			if (_setting_useDependencyChecker && !isFolder && !DependencyChecker.IsUsed(assetpath) )
+			{
+				Color c = GUI.color;
+				GUI.color = new Color(1,0,0,0.1f);
+				Rect r = pDrawingRect;
+				r.width += r.x;
+				r.x = 0;
+				GUI.DrawTexture(r, EditorGUIUtility.whiteTexture);
+				GUI.color = c;
+			}
+
+			bool doPreview = Common.Modifier( _setting_showHoverPreview, _setting_showHoverPreviewShift, _setting_showHoverPreviewCtrl, _setting_showHoverPreviewAlt );
+			bool doTooltip = Common.Modifier( _setting_showHoverTooltip, _setting_showHoverTooltipShift, _setting_showHoverTooltipCtrl, _setting_showHoverTooltipAlt );
 
 			if( doTooltip )
 			{
 					GUI.Label(pDrawingRect, new GUIContent(" ", tooltip));
 			}
 
-			isFolder = ( GetFileAttr( assetpath ) & FileAttributes.Directory ) != 0;
-
 			if( !_setting_showFileCount && isFolder )
 				return;
 
 			_mousePosition = new Vector2(Event.current.mousePosition.x + Common.ProjectWindow.position.x, Event.current.mousePosition.y + Common.ProjectWindow.position.y );
 
-#if UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3
+#if UNITY_4
 			if( Event.current.mousePosition.x < pDrawingRect.width - 16 )
 #endif
 			if( doPreview )
 			}
 			else
 			{
-#if UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3
+#if UNITY_4
 				newRect.width += pDrawingRect.x - (_needHackScrollbarWidthForDrawing ? 16 : 0);
 				newRect.x = 0;
 #else
 				GUILayout.FlexibleSpace();
             EditorGUILayout.EndHorizontal();
 
+			EditorGUILayout.BeginHorizontal();
+			EditorGUILayout.Space();
+				_setting_useDependencyChecker = GUILayout.Toggle(_setting_useDependencyChecker, "");
+			GUILayout.Label("Highlight unused assets (experimental!)");
+			GUILayout.FlexibleSpace();
+			EditorGUILayout.EndHorizontal();
+
 			/*
 						string removeExtension = null;
 						string changeExtension = null;
 			ConvertOldSettings();
 
 			_setting_showExtensionsWhen    = (ShowExtensions)Main.Int[this, "WhenExtensions", (int)Defaults.ProjectWindowExtensionsWhen];
-			_setting_showFileCount         = Main.Bool[this, "FileCount",           Defaults.ProjectWindowFileCount];
+			_setting_showFileCount         = Main.Bool[this, "FileCount",           Defaults.ProjectWindowFileCount         ];
 
-			_setting_showHoverPreview      = Main.Bool[this, "PreviewOnHover",      Defaults.ProjectWindowHoverPreview];
+			_setting_showHoverPreview      = Main.Bool[this, "PreviewOnHover",      Defaults.ProjectWindowHoverPreview      ];
             _setting_showHoverPreviewShift = Main.Bool[this, "PreviewOnHoverShift", Defaults.ProjectWindowHoverPreviewShift ];
-            _setting_showHoverPreviewCtrl  = Main.Bool[this, "PreviewOnHoverCtrl",  Defaults.ProjectWindowHoverPreviewCtrl ];
-            _setting_showHoverPreviewAlt   = Main.Bool[this, "PreviewOnHoverAlt",   Defaults.ProjectWindowHoverPreviewAlt ];
+            _setting_showHoverPreviewCtrl  = Main.Bool[this, "PreviewOnHoverCtrl",  Defaults.ProjectWindowHoverPreviewCtrl  ];
+            _setting_showHoverPreviewAlt   = Main.Bool[this, "PreviewOnHoverAlt",   Defaults.ProjectWindowHoverPreviewAlt   ];
 
-            _setting_showHoverTooltip      = Main.Bool[this, "HoverTooltip",        Defaults.ProjectWindowHoverTooltip ];
+            _setting_showHoverTooltip      = Main.Bool[this, "HoverTooltip",        Defaults.ProjectWindowHoverTooltip      ];
             _setting_showHoverTooltipShift = Main.Bool[this, "HoverTooltipShift",   Defaults.ProjectWindowHoverTooltipShift ];
-            _setting_showHoverTooltipCtrl  = Main.Bool[this, "HoverTooltipCtrl",    Defaults.ProjectWindowHoverTooltipCtrl ];
-            _setting_showHoverTooltipAlt   = Main.Bool[this, "HoverTooltipAlt",     Defaults.ProjectWindowHoverTooltipAlt];
+            _setting_showHoverTooltipCtrl  = Main.Bool[this, "HoverTooltipCtrl",    Defaults.ProjectWindowHoverTooltipCtrl  ];
+            _setting_showHoverTooltipAlt   = Main.Bool[this, "HoverTooltipAlt",     Defaults.ProjectWindowHoverTooltipAlt   ];
 
             //_setting_showFoldersFirst      = Main.Bool[this, "ShowFoldersFirst",    Application.platform != RuntimePlatform.OSXEditor];
 
+			_setting_useDependencyChecker  = Main.Bool[this, "DependencyChecker",   Defaults.ProjectWindowUseDependencyChceker ];
+
 			//string colormap = Common.GetLongPref("TeneProjectWindow_ColorMap");
         }
 
             Main.Bool[ this, "HoverTooltipCtrl"    ] = _setting_showHoverTooltipCtrl;
             Main.Bool[ this, "HoverTooltipAlt"     ] = _setting_showHoverTooltipAlt;
             //Main.Bool[ this, "ShowFoldersFirst"    ] = _setting_showFoldersFirst;
-        }
+
+            Main.Bool[ this, "DependencyChecker"   ] = _setting_useDependencyChecker;
+		}
 	}
 
     public class ProjectWindowExtensionsClass : AssetPostprocessor
 
             foreach( string file in pMoveFrom )
                 proj.ClearCache( file );
-        }
+		}
     }
+
+
+	public class ProjectWindowAMP : AssetModificationProcessor
+	{
+		private static void OnWillSaveAssets(string[] pPaths)
+		{
+			TeneProjectWindow proj = Main.Enhancement<TeneProjectWindow>();
+			if (proj != null)
+				proj.RefreshDependencies();
+		}
+	}
 }