Browse Source

Added scende dependecy logic

Syed zainul abedin 1 week ago
parent
commit
4e18481b1e

+ 54 - 3
Assets/IntelligentProjectAnalyzer/Editor/AssetDataFetcher.cs

@@ -5,6 +5,7 @@ using UnityEngine;
 using UnityEditor.Compilation;
 using UnityEditor.Compilation;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using IntelligentProjectAnalyzer.Analyzer;
 using IntelligentProjectAnalyzer.Analyzer;
+using UnityEditor.SceneManagement;
 
 
 // All using statements now refer to the new data-only class
 // All using statements now refer to the new data-only class
 using static IntelligentProjectAnalyzer.Editor.DependencyBuilderData;
 using static IntelligentProjectAnalyzer.Editor.DependencyBuilderData;
@@ -21,12 +22,13 @@ namespace IntelligentProjectAnalyzer.Editor
         /// Finds all relevant asset GUIDs for scripts, prefabs, and ScriptableObjects.
         /// Finds all relevant asset GUIDs for scripts, prefabs, and ScriptableObjects.
         /// </summary>
         /// </summary>
         /// <returns>A tuple containing queues of GUIDs for each asset type.</returns>
         /// <returns>A tuple containing queues of GUIDs for each asset type.</returns>
-        public static (Queue<string> scriptGuids, Queue<string> prefabGuids, Queue<string> soGuids) FindAssetGuids()
+        public static (Queue<string> scriptGuids, Queue<string> prefabGuids, Queue<string> soGuids, Queue<string> sceneGuids) FindAssetGuids()
         {
         {
             var scripts = new Queue<string>(AssetDatabase.FindAssets("t:MonoScript"));
             var scripts = new Queue<string>(AssetDatabase.FindAssets("t:MonoScript"));
             var prefabs = new Queue<string>(AssetDatabase.FindAssets("t:Prefab"));
             var prefabs = new Queue<string>(AssetDatabase.FindAssets("t:Prefab"));
             var scriptableObjects = new Queue<string>(AssetDatabase.FindAssets("t:ScriptableObject"));
             var scriptableObjects = new Queue<string>(AssetDatabase.FindAssets("t:ScriptableObject"));
-            return (scripts, prefabs, scriptableObjects);
+            var scenes = new Queue<string>(AssetDatabase.FindAssets("t:Scene"));
+            return (scripts, prefabs, scriptableObjects, scenes);
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -46,7 +48,7 @@ namespace IntelligentProjectAnalyzer.Editor
         {
         {
             var assetPath = AssetDatabase.GUIDToAssetPath(guid);
             var assetPath = AssetDatabase.GUIDToAssetPath(guid);
             if (string.IsNullOrEmpty(assetPath) || !assetPath.StartsWith("Assets/")) return null;
             if (string.IsNullOrEmpty(assetPath) || !assetPath.StartsWith("Assets/")) return null;
-
+    
             var go = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
             var go = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
             if (go == null) return null;
             if (go == null) return null;
 
 
@@ -173,5 +175,54 @@ namespace IntelligentProjectAnalyzer.Editor
             }
             }
             return setupData;
             return setupData;
         }
         }
+
+        public static SceneMetadata PreFetchSceneMetadata(string guid)
+        {
+            var assetPath = AssetDatabase.GUIDToAssetPath(guid);
+            if (string.IsNullOrEmpty(assetPath) || !assetPath.StartsWith("Assets/")) return null;
+            
+            var scene = EditorSceneManager.OpenScene(assetPath, OpenSceneMode.Single);
+
+            var sceneMetaData = new SceneMetadata() { Guid = guid };
+            var hash = new HashSet<string>();
+
+            // Find all components on the prefab and its children to get their script GUIDs.
+            foreach (var root in scene.GetRootGameObjects())
+            {
+                var components = root.GetComponentsInChildren<Component>(true);
+                foreach (var component in components)
+                {
+                    if (component == null) continue;
+                
+                    var script = MonoScript.FromMonoBehaviour(component as MonoBehaviour);
+                    if (script == null) continue;
+
+                    var scriptAssetPath = AssetDatabase.GetAssetPath(script);
+                    if (string.IsNullOrEmpty(scriptAssetPath)) continue;
+
+                    var scriptGuid = AssetDatabase.AssetPathToGUID(scriptAssetPath);
+                    if (!string.IsNullOrEmpty(scriptGuid) && scriptAssetPath.StartsWith("Assets/"))
+                    {
+                        hash.Add(scriptGuid);
+                    }
+                }
+            }
+            
+            var assetDependencies = AssetDatabase.GetDependencies(assetPath);
+            foreach (var dependency in assetDependencies)
+            {
+                if (string.IsNullOrEmpty(dependency) || !dependency.StartsWith("Assets/") || dependency == assetPath)
+                {
+                    continue;
+                }
+                var dependencyGuid = AssetDatabase.AssetPathToGUID(dependency);
+                if (!string.IsNullOrEmpty(dependencyGuid))
+                {
+                    hash.Add(dependencyGuid);
+                }
+            }
+            sceneMetaData.DependencyGuids = hash.ToList();
+            return sceneMetaData;
+        }
     }
     }
 }
 }

+ 8 - 1
Assets/IntelligentProjectAnalyzer/Editor/DependencyBuilderData.cs

@@ -19,7 +19,8 @@ namespace IntelligentProjectAnalyzer.Editor
             {
             {
                 typeof(ScriptMetadata),
                 typeof(ScriptMetadata),
                 typeof(PrefabMetadata),
                 typeof(PrefabMetadata),
-                typeof(ScriptableObjectMetadata)
+                typeof(SceneMetadata),
+                typeof(ScriptableObjectMetadata),
             };
             };
 
 
             public static int GetIndexFromType(Type type) => Types.IndexOf(type);
             public static int GetIndexFromType(Type type) => Types.IndexOf(type);
@@ -79,5 +80,11 @@ namespace IntelligentProjectAnalyzer.Editor
         /// </summary>
         /// </summary>
         [Serializable]
         [Serializable]
         public class ScriptableObjectMetadata : AssetMetadata { }
         public class ScriptableObjectMetadata : AssetMetadata { }
+        
+        /// <summary>
+        /// Metadata for scene.Its DependencyGuids will contain the GUIDs of all scripts attached as components.
+        /// </summary>
+        [Serializable]
+        public class SceneMetadata : AssetMetadata { }
     }
     }
 }
 }

+ 15 - 3
Assets/IntelligentProjectAnalyzer/Editor/DependencyStoreBuilder.cs

@@ -25,10 +25,12 @@ namespace IntelligentProjectAnalyzer.Editor
         private static Queue<string> _scriptGuidsToProcess;
         private static Queue<string> _scriptGuidsToProcess;
         private static Queue<string> _prefabGuidsToProcess;
         private static Queue<string> _prefabGuidsToProcess;
         private static Queue<string> _soGuidsToProcess;
         private static Queue<string> _soGuidsToProcess;
+        private static Queue<string> _sceneGuidsToProcess;
 
 
         // Lists to store the pre-fetched metadata
         // Lists to store the pre-fetched metadata
         private static List<AssetMetadata> _allMetadata;
         private static List<AssetMetadata> _allMetadata;
         private static List<ScriptMetadata> _scriptMetadata;
         private static List<ScriptMetadata> _scriptMetadata;
+        private static List<SceneMetadata> _sceneMetadata;
 
 
         // Task for background analysis
         // Task for background analysis
         private static Task<List<AssetMetadata>> _analysisTask;
         private static Task<List<AssetMetadata>> _analysisTask;
@@ -55,10 +57,11 @@ namespace IntelligentProjectAnalyzer.Editor
             Debug.Log($"--- Starting Async Dependency Store Build ---");
             Debug.Log($"--- Starting Async Dependency Store Build ---");
             DependencyCacheManager.CleanAndPrepareCache();
             DependencyCacheManager.CleanAndPrepareCache();
 
 
-            (_scriptGuidsToProcess, _prefabGuidsToProcess, _soGuidsToProcess) = AssetDataFetcher.FindAssetGuids();
+            (_scriptGuidsToProcess, _prefabGuidsToProcess, _soGuidsToProcess, _sceneGuidsToProcess) = AssetDataFetcher.FindAssetGuids();
 
 
             _allMetadata = new List<AssetMetadata>();
             _allMetadata = new List<AssetMetadata>();
             _scriptMetadata = new List<ScriptMetadata>();
             _scriptMetadata = new List<ScriptMetadata>();
+            _sceneMetadata = new List<SceneMetadata>();
 
 
             _currentState = BuilderState.PreFetching;
             _currentState = BuilderState.PreFetching;
         }
         }
@@ -92,12 +95,21 @@ namespace IntelligentProjectAnalyzer.Editor
 
 
         private static void ProcessPreFetchingBatch()
         private static void ProcessPreFetchingBatch()
         {
         {
-            var totalAssets = _scriptGuidsToProcess.Count + _prefabGuidsToProcess.Count + _soGuidsToProcess.Count;
+            var totalAssets = _scriptGuidsToProcess.Count + _prefabGuidsToProcess.Count + _soGuidsToProcess.Count + _sceneGuidsToProcess.Count;
             var initialTotal = _allMetadata.Count + totalAssets;
             var initialTotal = _allMetadata.Count + totalAssets;
 
 
             for (var i = 0; i < BatchSize; i++)
             for (var i = 0; i < BatchSize; i++)
             {
             {
-                if (_scriptGuidsToProcess.Count > 0)
+                if (_sceneGuidsToProcess.Count > 0)
+                {
+                    var meta = AssetDataFetcher.PreFetchSceneMetadata(_sceneGuidsToProcess.Dequeue());
+                    if (meta != null)
+                    {
+                        _allMetadata.Add(meta);
+                        _sceneMetadata.Add(meta);
+                    }
+                }
+                else if (_scriptGuidsToProcess.Count > 0)
                 {
                 {
                     var meta = AssetDataFetcher.PreFetchScriptMetadata(_scriptGuidsToProcess.Dequeue());
                     var meta = AssetDataFetcher.PreFetchScriptMetadata(_scriptGuidsToProcess.Dequeue());
                     if (meta != null)
                     if (meta != null)

+ 1 - 1
Assets/IntelligentProjectAnalyzer/Editor/DependencyViewer/DependencyViewerWindow.cs

@@ -112,7 +112,7 @@ namespace IntelligentProjectAnalyzer.Editor.DependencyViewer
             }
             }
 
 
             // --- Validation Step 1: Check Asset Type ---
             // --- Validation Step 1: Check Asset Type ---
-            var isValidType = newAsset is MonoScript or GameObject or ScriptableObject;
+            var isValidType = newAsset is MonoScript or GameObject or ScriptableObject or SceneAsset;
             if (!isValidType)
             if (!isValidType)
             {
             {
                 EditorUtility.DisplayDialog("Invalid Asset Type", "Please select a C# Script, Prefab, or ScriptableObject.", "OK");
                 EditorUtility.DisplayDialog("Invalid Asset Type", "Please select a C# Script, Prefab, or ScriptableObject.", "OK");