「Unity/GameObject」の版間の差分
ナビゲーションに移動
検索に移動
編集の要約なし |
|||
| (同じ利用者による、間の34版が非表示) | |||
| 2行目: | 2行目: | ||
GameObject child = obj.transform.Find("Child1Image").gameObject; | GameObject child = obj.transform.Find("Child1Image").gameObject; | ||
== | ==子供のオブジェクト一覧取得== | ||
obj.SetActive(true); | <pre> | ||
Transform transforms = obj.transform; | |||
// Transform transforms = obj.GetComponentInChildren<Transform>(); // これより、上の行のように"transform"を、直接使う方が負荷がかからずよい。 | |||
if (transforms.childCount == 0) { | |||
return; | |||
} | |||
foreach (Transform transform in transforms) | |||
{ | |||
GameObject body = transform.gameObject; | |||
Debug.Log("body.name=" + body.name); | |||
} | |||
</pre> | |||
==子供のオブジェクト削除== | |||
<pre> | |||
Transform transforms = obj.transform; | |||
// Transform transforms = obj.GetComponentInChildren<Transform>(); // これより、上の行のように"transform"を、直接使う方が負荷がかからずよい。 | |||
if (transforms.childCount > 0) { | |||
foreach (Transform transform in transforms) | |||
{ | |||
Destroy(transform.gameObject); | |||
} | |||
} | |||
</pre> | |||
==配下のオブジェクトすべて取得== | |||
<pre> | |||
public void AllUnderObject(GameObject obj) | |||
{ | |||
// ここで処理 | |||
Transform transforms = obj.GetComponentInChildren<Transform>(); | |||
if (transforms.childCount > 0) | |||
{ | |||
foreach (Transform transform in transforms) | |||
{ | |||
AllUnderObject(transform.gameObject); // 再帰処理 | |||
} | |||
} | |||
} | |||
</pre> | |||
参考:http://sonnahiga.blog.fc2.com/blog-entry-18.html | |||
==ルート配下のゲームオブジェクトリストを取得== | |||
GameObject[] objs = SceneManager.GetActiveScene().GetRootGameObjects() | |||
==現在のシーン名取得== | |||
using UnityEngine.SceneManagement; | |||
string sceneName = SceneManager.GetActiveScene().name; | |||
==アクティブ設定== | |||
obj.SetActive(true); // アクティブへ | |||
obj.SetActive(false); // 非アクティブ | |||
==アクティブ判定== | |||
obj.activeSelf; // true or false | |||
==オブジェクトより手前に== | ==オブジェクトより手前に== | ||
| 10行目: | 64行目: | ||
==ボタンなどが貼り付けられてるクラスからGameObject取得== | ==ボタンなどが貼り付けられてるクラスからGameObject取得== | ||
GameObject btnObj =GameObject.Find("Button"); | <pre> | ||
GameObject btnObj = GameObject.Find("Button"); | |||
Button btn = btnObj.GetComponent<Button>(); | Button btn = btnObj.GetComponent<Button>(); | ||
GameObject obj = btn.gameObject; | GameObject obj = btn.gameObject; | ||
</pre> | |||
==ResourcesからGameObject生成== | |||
Assets/Resources/Sphere.prefabからロード | |||
<pre> | |||
GameObject parent = GameObject.Find("parentObj"); | |||
GameObject prefab = (GameObject)Resources.Load("Sphere"); | |||
Vector3 position = new Vector3(0, 0, 0); | |||
GameObject obj = Instantiate(prefab, position, Quaternion.identity, parent.transform); | |||
</pre> | |||
===ResourcesのdirからGameObject生成=== | |||
Assets/Resources/Play/Sphere.prefabからロード | |||
GameObject parent = GameObject.Find("parentObj"); | |||
GameObject prefab = (GameObject)Resources.Load("Play/Sphere"); | |||
Vector3 position = new Vector3(0, 0, 0); | |||
GameObject obj = Instantiate(prefab, position, Quaternion.identity, parent.transform); | |||
==シーン遷移しても使いたい場合== | |||
<pre> | |||
void Start () { | |||
DontDestroyOnLoad(this); | |||
} | |||
</pre> | |||
==GameObjectの階層移動== | |||
例:Cameraオブジェクトをavaterオブジェクトの下へ | |||
GameObject.Find("Main Camera").transform.parent = avatar.transform; | |||
==静的オブジェクト化== | |||
GameObjectのInspectorを開いて、staticにチェックを入れる。 | |||
GameObjectを作って"StaticObjects"という名前にして、その下に置くとか管理すると良いかも。 | |||
==こちらも参照== | |||
[[Unity/Csharp/オブジェクト操作]] [ショートカット] | |||
2026年1月29日 (木) 13:47時点における最新版
子供のオブジェクト取得
GameObject child = obj.transform.Find("Child1Image").gameObject;
子供のオブジェクト一覧取得
Transform transforms = obj.transform;
// Transform transforms = obj.GetComponentInChildren<Transform>(); // これより、上の行のように"transform"を、直接使う方が負荷がかからずよい。
if (transforms.childCount == 0) {
return;
}
foreach (Transform transform in transforms)
{
GameObject body = transform.gameObject;
Debug.Log("body.name=" + body.name);
}
子供のオブジェクト削除
Transform transforms = obj.transform;
// Transform transforms = obj.GetComponentInChildren<Transform>(); // これより、上の行のように"transform"を、直接使う方が負荷がかからずよい。
if (transforms.childCount > 0) {
foreach (Transform transform in transforms)
{
Destroy(transform.gameObject);
}
}
配下のオブジェクトすべて取得
public void AllUnderObject(GameObject obj)
{
// ここで処理
Transform transforms = obj.GetComponentInChildren<Transform>();
if (transforms.childCount > 0)
{
foreach (Transform transform in transforms)
{
AllUnderObject(transform.gameObject); // 再帰処理
}
}
}
参考:http://sonnahiga.blog.fc2.com/blog-entry-18.html
ルート配下のゲームオブジェクトリストを取得
GameObject[] objs = SceneManager.GetActiveScene().GetRootGameObjects()
現在のシーン名取得
using UnityEngine.SceneManagement; string sceneName = SceneManager.GetActiveScene().name;
アクティブ設定
obj.SetActive(true); // アクティブへ obj.SetActive(false); // 非アクティブ
アクティブ判定
obj.activeSelf; // true or false
オブジェクトより手前に
Transform trans1 = GameObject.Find("Object1").transform;
trans1.SetSiblingIndex(0);
ボタンなどが貼り付けられてるクラスからGameObject取得
GameObject btnObj = GameObject.Find("Button");
Button btn = btnObj.GetComponent<Button>();
GameObject obj = btn.gameObject;
ResourcesからGameObject生成
Assets/Resources/Sphere.prefabからロード
GameObject parent = GameObject.Find("parentObj");
GameObject prefab = (GameObject)Resources.Load("Sphere");
Vector3 position = new Vector3(0, 0, 0);
GameObject obj = Instantiate(prefab, position, Quaternion.identity, parent.transform);
ResourcesのdirからGameObject生成
Assets/Resources/Play/Sphere.prefabからロード
GameObject parent = GameObject.Find("parentObj");
GameObject prefab = (GameObject)Resources.Load("Play/Sphere");
Vector3 position = new Vector3(0, 0, 0);
GameObject obj = Instantiate(prefab, position, Quaternion.identity, parent.transform);
シーン遷移しても使いたい場合
void Start () {
DontDestroyOnLoad(this);
}
GameObjectの階層移動
例:Cameraオブジェクトをavaterオブジェクトの下へ
GameObject.Find("Main Camera").transform.parent = avatar.transform;
静的オブジェクト化
GameObjectのInspectorを開いて、staticにチェックを入れる。
GameObjectを作って"StaticObjects"という名前にして、その下に置くとか管理すると良いかも。
こちらも参照
Unity/Csharp/オブジェクト操作 [ショートカット]