public override void FindReferences(string fileContents, string guid, ILookup <string, YamlSearch.LocalFileReferenceResult> localFileIds) { var scene = SceneManager.GetSceneByPath(assetPath); if (scene.IsValid()) { var gameObjects = scene.GetRootGameObjects(); foreach (var gameObject in gameObjects) { var components = gameObject.GetComponentsInChildren <Component>(true); references.AddRange(components.Where(c => c != null && (localFileIds.Contains(c.GetLocalIdentifier().ToString()) || assetObject.GetType().IsAssignableFrom(c.GetType())))); } } else { foreach (var kp in localFileIds) { var doc = YamlSearch.ExtractInstanceDetails(fileContents, kp.Key); if (doc.TryGetValue("m_Script", out var scriptRaw)) { var match = YamlSearch.GUIDRegex.Match(scriptRaw); if (match.Success) { var objAssetPath = AssetDatabase.GUIDToAssetPath(match.Value); var obj = AssetDatabase.LoadAssetAtPath(objAssetPath, typeof(Object)); references.Add(obj); } } } } }
/// <summary> /// Counts occurances of every asset /// </summary> /// <param name="filter">Search Filter</param> /// <param name="incremental">Should it yield between each subfile it searched</param> /// <param name="results">Map between GUID and their counts</param> /// <returns></returns> public static IEnumerator CountAssetsEnumerator(SearchType filter, bool incremental, Dictionary <string, CountResult> results) { //Find the assets var assets = FindAssets(filter); foreach (var asset in assets) { //If wea re not on the list yet, then add ourselves if (!results.ContainsKey(asset.guid)) { results.Add(asset.guid, new CountResult(asset.guid)); } //Skip asset types that are large and cannot have references. if (asset.assetType == typeof(LightingDataAsset)) { continue; } if (asset.assetType == typeof(AudioClip)) { continue; } if (asset.assetType == typeof(VideoClip)) { continue; } if (asset.assetType == typeof(Texture2D)) { continue; } //Set the search asset and yield so the GUI has time to update the dispaly LastSearchedAsset = asset.assetPath; yield return(null); //Count all the seperate GUID in this file var enumerator = YamlSearch.CountReferences(asset.assetPath, results); int iteration = 0; while (enumerator.MoveNext()) { if (incremental || ++iteration > MaxSearchIteration) { iteration = 0; yield return(null); } } } }
/// <summary> /// Reads the contents of the assets and attempts to find references to the searching object. For efficicency, it will return null for invalid results. /// </summary> /// <param name="searchingObject"></param> /// <param name="types"></param> public static IEnumerator FindAssetsEnumerator(Object searchingObject, SearchType types, List <AssetResult> results, long fileSizeLimit = 10000000, bool withProgressBar = false) { if (searchingObject == null) { yield break; } //Get the GUID and search type string guid; long instanceId; System.Type searchType = searchingObject is MonoScript monoScript?monoScript.GetClass() : AssetDatabase.GetMainAssetTypeAtPath(AssetDatabase.GetAssetPath(searchingObject)); bool canSearchComponents = typeof(Component).IsAssignableFrom(searchType); if (!AssetDatabase.TryGetGUIDAndLocalFileIdentifier(searchingObject, out guid, out instanceId)) { Debug.LogError("Failed to get GUID"); yield break; } var unloadedResults = FindAssets(types); var totalProgress = unloadedResults.Count(); List <AssetResult> loadedResults = new List <AssetResult>(totalProgress); var progress = 0; string fileContents; List <Component> loadedComponents = new List <Component>(10); List <GameObject> rootGameObjects = new List <GameObject>(10); var orderedUnloadedResults = unloadedResults.OrderBy(r => new FileInfo(r.assetPath).Length).Where(r => new FileInfo(r.assetPath).Length < fileSizeLimit); foreach (var foundResult in orderedUnloadedResults) { //Update the progress bar if (withProgressBar) { EditorUtility.DisplayProgressBar("Searching File", foundResult.assetPath, progress++ / (float)totalProgress); } //We dont care about these if (foundResult.assetType == typeof(LightingDataAsset)) { continue; } if (foundResult.assetType == typeof(AudioClip)) { continue; } if (foundResult.assetType == typeof(VideoClip)) { continue; } if (foundResult.assetType == typeof(Texture2D)) { continue; } //Read the file contents LastSearchedAsset = foundResult.assetPath; fileContents = File.ReadAllText(foundResult.assetPath); if (!fileContents.Contains(guid)) { //Skip if the total read bytes is a great amount. yield return(null); } else { //Dupe the result var result = foundResult; //Load the asset and find all sub components result.assetObject = AssetDatabase.LoadAssetAtPath(foundResult.assetPath, foundResult.assetType); if (result is ComponentResult componentResult) { var localFileReferences = YamlSearch.FindLocalFileReferences(fileContents, guid).ToLookup(k => k.fileId); componentResult.FindReferences(fileContents, guid, localFileReferences); } //Add an item results.Add(result); yield return(null); } } //Clear the progress bar if (withProgressBar) { EditorUtility.ClearProgressBar(); } }