Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.08 KB | None | 0 0
  1. #if UNITY_EDITOR
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEditor;
  6. using UnityEngine;
  7.  
  8. public class FindTextInScriptsWindow : EditorWindow
  9. {
  10.     static string pathKey = "efpafvn98qnvq23ru9fse"; // How to make keys not crash with other keys ^:D
  11.     static string searchKey = "rvi23uq4vekpfeaofi293gsp4";
  12.  
  13.     // Add menu named "My Window" to the Window menu
  14.     [MenuItem("Window/FindTextInScripts Window")]
  15.     static void Init()
  16.     {
  17.         // Get existing open window or if none, make a new one:
  18.         FindTextInScriptsWindow window = (FindTextInScriptsWindow)EditorWindow.GetWindow(typeof(FindTextInScriptsWindow));
  19.         window.Show();
  20.  
  21.         scriptPath = PlayerPrefs.GetString(pathKey, "");
  22.         searchText = PlayerPrefs.GetString(searchKey, "");
  23.     }
  24.  
  25.     struct FindTextInScriptsLine
  26.     {
  27.         public string text;
  28.         public string filename;
  29.         public int line;
  30.         public Object obj;
  31.     }
  32.  
  33.     List<FindTextInScriptsLine> todos = new List<FindTextInScriptsLine>();
  34.     Vector2 scrollPos;
  35.  
  36.     static string scriptPath = "";
  37.  
  38.     static string searchText = "";
  39.  
  40.     void OnGUI()
  41.     {
  42.         var oldText = searchText;
  43.         var oldPath = scriptPath;
  44.  
  45.         GUILayout.Label("Input text to find");
  46.         searchText = GUILayout.TextField(searchText);
  47.         GUILayout.Label("Search folder path");
  48.         scriptPath = GUILayout.TextField(scriptPath);
  49.  
  50.         if (searchText.Equals(oldText) == false || scriptPath.Equals(oldPath) == false)
  51.         {
  52.             PlayerPrefs.SetString(pathKey, scriptPath);
  53.             PlayerPrefs.SetString(searchKey, searchText);
  54.         }
  55.  
  56.         if (GUILayout.Button("Find occurrences", GUILayout.Width(EditorGUIUtility.currentViewWidth)))
  57.         {
  58.             todos.Clear();
  59.  
  60.             if (searchText.Equals("") == false)
  61.             {
  62.                 var pathName = Application.dataPath.Substring(0, Application.dataPath.Length - "Assets".Length);
  63.                 pathName = pathName.Replace('/', '\\');
  64.                 var pathStartAt = pathName.Length;
  65.  
  66.                 string path = "Assets/" + scriptPath;
  67.                 var info = new DirectoryInfo(path);
  68.                 var fileInfo = info.GetFiles("*.cs", SearchOption.AllDirectories);
  69.                 foreach (var file in fileInfo)
  70.                 {
  71.                     var reader = file.OpenText();
  72.                     var text = reader.ReadToEnd();
  73.                     var rows = text.Split('\n');
  74.                     for (int i = 0; i < rows.Length; i++)
  75.                     {
  76.                         if (rows[i].Contains(searchText))
  77.                         {
  78.                             FindTextInScriptsLine newLine;
  79.                             newLine.text = rows[i].Trim();
  80.                             newLine.filename = file.Name;
  81.                             newLine.line = i;
  82.                             newLine.obj = AssetDatabase.LoadAssetAtPath(file.FullName.Substring(pathStartAt, file.FullName.Length - pathStartAt), typeof(MonoScript));
  83.                             todos.Add(newLine);
  84.                         }
  85.                     }
  86.  
  87.                     reader.Close();
  88.                 }
  89.             }
  90.             else
  91.             {
  92.                 Debug.LogWarning("Trying to search empty string is not a good idea...");
  93.             }
  94.         }
  95.  
  96.         scrollPos = EditorGUILayout.BeginScrollView(scrollPos, false, false);
  97.         for (int i = 0; i < todos.Count; i++)
  98.         {
  99.             float margin = 30;
  100.             float buttonWidth = 200;
  101.  
  102.             EditorGUILayout.BeginHorizontal();
  103.             GUILayout.FlexibleSpace();
  104.             GUILayout.Label(todos[i].text, GUILayout.Width(EditorGUIUtility.currentViewWidth - buttonWidth - margin));
  105.  
  106.             if (GUILayout.Button(todos[i].filename, GUILayout.Width(buttonWidth)))
  107.             {
  108.                 AssetDatabase.OpenAsset(todos[i].obj, todos[i].line + 1);
  109.             }
  110.             EditorGUILayout.EndHorizontal();
  111.         }
  112.         EditorGUILayout.EndScrollView();
  113.     }
  114. }
  115. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement