Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by flamy · Apr 10, 2013 at 11:17 AM · editorutility

[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!!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
9
Best Answer

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()
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image flamy · Apr 10, 2013 at 12:10 PM 0
Share

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?!

avatar image flamy · Apr 10, 2013 at 12:12 PM 0
Share

thanks for your reply.. It seems i jus missed that small part.

edit the answer and add the first comment to it plz :)

avatar image whydoidoit · Apr 10, 2013 at 12:14 PM 0
Share

Done......

avatar image Gamegineer · Nov 08, 2013 at 10:07 PM 1
Share

Resources.FindObjectsOfTypeAll(typeof($$anonymous$$onoScript)) fails to find scripts in loaded dlls.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges