LoginSignup
7
7

More than 5 years have passed since last update.

タグでシーン内のゲームオブジェクトを検索するエディタ拡張

Last updated at Posted at 2015-06-14

シーン内のゲームオブジェクトをタグで検索できるスクリプトがUnity wikiにあったので紹介します。

FindSceneObjectsWithTag

以下は、自分用に少し修正したものです。Editorフォルダに入れて使って下さい。下図の様に、指定したタグのゲームオブジェクトを検索できます。 (この例ではPlayerタグがついたゲームオブジェクトを検索しています)
FindByTag2.png

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

public class FindSceneObjectsWithTag : EditorWindow
{
    public string TagToSearchFor = "Player";

    public bool LimitResultCount = false;
    public int MaxResults = 1;

    public List<GameObject> Results;
    private Vector2 ResultScrollPos;

    void OnGUI()
    {
        EditorGUILayout.BeginVertical();
        {
            EditorGUILayout.LabelField("Options", EditorStyles.boldLabel);
            {
                GUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Tag : ", GUILayout.MaxWidth(50));
                TagToSearchFor = EditorGUILayout.TagField(TagToSearchFor);
                GUILayout.EndHorizontal();

                if (GUILayout.Button("Find"))
                    Find();

                if (LimitResultCount = EditorGUILayout.Foldout(LimitResultCount, "Limit Result Count (Limit:"
                        + (LimitResultCount ? MaxResults.ToString() : "None") + ")"))
                    MaxResults = EditorGUILayout.IntField("Result Max:", MaxResults);
            }

            EditorGUILayout.LabelField("Results", EditorStyles.boldLabel);
            {
                if (Results != null)
                {
                    EditorGUILayout.LabelField("Scene objects found:", Results.Count.ToString(), EditorStyles.boldLabel);

                    ResultScrollPos = EditorGUILayout.BeginScrollView(ResultScrollPos);
                    {
                        if (LimitResultCount)
                        {
                            for (int i = 0; i < Mathf.Min(MaxResults, Results.Count); i++)
                                EditorGUILayout.ObjectField(Results[i], typeof(GameObject), false);
                        }
                        else
                        {
                            foreach (GameObject go in Results)
                                EditorGUILayout.ObjectField(go, typeof(GameObject), false);
                        }
                    }
                    EditorGUILayout.EndScrollView();
                }
            }
        }
        EditorGUILayout.EndVertical();
    }

    void Find()
    {
        if (TagToSearchFor != null && TagToSearchFor != "")
            Results = new List<GameObject>(GameObject.FindGameObjectsWithTag(TagToSearchFor));
    }

    [MenuItem("Tools/Find By Tag...")]
    static void Init()
    {
        FindSceneObjectsWithTag window
            = EditorWindow.GetWindow<FindSceneObjectsWithTag>("Find By Tag");

        window.ShowPopup();
        //window.ShowAuxWindow();
    }
}
7
7
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7