Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created March 7, 2014 05:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/9405694 to your computer and use it in GitHub Desktop.
Save tsubaki/9405694 to your computer and use it in GitHub Desktop.
シーン内のルートオブジェクトを取得するメソッドをtransformに追加する
using UnityEngine;
using System.Collections;
using System;
public static class TransformExtension
{
public static GameObject[] FindRootObject (this Transform transform)
{
return Array.FindAll (GameObject.FindObjectsOfType<GameObject> (), (item) => item.transform.parent == null);
}
}
@tsubaki
Copy link
Author

tsubaki commented Mar 7, 2014

実際に使う場合はこんな感じで使う。
ただactiveじゃないオブジェクトは取得できないので注意。その手の物は自己登録式でリスト作ったほうが効率的(もしくはエディタ拡張でビルド時にリスト作っちゃうか)

using UnityEngine;

public class Sample : MonoBehaviour
{
    void Start ()
    {
        var rootObjects = transform.FindRootObject ();
        foreach (var obj in rootObjects) {
            Debug.Log (obj.name);
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment