7
2

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.

【Unity】Sceneに含まれる特定のComponentをGameObjectが非活性なものまで含めて全て取得する

Last updated at Posted at 2019-06-08

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

7
2
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
7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?