Where did that component go?

by AngryAnt on August 31st, 2009

In larger Unity projects, components sometimes “get lost” – you refactor something and there’s an audio source still attached somewhere in your hierarchy and you simply cannot remember where.

You’re not the first one. A while ago I created a simple editor script to combat this issue as a response to a similar frustrated post on the Unity forums. Earlier this week I stumbled on the script again, picked it up and dusted it off a bit and decided to give it another chance to shine in some spotlight.

Use:

  • Save the script as ComponentLister.cs.
  • Place it in /Assets/Editor in your project.
  • Launch the window from the Component menu – should be the last item titled “Component lister”.
  • Click “Refresh” to list all components in your scene and the GameObjects to which they are attached.
  • To investigate, click a GameObject name in the list and it will be set as the active selection in the hierarchy.

Codes:

using UnityEngine;
using UnityEditor;
using System.Collections; 

public class ComponentLister : EditorWindow
{
    private Hashtable sets = new Hashtable();
    private Vector2 scrollPosition;

    [ MenuItem( "Component/Component lister" ) ]
    public static void Launch()
    {
        EditorWindow window = GetWindow( typeof( ComponentLister ) );
        window.Show();
    } 

    public void UpdateList()
    {
        Object[] objects; 

        sets.Clear(); 

        objects = FindObjectsOfType( typeof( Component ) );
        foreach( Component component in objects )
        {
            if( !sets.ContainsKey( component.GetType() ) )
            {
                sets[ component.GetType() ] = new ArrayList();
            } 

            ( ( ArrayList )sets[ component.GetType() ] ).Add( component.gameObject );
        }
    } 

    public void OnGUI()
    {
        GUILayout.BeginHorizontal( GUI.skin.GetStyle( "Box" ) );
            GUILayout.Label( "Components in scene:" );
            GUILayout.FlexibleSpace();

            if( GUILayout.Button( "Refresh" ) )
            {
                UpdateList();
            }
        GUILayout.EndHorizontal();

        scrollPosition = GUILayout.BeginScrollView(scrollPosition);

            foreach( System.Type type in sets.Keys )
            {
                GUILayout.Label( type.Name + ":" );
                foreach( GameObject gameObject in ( ArrayList )sets[ type ] )
                {
                    if( GUILayout.Button( gameObject.name ) )
                    {
                        Selection.activeObject = gameObject;
                    }
                }
            } 

        GUILayout.EndScrollView();
    }
}
4 Comments
  1. socksy permalink

    “Earlier this week I stumbled on the script again”

    So you, er, lost it? ;)

  2. Nowai! I *filed* it – y’know? ;)

  3. Alphageek permalink

    I came across this gem of a script today – fantastic, thanks!

    However I needed it to work with the iPhone Unity 1.5 environment where the “EditorWindow.GetWindow” method doesn’t exist. So I changed your Launch() method to:

    —–
    public static void Launch()
    {
    {
    if (window)
    DestroyImmediate(window);

    window = new ComponentLister() ;
    window.Show (true);
    }
    }

    —-
    and added the static class field

    public static ComponentLister window;

    It works with the iPhone environment now :)

    Cheers!

  4. Nahuel permalink

    i just love the support =) .

    thank you.

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS

Spam protection by WP Captcha-Free