facebook twitter hatena line email

「Unity/GameObject」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ResourceからGameObject生成)
(非アクティブへ)
(同じ利用者による、間の8版が非表示)
行31: 行31:
 
参考:http://sonnahiga.blog.fc2.com/blog-entry-18.html
 
参考:http://sonnahiga.blog.fc2.com/blog-entry-18.html
  
==非アクティブへ==
+
==アクティブ設定==
  obj.SetActive(true);
+
  obj.SetActive(true); // アクティブへ
 +
obj.SetActive(false); // 非アクティブ
 +
 
 +
==アクティブ判定==
 +
obj.activeSelf; // true or false
  
 
==オブジェクトより手前に==
 
==オブジェクトより手前に==
行45: 行49:
 
</pre>
 
</pre>
  
==ResourceからGameObject生成==
+
==ResourcesからGameObject生成==
 +
Assets/Resources/Sphere.prefabからロード
 
<pre>
 
<pre>
 
GameObject parent = GameObject.Find("parentObj");
 
GameObject parent = GameObject.Find("parentObj");
行53: 行58:
 
</pre>
 
</pre>
  
===ResouorceのdirからGameObject生成==
+
===ResourcesのdirからGameObject生成===
Play/Sphere.prefabからロード
+
Assets/Resources/Play/Sphere.prefabからロード
 +
GameObject parent = GameObject.Find("parentObj");
 
  GameObject prefab = (GameObject)Resources.Load("Play/Sphere");
 
  GameObject prefab = (GameObject)Resources.Load("Play/Sphere");
 +
Vector3 position = new Vector3(0, 0, 0);
 +
GameObject obj = Instantiate(prefab, position, Quaternion.identity, parent.transform);
  
 
==こちらも参照==
 
==こちらも参照==
 
[[Unity/Csharp/オブジェクト操作]] [ショートカット]
 
[[Unity/Csharp/オブジェクト操作]] [ショートカット]

2022年4月21日 (木) 22:43時点における版

子供のオブジェクト取得

GameObject child = obj.transform.Find("Child1Image").gameObject;

子供のオブジェクト一覧取得

Transform transforms  = obj.GetComponentInChildren<Transform>();
if (transforms.childCount == 0) {
    return;
}
foreach (Transform transform in transforms)
{
    GameObject body = transform.gameObject;
    Debug.Log("body.name=" + body.name);
}

配下のオブジェクトすべて取得

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

アクティブ設定

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

こちらも参照

Unity/Csharp/オブジェクト操作 [ショートカット]