「Unity/3d」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→Prefabで複製したものを削除) |
|||
| 行52: | 行52: | ||
} | } | ||
} | } | ||
| + | ==アタッチオブジェクトのcanvasの位置== | ||
| + | SetParentを使う | ||
| + | <pre> | ||
| + | GameObject rObject; | ||
| + | GameObject rouletteImage; | ||
| + | GameObject prefab = (GameObject)Resources.Load("RObject"); | ||
| + | Vector3 position = new Vector3( | ||
| + | rouletteBackImage.transform.localPosition.x, | ||
| + | rouletteBackImage.transform.localPosition.y, | ||
| + | rouletteBackImage.transform.localPosition.z - 0.2f); | ||
| + | GameObject rObject = Instantiate(prefab, position, Quaternion.identity); | ||
| + | rObject.transform.SetParent(rouletteImage.transform); | ||
| + | rObject.name = "RObject" + angle; | ||
| + | </pre> | ||
2020年5月1日 (金) 16:23時点における版
3D_Helloworld
- 新規プロジェクトで3Dを選択
- Hieraruchyで3Dオブジェクト/cubeを選択
- cubeを(0, 0, 0)を選択
重力&衝突追加
- 地面となる板を追加するために3Dオブジェクト/Planeを追加
- 3Dオブジェクト/cubeを選択し追加
- cubeを選択し、Inspectorの最下のAddComponentからPhysics/Rigidbodyを選択
- cubeの衝突のComponent(Colinder)はもともとついてるので何もしなくて良い。
Prefabで複製を作成する
例としてcubeを複製
- Hieraruchyに3Dオブジェクト/cubeを選択し追加
- Project(Assetsの下に)にResourcesディレクトリを作成する
- cubeをResourcesディレクトリへドラッグする
以下コードで複製できる
for (int i = 0; i < 100; i++) {
float x = Random.Range(-5.0f, 5.0f);
float y = i * 2 - 4f;
float z = Random.Range(-5.0f, 5.0f);
GameObject prefab = (GameObject)Resources.Load("Sphere");
Vector3 position = new Vector3(x, y, z);
GameObject obj = Instantiate(prefab, position, Quaternion.identity);
obj.name = "sphere" + i;
}
Resourcesディレクトリはオブジェクトを読込時に使われるディレクトリ名でunityのルールらしい。
Prefabで複製したものを削除
List<GameObject> list_obj = new List<GameObject>();
for (int i = 0; i < 100; i++) {
float x = Random.Range(-5.0f, 5.0f);
float y = i * 2 - 4f;
float z = Random.Range(-5.0f, 5.0f);
GameObject prefab = (GameObject)Resources.Load("Sphere");
Vector3 position = new Vector3(x, y, z);
GameObject obj = Instantiate(prefab, position, Quaternion.identity);
}
list_obj.Add(obj);
// 削除チェック
for (int i = 0; i < list_obj.Count; i++)
{
GameObject obj = list_obj[i];
if (obj == null) {
continue;
}
if (obj.transform.position.y < -10) {
Destroy(list_obj[i]);
Debug.Log("削除 i=" + i);
}
}
アタッチオブジェクトのcanvasの位置
SetParentを使う
GameObject rObject;
GameObject rouletteImage;
GameObject prefab = (GameObject)Resources.Load("RObject");
Vector3 position = new Vector3(
rouletteBackImage.transform.localPosition.x,
rouletteBackImage.transform.localPosition.y,
rouletteBackImage.transform.localPosition.z - 0.2f);
GameObject rObject = Instantiate(prefab, position, Quaternion.identity);
rObject.transform.SetParent(rouletteImage.transform);
rObject.name = "RObject" + angle;
