Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEditor;
  6. using Object = UnityEngine.Object;
  7.  
  8. public static class DependencyTracker
  9. {
  10. private static Dictionary<string, List<string>> dependantCache;
  11.  
  12. static DependencyTracker()
  13. {
  14. EditorApplication.projectChanged += () => dependantCache = null;
  15. }
  16.  
  17. [MenuItem("Assets/Select Dependants", priority = 31)]
  18. private static void SelectDependants()
  19. {
  20. Object[] selection = Selection.objects;
  21. if (selection.Length == 0)
  22. return;
  23.  
  24. if (dependantCache != null)
  25. SelectDependants(selection);
  26. else
  27. StartCoroutine(GatherDependencies(), () => SelectDependants(selection));
  28. }
  29.  
  30. private static void SelectDependants(Object[] selection)
  31. {
  32. var assetPaths = selection.Select(i => AssetDatabase.GetAssetPath(i));
  33.  
  34. var dependantPaths = selection.Length == 1 && AssetDatabase.IsValidFolder(assetPaths.First())
  35. ? SelectFolderDependants(assetPaths.First())
  36. : EnumerateFileDependants(assetPaths);
  37.  
  38. Selection.objects = dependantPaths
  39. .Select(i => AssetDatabase.LoadMainAssetAtPath(i))
  40. .Concat(selection)
  41. .ToArray();
  42. }
  43.  
  44. private static IEnumerable<string> EnumerateFileDependants(IEnumerable<string> assetPaths)
  45. {
  46. foreach (var assetPath in assetPaths)
  47. if (dependantCache.TryGetValue(assetPath, out var dependantPaths))
  48. foreach (string dependantPath in dependantPaths)
  49. yield return dependantPath;
  50. }
  51.  
  52. // Find assets inside a folder that are referenced by assets outside it.
  53. private static IEnumerable<string> SelectFolderDependants(string folderPath)
  54. {
  55. foreach (var assetPath in dependantCache.Keys)
  56. if (assetPath.StartsWith(folderPath))
  57. foreach (var dependantPath in dependantCache[assetPath])
  58. if (!dependantPath.StartsWith(folderPath))
  59. yield return dependantPath;
  60. }
  61.  
  62. private static IEnumerator GatherDependencies()
  63. {
  64. EditorUtility.DisplayProgressBar("Gathering Dependencies", "", 0);
  65. yield return null;
  66.  
  67. var assets = AssetDatabase.FindAssets(null, new[] { "Assets" })
  68. .Distinct()
  69. .ToList();
  70.  
  71. var lastYieldTime = DateTime.Now;
  72.  
  73. var dependencies = new List<KeyValuePair<string, string>>();
  74. for (int i = 0; i < assets.Count; i++)
  75. {
  76. string assetPath = AssetDatabase.GUIDToAssetPath(assets[i]);
  77.  
  78. var now = DateTime.Now;
  79. if (now - lastYieldTime > TimeSpan.FromMilliseconds(100))
  80. {
  81. lastYieldTime = now;
  82. EditorUtility.DisplayProgressBar("Gathering Dependencies", assetPath,
  83. (float)i / assets.Count);
  84. yield return null;
  85. }
  86.  
  87. foreach (var dependencyPath in AssetDatabase.GetDependencies(assetPath))
  88. if (dependencyPath != assetPath)
  89. dependencies.Add(new KeyValuePair<string, string>(dependencyPath, assetPath));
  90. }
  91.  
  92. EditorUtility.DisplayProgressBar("Gathering Dependencies", "Building dictionary", 1);
  93. yield return null;
  94.  
  95. dependantCache = dependencies
  96. .GroupBy(i => i.Key)
  97. .ToDictionary(i => i.Key, i => i
  98. .Select(j => j.Value)
  99. .ToList());
  100.  
  101. EditorUtility.ClearProgressBar();
  102. }
  103.  
  104. private static void StartCoroutine(IEnumerator routine, Action onCompleted = null)
  105. {
  106. EditorApplication.CallbackFunction routineFunc = null;
  107. routineFunc = () =>
  108. {
  109. if (!routine.MoveNext())
  110. {
  111. EditorApplication.update -= routineFunc;
  112. if (onCompleted != null)
  113. onCompleted();
  114. }
  115. };
  116.  
  117. EditorApplication.update += routineFunc;
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement