Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if UNITY_EDITOR
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEditor;
- using UnityEngine;
- public class FindTextInScriptsWindow : EditorWindow
- {
- static string pathKey = "efpafvn98qnvq23ru9fse"; // How to make keys not crash with other keys ^:D
- static string searchKey = "rvi23uq4vekpfeaofi293gsp4";
- // Add menu named "My Window" to the Window menu
- [MenuItem("Window/FindTextInScripts Window")]
- static void Init()
- {
- // Get existing open window or if none, make a new one:
- FindTextInScriptsWindow window = (FindTextInScriptsWindow)EditorWindow.GetWindow(typeof(FindTextInScriptsWindow));
- window.Show();
- scriptPath = PlayerPrefs.GetString(pathKey, "");
- searchText = PlayerPrefs.GetString(searchKey, "");
- }
- struct FindTextInScriptsLine
- {
- public string text;
- public string filename;
- public int line;
- public Object obj;
- }
- List<FindTextInScriptsLine> todos = new List<FindTextInScriptsLine>();
- Vector2 scrollPos;
- static string scriptPath = "";
- static string searchText = "";
- void OnGUI()
- {
- var oldText = searchText;
- var oldPath = scriptPath;
- GUILayout.Label("Input text to find");
- searchText = GUILayout.TextField(searchText);
- GUILayout.Label("Search folder path");
- scriptPath = GUILayout.TextField(scriptPath);
- if (searchText.Equals(oldText) == false || scriptPath.Equals(oldPath) == false)
- {
- PlayerPrefs.SetString(pathKey, scriptPath);
- PlayerPrefs.SetString(searchKey, searchText);
- }
- if (GUILayout.Button("Find occurrences", GUILayout.Width(EditorGUIUtility.currentViewWidth)))
- {
- todos.Clear();
- if (searchText.Equals("") == false)
- {
- var pathName = Application.dataPath.Substring(0, Application.dataPath.Length - "Assets".Length);
- pathName = pathName.Replace('/', '\\');
- var pathStartAt = pathName.Length;
- string path = "Assets/" + scriptPath;
- var info = new DirectoryInfo(path);
- var fileInfo = info.GetFiles("*.cs", SearchOption.AllDirectories);
- foreach (var file in fileInfo)
- {
- var reader = file.OpenText();
- var text = reader.ReadToEnd();
- var rows = text.Split('\n');
- for (int i = 0; i < rows.Length; i++)
- {
- if (rows[i].Contains(searchText))
- {
- FindTextInScriptsLine newLine;
- newLine.text = rows[i].Trim();
- newLine.filename = file.Name;
- newLine.line = i;
- newLine.obj = AssetDatabase.LoadAssetAtPath(file.FullName.Substring(pathStartAt, file.FullName.Length - pathStartAt), typeof(MonoScript));
- todos.Add(newLine);
- }
- }
- reader.Close();
- }
- }
- else
- {
- Debug.LogWarning("Trying to search empty string is not a good idea...");
- }
- }
- scrollPos = EditorGUILayout.BeginScrollView(scrollPos, false, false);
- for (int i = 0; i < todos.Count; i++)
- {
- float margin = 30;
- float buttonWidth = 200;
- EditorGUILayout.BeginHorizontal();
- GUILayout.FlexibleSpace();
- GUILayout.Label(todos[i].text, GUILayout.Width(EditorGUIUtility.currentViewWidth - buttonWidth - margin));
- if (GUILayout.Button(todos[i].filename, GUILayout.Width(buttonWidth)))
- {
- AssetDatabase.OpenAsset(todos[i].obj, todos[i].line + 1);
- }
- EditorGUILayout.EndHorizontal();
- }
- EditorGUILayout.EndScrollView();
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement