x


How can I see script dependencies?

I've recently joined a project as a coder and most of the scripts are in the Scripts folder. Sometimes I come across scripts that have duplicates and I want to delete one of them, but I cant figure out how I can see WHERE that script is beeing used. Other objects in Unity have the action "Select dependencies", but this doesnt seem to work for Scripts.

more ▼

asked Dec 30 '09 at 06:18 PM

Skjalg Sturlasson Maehre gravatar image

Skjalg Sturlasson Maehre
92 2 2 3

(comments are locked)
10|3000 characters needed characters left

6 answers: sort voted first

AngryAnt has a great script on his site that does the trick:

  • 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.

    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();
    
    }

    }

more ▼

answered Feb 18 '10 at 06:06 PM

JDonavan 1 gravatar image

JDonavan 1
462 2 6 18

Looks like you need to clean up your code segment, but yes, AngryAnt's ComponentLister script will do the trick. You can find the post here: http://angryant.com/general/tipsandtricks/where-did-that-component-go/

Feb 18 '10 at 06:30 PM burnumd

Yeah I tried cleaning it up. Every time it left something out.

Feb 18 '10 at 07:59 PM JDonavan 1
(comments are locked)
10|3000 characters needed characters left

I'm pretty sure this is not possible. My reasoning for this is that all scripts are included at runtime, and therefore all scripts are potentially available as types which could potentially be instantiated at runtime. In order for the editor to know whether any particular scripts are used or not, it would have to be able to somehow parse every possible runtime permutation of your code.

consider:

gameObject.AddComponent ("FoobarScript_" + Random.Range(a,b).ToString());

The editor would have to work out every potential value for a & b, before knowing which scripts might potentially be called on! :)

more ▼

answered Dec 30 '09 at 08:06 PM

duck gravatar image

duck ♦♦
40.9k 92 148 415

(comments are locked)
10|3000 characters needed characters left

This should be possible with editor scripting - but I think it's not trivial:

You'd have to go through all your scenes. Among other things, you'd use EditorApplication.OpenScene to do this. In the scenes, you should be able to use Object.FindObjectsOfType with the type you're looking for, to get all game objects that have the script you're looking for attached (actually, you get the components - but from there you can easily get the actual game object; and the hierarchy of the game objects is stored in the "transform" attribute of the components which the inherit from Component).

You should be able to select those objects with Selection.gameObjects - but since you're iterating through the scenes that won't help you much, so it's probably better to simply log the names of the game objects and maybe iterate through their transform.parent to get the full path. I haven't tried setting Selection.gameObjects - but the scripting reference doesn't say it's read-only so that should work (if you want to just do this for a single scene, that sure would be nice).

Finally, you'd have to check out all the prefabs in the project. One approach might be simple iterating through the file structure (that could be done with standard .NET API calls - DirectoryInfo.GetDirectories is one of the methods you'd be using for that) and then using AssetDatabase.LoadAssetAtPath to get "living instances" of any prefab and then checking if those contain the script you're looking for (via GetComponent - if it doesn't return null for the given type, the script is used).

I'm not sure this will all work (and you'll probably need a bit more than what I've linked here), and when it works (which I would hope) it'll be a bit of work to implement this - but it's certainly worth a try and would be a great example of the power of editor scripting ;-)

more ▼

answered Jan 01 '10 at 10:39 PM

jashan gravatar image

jashan
10.1k 25 40 116

(comments are locked)
10|3000 characters needed characters left

I'm sorry if I didn't phrase my question correctly, and that it was unclear. Since scripts are added to gameobjects using the unity inspector, I was merely looking for a way to see which gameobjects a certain script had been added to. Now I have to go through every gameobject by hand in the hierarchy or look at prefabs in the Project that I think has some relation to the script and see if they have been added to it.

more ▼

answered Jan 01 '10 at 05:28 PM

Skjalg Sturlasson Maehre gravatar image

Skjalg Sturlasson Maehre
92 2 2 3

(comments are locked)
10|3000 characters needed characters left

I tried a little improvement of Angry Ant's script. Basically all component are sorted and you can fold/unfold them for better readibility.

I did it during my student project (also my first Unity project). And now that the project is over, I'm happy to share it with the commmunity.

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;

/**
 * \author Nicolas Musset, Angry Ant (original version)
 * \date 2011-03-28
 * \class ComponentLister
 * \brief This class adds a window in the Unity editor that lists all
 *  components used in the current scene.
 */
public class ComponentLister : EditorWindow
{
    private SortedList<string, ArrayList> sets =
       new SortedList<string, ArrayList>();
    private SortedList<string, bool> foldedOuts =
       new SortedList<string, bool>();
    private Vector2 scrollPosition;

    /**
     * Menu entry for this window
     */
    [MenuItem("Component/Component Lister", priority = 41)]
    public static void MenuComponentLister()
    {
        EditorWindow window = GetWindow(typeof(ComponentLister));
        window.Show();
    }

    /**
     * Updates the list of components
     */
    public void UpdateList()
    {
        Object[] objects;
        objects = FindObjectsOfType(typeof(Component));

        sets.Clear();
        sets.Capacity = objects.Length;
        foldedOuts.Clear();
        foldedOuts.Capacity = objects.Length;

        foreach (Component component in objects)
        {
            string name = component.GetType().Name;
            if (!sets.ContainsKey(name))
            {
                sets[name] = new ArrayList();
                foldedOuts[name] = false;
            }

            (sets[name]).Add(component.gameObject);
        }
    }

    /**
     * Updates the list each time the hierarchy has been changed.
     * This also work when the scene is changed.
     */
    public void OnHierarchyChange()
    {
        UpdateList();
        Repaint();
    }

    /**
     * Override.
     */
    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 (string name in sets.Keys)
        {
            foldedOuts[name] =
          EditorGUILayout.Foldout(foldedOuts[name], name,
                                  new GUIStyle(EditorStyles.foldout)
                                  { fontStyle = FontStyle.Bold });
            if (foldedOuts[name])
            {
                foreach (GameObject gameObject in sets[name])
                {
                    if (GUILayout.Button(gameObject.name))
                    {
                        Selection.activeObject = gameObject;
                    }
                }
            }
        }

        GUILayout.EndScrollView();
    }
}
more ▼

answered Jun 08 '11 at 01:03 PM

Kryptos gravatar image

Kryptos
7.2k 5 32

Very handy script! Can't ask for more :-)

Apr 12 '12 at 11:48 AM hylde
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5051
x1663
x347

asked: Dec 30 '09 at 06:18 PM

Seen: 3666 times

Last Updated: Aug 30 '12 at 09:04 AM