Script Dependencies Function for Unity

using UnityEngine;
using UnityEditor;
using System.Collections; 

public class ComponentLister : EditorWindow
{
    bool forceRefresh=false;

    private bool onlyScripts=false;
    private string filterComponent="";
    private SortedList sets = new SortedList(new myReverserClass());
    private ArrayList scripts = new ArrayList();
    private Vector2 scrollPosition;

   public class myReverserClass : IComparer  {

      // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
      public int Compare( System.Object x, System.Object y )  {
          return ( (new CaseInsensitiveComparer()).Compare( ((System.Type)x).Name, ((System.Type)y).Name ) );
      }
   }

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


    public void UpdateList()
    {
        Object[] objects; 

        sets.Clear(); 
        scripts.Clear();

        objects = FindObjectsOfType( typeof( Component ) );
        foreach( Component component in objects )
        {
            if (component is MonoBehaviour) {
                //Debug.Log(component.GetType() + " attached to "+component.name);
                scripts.Add(component.GetType());
            }

            if( !sets.ContainsKey( component.GetType() ) )
            {
                sets.Add(component.GetType(),new ArrayList());
            } 
            int i=sets.IndexOfKey(component.GetType());
            ( ( ArrayList )sets.GetByIndex(i)).Add( (string)component.gameObject.name );
        }

        foreach( Component component in objects )
        {
            int i=sets.IndexOfKey(component.GetType());
            ( ( ArrayList )sets.GetByIndex(i)).Sort();
        }       

    } 

    public void OnHierarchyChange() {
        //Debug.Log("OnHierarchyChange");
        UpdateList();
        Repaint();
        forceRefresh = true;
    }

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

            GUILayout.FlexibleSpace();

            if( GUILayout.Button( "Refresh" ) || forceRefresh )
            {
                //if (forceRefresh) Debug.Log("forceRefresh");
                forceRefresh=false;
                UpdateList();

            }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal( GUI.skin.GetStyle( "Box" ) );
        filterComponent = EditorGUILayout.TextField ("Filter:", filterComponent);
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal( GUI.skin.GetStyle( "Box" ) );
        onlyScripts = EditorGUILayout.Toggle("Show only scripts", onlyScripts);
        GUILayout.EndHorizontal();

        scrollPosition = GUILayout.BeginScrollView(scrollPosition);


            foreach( System.Type type in sets.Keys )
            {   
                if (type.Name.ToLower().Contains(filterComponent.ToLower())) {
                    bool showGameObjects=false;
                    if (scripts.Contains(type)) {
                        GUI.contentColor = Color.yellow;
                        GUILayout.Label( type.Name + " (Script):" );
                        showGameObjects=true;
                    }
                    else if (!onlyScripts){
                        GUI.contentColor = Color.white;
                        GUILayout.Label( type.Name + ":" );
                        showGameObjects=true;
                    }

                    if (showGameObjects) {
                        GUI.contentColor = Color.white;
                        foreach( string gameObject in ( ArrayList )sets[ type ] )
                        {
                            if( GUILayout.Button( gameObject ) )
                            {
                                Selection.activeObject = GameObject.Find(gameObject);
                            }
                        }
                    }
                }
            } 

        GUILayout.EndScrollView();
    }
}

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다