Skip to content

Instantly share code, notes, and snippets.

@SiarheiPilat
Last active January 23, 2023 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SiarheiPilat/c0fdccc4c5befd5facea0ced6741e01c to your computer and use it in GitHub Desktop.
Save SiarheiPilat/c0fdccc4c5befd5facea0ced6741e01c to your computer and use it in GitHub Desktop.
Find all custom scripts in the hierarchy of the current scene, show as a list of buttons, user can ping the object by clicking a button. Omits native Unity components.
using UnityEngine;
using UnityEditor;
using System;
namespace Suasor
{
public class FindCustomScriptsEditor : EditorWindow
{
Vector2 scrollPosition;
public string[] Strings = new string[4] {"BreakableHelper", "ObjectColor", "StickerController", "Obi" }; // REPLACE THIS WITH LIST EDITABLE
[MenuItem("Tools/Suasor/FindCustomScriptsEditor")]
static void Init()
{
FindScriptsInScene window = (FindScriptsInScene)EditorWindow.GetWindow(typeof(FindScriptsInScene));
window.Show();
}
void OnGUI()
{
ScriptableObject target = this;
SerializedObject so = new SerializedObject(target);
SerializedProperty stringsProperty = so.FindProperty("Strings");
EditorGUILayout.PropertyField(stringsProperty, true); // True means show c$$anonymous$$ldren
so.ApplyModifiedProperties(); // Remember to apply modified properties
EditorGUILayout.BeginVertical();
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
MonoBehaviour[] scripts = FindObjectsOfType<MonoBehaviour>();
foreach (MonoBehaviour script in scripts)
{
Type scriptType = script.GetType();
var scope = scriptType.Namespace;
if ((scope == null || !scope.StartsWith("Unity")) && !StringContainsOneOfWords(script.GetType().FullName))
{
if (GUILayout.Button(script.GetType().FullName + ".cs on object " + script.gameObject.name))
{
EditorGUIUtility.PingObject(script.gameObject);
}
}
}
EditorGUILayout.EndScrollView();
EditorGUILayout.EndVertical();
}
bool StringContainsOneOfWords(string str)
{
if (Strings.Length == 0)
{
return false;
}
foreach (string word in Strings)
{
if (str.Contains(word))
{
return true;
}
}
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment