GameObjectがActiveならFindObjectsOfType()
で済みますが、非活性だと取得できません。
そこで非活性なものまで含めて取得できるコードを書きました。
#コード
MyGameObjectUtility.cs
using System.Collections.Generic;
using System.Linq;
using UnityEngine.SceneManagement;
public static class MyGameObjectUtility
{
// 通常trueしか指定しないのでデフォルト引数をtrueにしてます
public static T[] GetComponentsInActiveScene<T>(bool includeInactive = true)
{
// ActiveなSceneのRootにあるGameObject[]を取得する
var rootGameObjects = SceneManager.GetActiveScene().GetRootGameObjects();
// 空の IEnumerable<T>
IEnumerable<T> resultComponents = (T[])Enumerable.Empty<T>();
foreach (var item in rootGameObjects)
{
// includeInactive = true を指定するとGameObjectが非活性なものからも取得する
var components = item.GetComponentsInChildren<T>(includeInactive);
resultComponents = resultComponents.Concat(components);
}
return resultComponents.ToArray();
}
// 1つだけ取得したい場合はこちら(GetComponentsInActiveSceneを元にして書いたので少し非効率です)
public static T GetComponentInActiveScene<T>(bool includeInactive = true)
{
// ActiveなSceneのRootにあるGameObject[]を取得する
var rootGameObjects = SceneManager.GetActiveScene().GetRootGameObjects();
// 空の IEnumerable<T>
IEnumerable<T> resultComponents = (T[])Enumerable.Empty<T>();
foreach (var item in rootGameObjects)
{
// includeInactive = true を指定するとGameObjectが非活性なものからも取得する
var components = item.GetComponentsInChildren<T>(includeInactive);
resultComponents = resultComponents.Concat(components);
}
return resultComponents.First();
}
}
#使用例
ゲーム中というよりは、主にInspectorの参照を取得する際に使用するのを想定してます。
MenuButtonsHandler.cs
using UnityEngine;
public class MenuButtonsHandler : MonoBehaviour
{
[SerializeField] MenuContentHandler[] MenuContentHandlers;
// Scriptをアタッチした際に自動で呼ばれる, Inspectorからも呼べる
private void Reset()
{
MenuContentHandlers = MyGameObjectUtility.GetComponentsInActiveScene<MenuContentHandler>();
}
}
UiHub.cs
using UnityEngine;
public class UiHub : MonoBehaviour
{
public static UiHub Instance;
public PartyScrollHandler PartyScrollHandler;
public PartyEditView PartyEditView;
public HolderScrollHandler HolderScrollHandler;
public EquipmentEditScrollHandler EquipmentEditScrollHandler;
public SummonScrollHandler SummonScrollHandler;
public UnitCardScrollHandler UnitCardScrollHandler;
public ComposeView ComposeView;
public ResultInfoWindow ResultInfoWindow;
public ComposeOptionSelectScroll ComposeOptionSelectScroll;
public ComposeOptionConfirmScroll ComposeOptionConfirmScroll;
public AdventureScrollHandler AdventureScrollHandler;
public BattleLogScrollHandler BattleLogScrollHandler;
public JobScrollHandler JobScrollHandler;
public MessageWindow MessageWindow;
public DungeonScrollHandler DungeonScrollHandler;
private void Awake()
{
Instance = this;
}
private void Reset()
{
PartyScrollHandler = MyGameObjectUtility.GetComponentInActiveScene<PartyScrollHandler>();
PartyEditView = MyGameObjectUtility.GetComponentInActiveScene<PartyEditView>();
HolderScrollHandler = MyGameObjectUtility.GetComponentInActiveScene<HolderScrollHandler>();
EquipmentEditScrollHandler = MyGameObjectUtility.GetComponentInActiveScene<EquipmentEditScrollHandler>();
SummonScrollHandler = MyGameObjectUtility.GetComponentInActiveScene<SummonScrollHandler>();
UnitCardScrollHandler = MyGameObjectUtility.GetComponentInActiveScene<UnitCardScrollHandler>();
ComposeView = MyGameObjectUtility.GetComponentInActiveScene<ComposeView>();
ResultInfoWindow = MyGameObjectUtility.GetComponentInActiveScene<ResultInfoWindow>();
ComposeOptionSelectScroll = MyGameObjectUtility.GetComponentInActiveScene<ComposeOptionSelectScroll>();
ComposeOptionConfirmScroll = MyGameObjectUtility.GetComponentInActiveScene<ComposeOptionConfirmScroll>();
AdventureScrollHandler = MyGameObjectUtility.GetComponentInActiveScene<AdventureScrollHandler>();
BattleLogScrollHandler = MyGameObjectUtility.GetComponentInActiveScene<BattleLogScrollHandler>();
JobScrollHandler = MyGameObjectUtility.GetComponentInActiveScene<JobScrollHandler>();
MessageWindow = MyGameObjectUtility.GetComponentInActiveScene<MessageWindow>();
DungeonScrollHandler = MyGameObjectUtility.GetComponentInActiveScene<DungeonScrollHandler>();
}
}