Guest User

Untitled

a guest
Feb 25th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. /*
  2.  
  3.  
  4. This tool iterates through all the files in your project and checks for scripts.
  5. It then goes through all the GameObjects in all the scenes in the project, and
  6. checks for scripts that are not present on any GameObjects.
  7.  
  8. Note that this does not mean that the script is not used, just that it is possible that it isn't.
  9. The script could still be used in many other ways. This tool is just to narrow the search for
  10. unused code.
  11.  
  12. The script is tested on MacOS, but should work just as fine on Windows.
  13.  
  14.  
  15. ////// DISCLAIMER /////
  16. Permission is hereby granted, free of charge, to any person obtaining a copy
  17. of this software and associated documentation files (the "Software"), to deal
  18. in the Software without restriction, including without limitation the rights
  19. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  20. of the Software, and to permit persons to whom the Software is furnished to do so,
  21. subject to the following conditions: The above copyright notice and this permission
  22. notice shall be included in all copies or substantial portions of the Software.
  23. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  24. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  25. PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  26. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  27. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  28. OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. (MIT License)
  30.  
  31.  
  32.  
  33. */
  34.  
  35.  
  36. #if UNITY_EDITOR
  37. using UnityEditor;
  38. using UnityEngine;
  39. using System;
  40. using System.IO;
  41. using System.Collections;
  42. using System.Collections.Generic;
  43. using UnityEngine.SceneManagement;
  44. using UnityEditor.SceneManagement;
  45. using System.Linq;
  46.  
  47. public class InactiveCodeDetector : EditorWindow
  48. {
  49. private bool isDone = false;
  50. private List<string> paths = new List<string>();
  51. private List<Scene> allScenes = new List<Scene>();
  52.  
  53. private Scene currentlyOpenScene;
  54.  
  55. Vector2 scrollPosition;
  56.  
  57.  
  58. private List<string> allUsedComponents = new List<string>();
  59.  
  60. private List<String> unusedAssets = new List<string>();
  61.  
  62. [MenuItem("Tools/Clean up scripts")]
  63. public static void ShowWindow(){
  64. EditorWindow.GetWindow<InactiveCodeDetector>("Clean up scripts");
  65. }
  66.  
  67. private void OnGUI()
  68. {
  69. GUILayout.Label("Find possibly unused scripts.");
  70. if(GUILayout.Button("Search")){
  71. isDone = false;
  72. unusedAssets.Clear();
  73. currentlyOpenScene = EditorSceneManager.GetActiveScene();
  74. FindAll();
  75. CheckAllScenes();
  76. Compare();
  77. CloseScenes();
  78. CleanUp();
  79. isDone = true;
  80. }
  81.  
  82. if(isDone){
  83. PromtUser();
  84. }
  85.  
  86. }
  87.  
  88. private void PromtUser(){
  89.  
  90. string content = "The following scripts are not found on any GameObject in this project.";
  91. string warning = "NOTE - This does NOT mean it is safe to delete. The script could still be accessed via code or on a prefab";
  92.  
  93. scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
  94. GUILayout.Label(content);
  95. GUILayout.Label(warning, EditorStyles.boldLabel);
  96. foreach(string s in unusedAssets){
  97. GUILayout.Label(s);
  98. if(GUILayout.Button("Show in finder/explorer")){
  99. EditorUtility.RevealInFinder(s);
  100. }
  101. GUILayout.Space(10);
  102. }
  103. EditorGUILayout.EndScrollView();
  104. }
  105.  
  106. private void CleanUp(){
  107. paths.Clear();
  108. allScenes.Clear();
  109. allUsedComponents.Clear();
  110.  
  111. }
  112.  
  113. private void Compare(){
  114. foreach(string filename in paths){
  115. string formated = filename.Split('/').Last();
  116. if (formated != "InactiveCodeDetector.cs")
  117. {
  118. string isUsed = (from string s in allUsedComponents where s.ToLower() == formated.ToLower() select s).FirstOrDefault();
  119.  
  120. if (isUsed == null)
  121. {
  122. unusedAssets.Add(filename);
  123. //Debug.Log("Script " + filename + " is not attached to any game object in this project");
  124. }
  125. }
  126. }
  127. }
  128.  
  129. private void CheckAllScenes(){
  130. Scene[] scenes = new Scene[EditorSceneManager.sceneCount];
  131. for (int i = 0; i < scenes.Length; i++)
  132. {
  133. scenes[i] = EditorSceneManager.GetSceneAt(i);
  134.  
  135.  
  136. GameObject[] rootObjectsInScene = scenes[i].GetRootGameObjects();
  137. foreach (GameObject g in rootObjectsInScene)
  138. {
  139. foreach(Component c in g.GetComponents<Component>()){
  140. string name = ObjectNames.GetInspectorTitle(c);
  141. if (name.EndsWith("(Script)"))
  142. {
  143. string formated = name.Replace("(Script)", String.Empty).Replace(" ", String.Empty) + ".cs";
  144. allUsedComponents.Add(formated.ToLower());
  145. }
  146.  
  147. }
  148. }
  149. }
  150. }
  151.  
  152. private void CloseScenes(){
  153.  
  154. foreach(Scene s in allScenes){
  155. if(s != currentlyOpenScene){
  156. EditorSceneManager.CloseScene(s, true);
  157. }
  158. }
  159.  
  160. }
  161.  
  162. private void FindAll(){
  163. string p = Application.dataPath;
  164. ProcessDirectory(p);
  165. }
  166.  
  167. private void ProcessDirectory(string path){
  168. string[] fileEntries = Directory.GetFiles(path);
  169. foreach(string filename in fileEntries){
  170. if (filename.EndsWith(".cs"))
  171. {
  172. paths.Add(filename);
  173. }
  174. if(filename.EndsWith(".unity")){
  175.  
  176. Scene scene = EditorSceneManager.OpenScene(filename, OpenSceneMode.Additive);
  177. allScenes.Add(scene);
  178. }
  179. }
  180. string[] subdirectories = Directory.GetDirectories(path);
  181. foreach(string sub in subdirectories){
  182. ProcessDirectory(sub);
  183. }
  184. }
  185.  
  186.  
  187. }
  188.  
  189.  
  190. #endif
Add Comment
Please, Sign In to add comment