5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at

レイヤーでシーン内のゲームオブジェクトを検索するエディタスクリプトです。

下図のようにレイヤーを指定して、そのレイヤーに属するシーン内のゲームオブジェクトを検索、列挙できます。
下図はEventレイヤーに属するゲームオブジェクトを検索、列挙している例です。
Editorフォルダに入れて使って下さい。

FindByLayer2.png

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

public class FindSceneObjectsWithLayer : EditorWindow
{
	public int LayerToSearchFor = 0;

	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("Layer : ", GUILayout.MaxWidth(60));
				LayerToSearchFor = EditorGUILayout.LayerField(LayerToSearchFor);
				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()
	{
		GameObject[] goArray = FindObjectsOfType(typeof(GameObject)) as GameObject[];
		Results = new List<GameObject>();
		for (int i = 0; i < goArray.Length; i++)
		{
			if (goArray[i].layer == LayerToSearchFor)
			{
				Results.Add(goArray[i]);
			}
		}
	}

	[MenuItem("Tools/Find By Layer...")]
	static void Init()
	{
		FindSceneObjectsWithLayer window = EditorWindow.GetWindow<FindSceneObjectsWithLayer>("Find By Layer");
		window.ShowPopup();
		//window.ShowAuxWindow();
	}
}
5
4
0

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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?