読者です 読者をやめる 読者になる 読者になる

うさがにっき

読書感想文とプログラムのこと書いてきます

ScriptからGameObject作成

空のGameObject作成

// make blake GameObject
GameObject go = new GameObject("GameObject");

作成と同時にComponent attach

// attach Comonent
GameObject go2 = new GameObject("GameObject", typeof(Rigidbody));

attachしたComponent取得

// get attaching Component
Rigidbody rigd = new GameObject("GameObject", typeof(Rigidbody)).GetComponent<Rigidbody>();
Rigidbody rigd2 = new GameObject ("GameObject").AddComponent<Rigidbody> ();

attachしたと同時にHideFlg設定

// atttach Comonent and setting hideFlg(editor only)
// https://docs.unity3d.com/ScriptReference/EditorUtility.CreateGameObjectWithHideFlags.html
// https://docs.unity3d.com/ScriptReference/HideFlags.html
EditorUtility.CreateGameObjectWithHideFlags("GameObject", HideFlags.HideAndDontSave, typeof(Rigidbody));

プリミティブなGameObject作成

// make primitive gameobject
GameObject primitivego = GameObject.CreatePrimitive(PrimitiveType.Cube);

GameObject複製

GameObject copygo = (GameObject)Instantiate(go, transform.position, transform.rotation);

prefabから複製

// copy gameobject from prefab
GameObject pgo = (GameObject)Instantiate(prefab, transform.position, transform.rotation);

unity-bu.booth.pm