- Home /
[Editor] Find all objects with a particular script in the scene
Hello there,
I am trying to write a editor script which selects all the game objects that has a particular script attached to. So far, I found a way to search for the scripts in the scene and show the list of monobehaviours,using reflection and also selecting works properly. The below is the script used for that,
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System;
public class ScriptFinder : EditorWindow
{
static ScriptFinder window;
static string scriptValue="";
static string oldScriptValue;
static List<Type> components;
static List<string> componentNames;
static List<GameObject> sceneObjects;
static int selectedIndex=0;
static int prevIndex=0;
static Vector2 scrollValue=Vector2.zero;
//public
// Use this for initialization
[MenuItem("EditorUtility/Script Finder")]
static void OpenScriptFinder()
{
//EditorUtility.FocusProjectWindow();
window = (ScriptFinder) EditorWindow.GetWindow (typeof (ScriptFinder));
/*
Assembly _asm = Assembly.GetAssembly(typeof(AnimationComponent));
AssemblyName _asmName = _asm.GetName();
Debug.Log(_asmName.FullName);
*/
Assembly _assembly = Assembly.Load("Assembly-CSharp");
components = new List<Type>();
componentNames = new List<string>();
foreach ( Type type in _assembly.GetTypes())
{
if(type.IsClass)
{
if(type.BaseType.FullName.Contains("MonoBehaviour")){
components.Add(type);
componentNames.Add(type.Name);
// Debug.Log(type.Name);
}
else
{
if(!type.BaseType.FullName.Contains("System"))
{
Type _type = type.BaseType;
components.Add(_type);
componentNames.Add(type.Name);
// Debug.Log(type.Name);
}
}
}
}
prevIndex = selectedIndex;
SelectScript(components[selectedIndex]);
}
static void SelectScript(Type _type)
{
Debug.Log("Selected Type: "+_type);
sceneObjects = new List<GameObject>();
// Debug.Log(GameObject.FindSceneObjectsOfType(typeof(GameObject)).Length);
foreach(UnityEngine.Object _obj in GameObject.FindSceneObjectsOfType(_type))
{
Debug.Log(_obj.name);
sceneObjects.Add(GameObject.Find(_obj.name));
}
Selection.objects = sceneObjects.ToArray();
}
void OnGUI()
{
selectedIndex = EditorGUILayout.Popup(selectedIndex,componentNames.ToArray());
if(selectedIndex!=prevIndex)
{
SelectScript(components[selectedIndex]);
}
scrollValue=EditorGUILayout.BeginScrollView(scrollValue);
foreach(GameObject _obj in sceneObjects)
{
if(GUILayout.Button(_obj.name,GUIStyle.none))
{
Selection.activeObject = _obj;
EditorGUIUtility.PingObject(_obj);
}
}
EditorGUILayout.EndScrollView();
prevIndex = selectedIndex;
}
}
Create a script called ScriptFinder and copy the code, it works perfectly fine. But the problem is that, it is working only for C# scripts, is there a way to find the javascript classes using this method or any other approach?
Please help!!
Answer by whydoidoit · Apr 10, 2013 at 11:56 AM
Um - I hate to be the bearer of bad news - but it already does that :)
Type the script name into the search box on the hierarchy view. (You can also prefix it with t: to ensure it doesn't select any objects with that name).
BTW you can find all scripts by doing:
Resources.FindObjectsOfTypeAll(typeof(MonoScript))
The MonoScript will give you the class defined by the script and excluding ones without hideflags will ignore the system ones you don't want.
You can check all assemblies by doing
AppDomain.CurrentDomain.GetAssemblies()
Ah damn, wasted half a day on this. =/
Anyway I will try to complete this, since you got to enter the whole name of the component which is also case sensitive. I would feel lazy to do it most of the time. ;)
any suggestions on how to do this for unityscript too?!
thanks for your reply.. It seems i jus missed that small part.
edit the answer and add the first comment to it plz :)
Resources.FindObjectsOfTypeAll(typeof($$anonymous$$onoScript)) fails to find scripts in loaded dlls.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Cross project scripting? 1 Answer
How can an editor script know when another script was removed from the project? 1 Answer
Detecting serialization reload in editor 1 Answer
How to get variables of the script show in unity editor/inspector? 2 Answers