Advertisement
Tooster

Untitled

Sep 20th, 2019
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. public class FindMissingScriptsRecursively : EditorWindow
  2. {
  3. private Object target;
  4. private bool recursive = true, compact = true;
  5.  
  6. // [SerializeField] private TreeViewState treeState;
  7. // private T3TreeView treeView;
  8.  
  9. [MenuItem("T3/missfinder")]
  10. public static void ShowWindow() { GetWindow(typeof(FindMissingScriptsRecursively)); }
  11.  
  12. // private void OnEnable()
  13. // {
  14. // if(treeState == null)
  15. // treeState = new TreeViewState();
  16. // treeView = new T3TreeView(treeState);
  17. // }
  18. Vector2 scroll = new Vector2();
  19. void OnGUI()
  20. {
  21. GUI.skin.label.wordWrap = true;
  22. titleContent.text = "T3 missfinder";
  23.  
  24. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  25.  
  26. target = EditorGUILayout.ObjectField(target, typeof(Object), true, GUILayout.Height(20));
  27. EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
  28. recursive = EditorGUILayout.ToggleLeft("recursive", recursive, GUILayout.Height(20));
  29. compact = EditorGUILayout.ToggleLeft("compact", compact, GUILayout.Height(20));
  30. EditorGUILayout.EndHorizontal();
  31.  
  32. if(GUILayout.Button("analyze object", GUILayout.Height(30)))
  33. {
  34. Result = "";
  35. if(target is GameObject) FindForGameObject(target as GameObject);
  36. else FindForFolder();
  37. }
  38.  
  39. GUILayout.Label("Missing reference finder by T3s. Choose a prefab/folder and run `analyze` to get missing references");
  40. EditorGUILayout.EndVertical();
  41.  
  42. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  43.  
  44. scroll = EditorGUILayout.BeginScrollView(scroll);
  45. EditorGUILayout.TextArea(Result);
  46. EditorGUILayout.EndScrollView();
  47. // treeView.OnGUI(new Rect(0, 0, position.width, position.height));
  48. EditorGUILayout.EndVertical();
  49. }
  50.  
  51. private string Result { get; set; }
  52.  
  53. private void FindForGameObject(GameObject go, string path = "")
  54. {
  55. FindForSingleGameObject(go, path + go.name + "/");
  56. if(recursive)
  57. foreach(Transform child in go.transform)
  58. FindForGameObject(child.gameObject, go.name + "/");
  59. }
  60.  
  61. private void FindForSingleGameObject(GameObject go, string path = "")
  62. {
  63. var components = go.GetComponents<Component>();
  64. foreach(Component c in components)
  65. {
  66. SerializedObject so = new SerializedObject(c);
  67. var sp = so.GetIterator();
  68. while(sp.NextVisible(true))
  69. if(sp.propertyType == SerializedPropertyType.ObjectReference
  70. && sp.objectReferenceValue == null)
  71. Result += "\n" + path + " ["+ c.GetType() + "]-> " + sp.displayName;
  72. }
  73. }
  74.  
  75. private void FindForFolder() { Debug.LogError("Analyze for folder not implemented... YET..."); }
  76.  
  77. // [Serializable]
  78. // internal class T3TreeView : TreeView
  79. // {
  80. // public T3TreeView(TreeViewState state) : base(state,) { }
  81. //
  82. // protected override TreeViewItem BuildRoot() { }
  83. //
  84. // protected override IList<TreeViewItem> BuildRows(TreeViewItem root) { return base.BuildRows(root); }
  85. // }
  86. //
  87. // internal class T3TreeNode
  88. // {
  89. // public string path;
  90. // public Component script;
  91. // public Object missingField;
  92. //
  93. // public T3TreeNode(string path, Component script, Object missingField)
  94. // {
  95. // this.path = path;
  96. // this.script = script;
  97. // this.missingField = missingField;
  98. // }
  99. // }
  100. //
  101. // internal class T3TreeModel : List<T3TreeNode>
  102. // {
  103. // }
  104.  
  105.  
  106. // public static void FindMissingScripts()
  107. // {
  108. // StringBuilder resultBuilder = new StringBuilder();
  109. // string fileName = "missing.txt";
  110. //
  111. //// var components = Selection.activeGameObject.GetComponents<Component>();
  112. //// foreach(var c in components)
  113. //// {
  114. //// var sp = new SerializedObject(c).GetIterator();
  115. //// while(sp.NextVisible(true))
  116. //// if(sp.propertyType == SerializedPropertyType.ObjectReference && sp.objectReferenceValue == null)
  117. //// }
  118. //
  119. // foreach(GameObject selectedGameObject in Selection.gameObjects)
  120. // {
  121. // string assetPath = AssetDatabase.GetAssetPath(selectedGameObject);
  122. // AssetDatabase
  123. // .LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(selectedGameObject))
  124. // .OfType<GameObject>()
  125. // .ForEach(gameObject => FindMissingScriptsInGameObject(assetPath, gameObject, resultBuilder));
  126. // }
  127. //
  128. // SaveResultToFile(resultBuilder, fileName);
  129. // Debug.LogError("Done. Results are in file " + fileName);
  130. // }
  131. //
  132. // private static void FindMissingScriptsInGameObject(string assetPath, GameObject gameObject, StringBuilder resultBuilder)
  133. // {
  134. // gameObject.GetComponents<Component>().Where(c => c == null)
  135. // .ForEach(c => LogMissingScript(assetPath, gameObject, resultBuilder));
  136. // }
  137. //
  138. // private static void LogMissingScript(string assetPath, GameObject gameObject, StringBuilder resultBuilder)
  139. // {
  140. // resultBuilder.AppendLine(string.Format("{0:50} {1:50}", assetPath, Chui.Util.GetFullPath(gameObject)));
  141. // }
  142. //
  143. // private static void SaveResultToFile(StringBuilder resultBuilder, string fileName)
  144. // {
  145. // File.WriteAllText(fileName, resultBuilder.ToString());
  146. // }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement