internal void SetAssetInfo(UnityEngine.Object obj, string path)
        {
            long fileSize = -1;

            fileSize = getImportedFileSize(path);

            m_assetActualSize = fileSize;
            //SetSize();
            m_assetSizeString = (m_assetActualSize != -1) ? AssetHunterHelper.SizeSuffix(m_assetActualSize, 1) : "NA";

            this.m_path = path;
            //string[] parts = path.Split('/');
            this.m_name = obj.name;// parts[parts.Length - 1];

            m_assetGUID = UnityEditor.AssetDatabase.AssetPathToGUID(path);
            m_type      = new AssetHunterSerializableSystemType(obj.GetType());

            AssetHunterHelper.UnloadUnused();
        }
Example #2
0
        internal void SetAssetInfo(UnityEngine.Object obj, string path)
        {
            long fileSize;

#if UNITY_2017_1_OR_NEWER
            fileSize = Profiler.GetRuntimeMemorySizeLong(obj);
#else
            fileSize = Profiler.GetRuntimeMemorySize(obj);
#endif
            SetSize(fileSize);

            this.m_path = path;
            //string[] parts = path.Split('/');
            this.m_name = obj.name;// parts[parts.Length - 1];

            m_assetGUID = UnityEditor.AssetDatabase.AssetPathToGUID(path);
            m_type      = new AssetHunterSerializableSystemType(obj.GetType());

            AssetHunterHelper.UnloadUnused();
        }
        private void OnGUI()
        {
            if (settings == null)
            {
                settings = AssetHunterSettings.LoadSettings();
            }

            scrollPos = EditorGUILayout.BeginScrollView(scrollPos);

            //Show all used types
            EditorGUILayout.BeginVertical();
            string selectedPath = AssetDatabase.GetAssetPath(Selection.activeObject);

            //Make sure this window has focus to update contents
            AssetHunterSettingsWindow.Instance.Repaint();

            EditorGUILayout.Separator();
            GUILayout.Label("This is the settingswindow for Asset Hunter! " + System.Environment.NewLine + "-Choose folders, types or filenames to exclude when scanning the project", EditorStyles.boldLabel);
            GUILayout.Label("NB: If your project window is in \"Two column layout\" you need to select folders in the right hand side of that window", EditorStyles.miniLabel);

            GUILayout.Label("----------------------------------------------------------------------------", EditorStyles.boldLabel);
            //Force memorycleanup
            settings.m_MemoryCleanupActive = GUILayout.Toggle(settings.m_MemoryCleanupActive, "Force memory cleanup");
            GUILayout.Label("Enable this if you experience memory crashes (Much slower)", EditorStyles.miniLabel);
            GUILayout.Label("----------------------------------------------------------------------------", EditorStyles.boldLabel);

            EditorGUILayout.Separator();
            EditorGUILayout.Separator();

            //Do we have a folder selected
            bool bFolderSelected = System.IO.Directory.Exists(selectedPath);
            //Is it valid
            bool validSelection = (/*bFolderSelected && */ settings.ValidateDirectory(Selection.activeObject));

            //Select folder to exclude
            EditorGUILayout.BeginHorizontal();

            GUI.color = (validSelection ? AssetHunterHelper.AH_BLUE : AssetHunterHelper.AH_GREY);

            if (GUILayout.Button(validSelection ? "Exclude selected folder" : "No valid folder selected", GUILayout.Width(btnMinWidthLarge)))
            {
                if (validSelection)
                {
                    AssetHunterSettings.ExcludeDirectory(Selection.activeObject);
                }
            }

            GUI.color = m_IntialGUIColor;

            if (validSelection)
            {
                GUILayout.Label(selectedPath, EditorStyles.miniBoldLabel);
            }

            GUI.color = m_IntialGUIColor;
            GUILayout.FlexibleSpace();
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Separator();

            //Select type to exclude
            EditorGUILayout.BeginHorizontal();
            AssetHunterSerializableSystemType selectedType = null;

            if (Selection.activeObject)
            {
                selectedType = new AssetHunterSerializableSystemType(Selection.activeObject.GetType());
            }

            //Do we have a valid asset selected
            validSelection = (selectedType != null && !bFolderSelected && settings.ValidateType(selectedType));

            GUI.color = (validSelection ? AssetHunterHelper.AH_BLUE : AssetHunterHelper.AH_GREY);

            if (GUILayout.Button(validSelection ? "Exclude selected type" : "No valid type selected", GUILayout.Width(btnMinWidthLarge)))
            {
                if (validSelection)
                {
                    AssetHunterSettings.ExcludeType(selectedType);
                }
            }

            if (validSelection)
            {
                GUILayout.Label(selectedType.SystemType.ToString(), EditorStyles.miniBoldLabel);
            }

            GUI.color = m_IntialGUIColor;

            GUILayout.FlexibleSpace();
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Separator();

            //Exluded filename substrings
            EditorGUILayout.BeginHorizontal();

            validSelection = !string.IsNullOrEmpty(m_excludeSubstringInput) && settings.ValidateSubstring(m_excludeSubstringInput);
            GUI.color      = (validSelection ? AssetHunterHelper.AH_BLUE : AssetHunterHelper.AH_GREY);

            bool bHasHitEnter = false;

            Event e = Event.current;

            if (e.keyCode == KeyCode.Return)
            {
                bHasHitEnter = true;
            }

            if (bHasHitEnter || GUILayout.Button(validSelection ? "Exclude substring" : "No valid search string", GUILayout.Width(btnMinWidthLarge)))
            {
                if (validSelection)
                {
                    AssetHunterSettings.ExcludeSubstring(m_excludeSubstringInput);
                    //settings.ExcludeSubstring(m_excludeSubstringInput);
                    m_excludeSubstringInput = string.Empty;
                }
            }

            GUI.color = m_IntialGUIColor;
            m_excludeSubstringInput = GUILayout.TextField(m_excludeSubstringInput);

            //GUILayout.FlexibleSpace();
            EditorGUILayout.EndHorizontal();
            if (validSelection)
            {
                GUILayout.Label(string.Format("Will exclude any asset with \"{0}\" in its path/name (might make asset hunter perform slower)", m_excludeSubstringInput));
            }

            EditorGUILayout.Separator();
            EditorGUILayout.Separator();

            //Select type to exclude
            EditorGUILayout.BeginHorizontal();

            //Do we have a valid asset selected
            validSelection = Selection.activeObject != null && (settings.ValidateAssetGUID(Selection.activeObject));

            GUI.color = (validSelection ? AssetHunterHelper.AH_BLUE : AssetHunterHelper.AH_GREY);

            if (GUILayout.Button(validSelection ? "Exclude selected asset" : "No project asset selected", GUILayout.Width(btnMinWidthLarge)))
            {
                if (validSelection)
                {
                    AssetHunterSettings.ExcludeIndividualAssetWithObject(Selection.activeObject);
                }
            }

            if (validSelection)
            {
                GUILayout.Label(Selection.activeObject.name, EditorStyles.miniBoldLabel);
            }

            GUI.color = m_IntialGUIColor;

            GUILayout.FlexibleSpace();
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Separator();

            GUILayout.Label("---------------------------Excluded Folders------------------------------", EditorStyles.boldLabel);

            if (settings.m_DirectoryExcludes.Count >= 1)
            {
                for (int i = settings.m_DirectoryExcludes.Count - 1; i >= 0; i--)
                {
                    EditorGUILayout.BeginHorizontal();
                    GUI.color = AssetHunterHelper.AH_RED;

                    if (GUILayout.Button("Remove", GUILayout.Width(btnMinWidthSmall)))
                    {
                        settings.RemoveDirectoryAtIndex(i);
                        continue;
                    }
                    GUI.color = m_IntialGUIColor;
                    EditorGUILayout.ObjectField(settings.m_DirectoryExcludes[i], typeof(UnityEngine.Object), false);
                    EditorGUILayout.EndHorizontal();
                }
            }
            else
            {
                EditorGUILayout.LabelField("No folders are currently excluded");
            }

            EditorGUILayout.Separator();
            GUILayout.Label("---------------------------Excluded Types--------------------------------", EditorStyles.boldLabel);

            if (settings.m_AssetTypeExcludes.Count >= 1)
            {
                for (int i = settings.m_AssetTypeExcludes.Count - 1; i >= 0; i--)
                {
                    EditorGUILayout.BeginHorizontal();
                    GUI.color = AssetHunterHelper.AH_RED;
                    if (GUILayout.Button("Remove", GUILayout.Width(btnMinWidthSmall)))
                    {
                        settings.RemoveTypeAtIndex(i);
                        continue;
                    }
                    GUI.color = m_IntialGUIColor;
                    GUILayout.Label(settings.m_AssetTypeExcludes[i].Name);
                    EditorGUILayout.EndHorizontal();
                }
            }
            else
            {
                EditorGUILayout.LabelField("No types are currently excluded");
            }

            EditorGUILayout.Separator();

            GUILayout.Label("---------------------------Excluded Substrings---------------------------", EditorStyles.boldLabel);
            if (settings.m_AssetSubstringExcludes.Count >= 1)
            {
                for (int i = settings.m_AssetSubstringExcludes.Count - 1; i >= 0; i--)
                {
                    EditorGUILayout.BeginHorizontal();
                    GUI.color = AssetHunterHelper.AH_RED;
                    if (GUILayout.Button("Remove", GUILayout.Width(btnMinWidthSmall)))
                    {
                        settings.RemoveSubstringAtIndex(i);
                        continue;
                    }
                    GUI.color = m_IntialGUIColor;
                    GUILayout.Label(string.Format("\"{0}\"", settings.m_AssetSubstringExcludes[i]));
                    EditorGUILayout.EndHorizontal();
                }
            }
            else
            {
                EditorGUILayout.LabelField("No substrings are currently excluded");
            }


            GUILayout.Label("-----------------------Individually excluded assets----------------------", EditorStyles.boldLabel);
            if (settings.m_AssetGUIDExcludes.Count >= 1)
            {
                bool isDirty = false;
                for (int i = settings.m_AssetGUIDExcludes.Count - 1; i >= 0; i--)
                {
                    EditorGUILayout.BeginHorizontal();
                    GUI.color = AssetHunterHelper.AH_RED;
                    if (GUILayout.Button("Remove", GUILayout.Width(btnMinWidthSmall)))
                    {
                        settings.RemoveAssetIDAtIndex(i);
                        continue;
                    }
                    GUI.color = m_IntialGUIColor;

                    string path = AssetDatabase.GUIDToAssetPath(settings.m_AssetGUIDExcludes[i]);
                    Object obj  = AssetDatabase.LoadAssetAtPath(path, typeof(Object));

                    if (obj != null)
                    {
                        EditorGUILayout.ObjectField(obj, typeof(UnityEngine.Object), false);
                    }
                    else
                    {
                        isDirty = true;
                    }
                    //GUILayout.Label(string.Format("\"{0}\"", "Asset cannot be found"));

                    EditorGUILayout.EndHorizontal();
                }

                if (Event.current.type != EventType.Layout && isDirty)
                {
                    settings.CleanExcludedAssets();
                }
            }
            else
            {
                EditorGUILayout.LabelField("No project assets are currently excluded");
            }

            EditorGUILayout.EndVertical();
            EditorGUILayout.EndScrollView();
        }
        private static void traverseDirectory(int parentIndex, string path, List <BuildReportAsset> usedAssets, int heirarchyDepth, ref int directoriesTraversed, SortedDictionary <AssetHunterSerializableSystemType, bool> validTypeList)
        {
            directoriesTraversed++;

            EditorUtility.DisplayProgressBar(
                "Traversing Directories",
                "(" + directoriesTraversed + " of " + m_NumberOfDirectories + ") Analyzing " + path.Substring(path.IndexOf("/Assets") + 1),
                (float)directoriesTraversed / (float)m_NumberOfDirectories);

            //Get the settings to exclude certain folders or suffixes
            foreach (UnityEngine.Object dir in AssetHunterMainWindow.Instance.settings.m_DirectoryExcludes)
            {
                //TODO Can this be done more elegantly
                int                startingIndex = Application.dataPath.Length - 6;
                string             relativePath  = path.Substring(startingIndex, path.Length - startingIndex);
                UnityEngine.Object obj           = AssetDatabase.LoadAssetAtPath(relativePath, typeof(UnityEngine.Object));

                if (dir == obj)
                {
                    //This folder was exluded
                    return;
                }
            }

            //Exclude types and folders that should not be reviewed
            //TODO perhaps improve performance of this step (Also use String.Contains(excluder, StringComparison.OrdinalIgnoreCase)) might be better not to use LINQ
            string[] assetsInDirectory = Directory.GetFiles(path, "*.*", SearchOption.TopDirectoryOnly)
                                         .Where(name => !name.EndsWith(".meta", StringComparison.OrdinalIgnoreCase) &&
                                                (!name.EndsWith(".unity", StringComparison.OrdinalIgnoreCase)) &&
                                                (!name.EndsWith("thumbs.db", StringComparison.OrdinalIgnoreCase)) &&
                                                (!name.EndsWith(".orig", StringComparison.OrdinalIgnoreCase)) &&
                                                (!name.EndsWith(".dll", StringComparison.OrdinalIgnoreCase)) &&
                                                (!name.EndsWith(".mdb", StringComparison.OrdinalIgnoreCase)) &&
                                                (!name.Contains(Path.DirectorySeparatorChar + "heureka" + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase)) &&
                                                (!name.Contains(Path.DirectorySeparatorChar + "plugins" + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase)) &&
                                                (!name.Contains(Path.DirectorySeparatorChar + "streamingassets" + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase)) &&
                                                (!name.Contains(Path.DirectorySeparatorChar + "resources" + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase)) &&
                                                (!name.Contains(Path.DirectorySeparatorChar + "editor default resources" + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase)) &&
                                                (!name.Contains(Path.DirectorySeparatorChar + "editor" + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase)) &&
                                                (!name.EndsWith(@".ds_store", StringComparison.OrdinalIgnoreCase)) &&
                                                (!name.EndsWith(@".workspace.mel", StringComparison.OrdinalIgnoreCase)) &&
                                                (!name.EndsWith(@".mayaswatches", StringComparison.OrdinalIgnoreCase)))
                                         .ToArray();

            //TODO this could also be improved for performance
            for (int i = 0; i < assetsInDirectory.Length; i++)
            {
                assetsInDirectory[i] = assetsInDirectory[i].Substring(assetsInDirectory[i].IndexOf("/Assets") + 1);
                assetsInDirectory[i] = assetsInDirectory[i].Replace(@"\", "/");
            }

            //Remove the assets we ignore through settings && Find any assets that does not live in UsedAssets List
            //TODO for performance reasons, perhaps dont to this for each folder, but just once, when finished?
            //That would mean to do folder creation and populating unused assets lists after all folders are traversed
            var result = assetsInDirectory.Where(p => (!AssetHunterMainWindow.Instance.settings.m_AssetGUIDExcludes.Any(p2 => UnityEditor.AssetDatabase.GUIDToAssetPath(p2) == p)) &&
                                                 !usedAssets.Any(p2 => UnityEditor.AssetDatabase.GUIDToAssetPath(p2.GUID) == p));

            //Create new folder object
            AssetHunterProjectFolderInfo afInfo = new AssetHunterProjectFolderInfo();

            //TODO this could also be improved for performance
            afInfo.DirectoryName = path.Substring(path.IndexOf("/Assets") + 1).Replace(@"\", "/");
            afInfo.ParentIndex   = parentIndex;

            if (heirarchyDepth == 0)
            {
                afInfo.FoldOut = true;
            }

            //Add to static list
            AssetHunterMainWindow.Instance.AddProjectFolderInfo(afInfo);

            if (parentIndex != -1)
            {
                AssetHunterMainWindow.Instance.GetFolderList()[parentIndex].AddChildFolder(afInfo);
            }

            UnityEngine.Object objToFind;
            foreach (string assetName in result)
            {
                bool bExclude = false;

                foreach (string excluder in AssetHunterMainWindow.Instance.settings.m_AssetSubstringExcludes)
                {
                    //Exlude Asset Exclude substrings from settings
                    //If we find an excluded asset just continue to next iteration in loop
                    if (assetName.Contains(excluder, StringComparison.OrdinalIgnoreCase))
                    {
                        bExclude = true;
                    }
                }
                if (bExclude)
                {
                    continue;
                }

                objToFind = AssetDatabase.LoadAssetAtPath(assetName, typeof(UnityEngine.Object));

                if (objToFind == null)
                {
                    Debug.LogWarning("Couldnt find " + assetName);
                    continue;
                }

                AssetHunterSerializableSystemType assetType = new AssetHunterSerializableSystemType(objToFind.GetType());

                if (assetType.SystemType != typeof(MonoScript) && (!AssetHunterMainWindow.Instance.settings.m_AssetTypeExcludes.Contains(assetType)))
                {
                    AssetObjectInfo newAssetInfo = new AssetObjectInfo(assetName, assetType);
                    afInfo.AddAsset(newAssetInfo);
                }

                objToFind = null;

                //Memory leak safeguard
                //This have heavy performance implications
                if (AssetHunterMainWindow.Instance.settings.m_MemoryCleanupActive)
                {
                    UnloadUnused();
                }
            }

            string[] nextLevelDirectories = System.IO.Directory.GetDirectories(path, "*.*", System.IO.SearchOption.TopDirectoryOnly);

            //Memory leak safeguard per folder
            if (!AssetHunterMainWindow.Instance.settings.m_MemoryCleanupActive)
            {
                UnloadUnused();
            }

            foreach (string nld in nextLevelDirectories)
            {
                traverseDirectory(AssetHunterMainWindow.Instance.GetFolderList().IndexOf(afInfo), nld, usedAssets, (heirarchyDepth + 1), ref directoriesTraversed, validTypeList);
            }
        }
 internal void ExcludeType(AssetHunterSerializableSystemType newtype)
 {
     m_AssetTypeExcludes.Add(newtype);
 }
 internal bool ValidateType(AssetHunterSerializableSystemType newtype)
 {
     return(!m_AssetTypeExcludes.Contains(newtype));
 }