Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.62 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.         LoadPrefs();
  22.     }
  23.  
  24.     static void LoadPrefs()
  25.     {
  26.         scriptPath = PlayerPrefs.GetString(pathKey, "");
  27.         searchText = PlayerPrefs.GetString(searchKey, "");
  28.     }
  29.  
  30.     struct FindTextInScriptsLine
  31.     {
  32.         public string text;
  33.         public string filename;
  34.         public int line;
  35.         public Object obj;
  36.     }
  37.  
  38.     List<FindTextInScriptsLine> todos = new List<FindTextInScriptsLine>();
  39.     Vector2 scrollPos;
  40.  
  41.     static string scriptPath = "";
  42.  
  43.     static string searchText = "";
  44.  
  45.     static bool checkedReload = false;
  46.     void OnGUI()
  47.     {
  48.         // To prevent reloads from causing prefs to dissapear
  49.         if (checkedReload == false)
  50.         {
  51.             checkedReload = true;
  52.             LoadPrefs();
  53.         }
  54.  
  55.         var oldText = searchText;
  56.         var oldPath = scriptPath;
  57.  
  58.         GUILayout.Label("Input text to find");
  59.         searchText = GUILayout.TextField(searchText);
  60.         GUILayout.Label("Search folder path");
  61.         scriptPath = GUILayout.TextField(scriptPath);
  62.  
  63.         if (searchText.Equals(oldText) == false || scriptPath.Equals(oldPath) == false)
  64.         {
  65.             PlayerPrefs.SetString(pathKey, scriptPath);
  66.             PlayerPrefs.SetString(searchKey, searchText);
  67.         }
  68.  
  69.  
  70.         if (GUILayout.Button("Find occurrences", GUILayout.Width(EditorGUIUtility.currentViewWidth)))
  71.         {
  72.             todos.Clear();
  73.  
  74.             if (searchText.Equals("") == false)
  75.             {
  76.                 var pathName = Application.dataPath.Substring(0, Application.dataPath.Length - "Assets".Length);
  77.                 pathName = pathName.Replace('/', '\\');
  78.                 var pathStartAt = pathName.Length;
  79.  
  80.                 string path = "Assets/" + scriptPath;
  81.                 var info = new DirectoryInfo(path);
  82.                 var fileInfo = info.GetFiles("*.cs", SearchOption.AllDirectories);
  83.                 foreach (var file in fileInfo)
  84.                 {
  85.                     var reader = file.OpenText();
  86.                     var text = reader.ReadToEnd();
  87.                     var rows = text.Split('\n');
  88.                     for (int i = 0; i < rows.Length; i++)
  89.                     {
  90.                         if (rows[i].Contains(searchText))
  91.                         {
  92.                             FindTextInScriptsLine newLine;
  93.                             newLine.text = rows[i].Trim();
  94.                             newLine.filename = file.Name;
  95.                             newLine.line = i;
  96.                             newLine.obj = AssetDatabase.LoadAssetAtPath(file.FullName.Substring(pathStartAt, file.FullName.Length - pathStartAt), typeof(MonoScript));
  97.                             todos.Add(newLine);
  98.                         }
  99.                     }
  100.  
  101.                     reader.Close();
  102.                 }
  103.             }
  104.             else
  105.             {
  106.                 Debug.LogWarning("Trying to search empty string is not a good idea...");
  107.             }
  108.         }
  109.  
  110.         scrollPos = EditorGUILayout.BeginScrollView(scrollPos, false, false);
  111.         for (int i = 0; i < todos.Count; i++)
  112.         {
  113.             float margin = 30;
  114.             float buttonWidth = 200;
  115.  
  116.             EditorGUILayout.BeginHorizontal();
  117.             GUILayout.FlexibleSpace();
  118.             //GUILayout.Label(todos[i].text, GUILayout.Width(EditorGUIUtility.currentViewWidth - buttonWidth - margin));
  119.             //GUILayout.Box(todos[i].text, GUILayout.Width(EditorGUIUtility.currentViewWidth - buttonWidth - margin));
  120.             GUILayout.TextField(todos[i].text, GUI.skin.label, GUILayout.Width(EditorGUIUtility.currentViewWidth - buttonWidth - margin));
  121.  
  122.             if (GUILayout.Button(todos[i].filename, GUILayout.Width(buttonWidth)))
  123.             {
  124.                 AssetDatabase.OpenAsset(todos[i].obj, todos[i].line + 1);
  125.             }
  126.             EditorGUILayout.EndHorizontal();
  127.         }
  128.         EditorGUILayout.EndScrollView();
  129.     }
  130. }
  131. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement