「Unity/3d/Prefab」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→Sceneに配置されてるPrefabの修正を、Prefab側に反映) |
|||
| 行79: | 行79: | ||
もしくはInstantiateの第4パラメータを使う。 | もしくはInstantiateの第4パラメータを使う。 | ||
GameObject obj = Instantiate(prefab, position, Quaternion.identity, rouletteBackImage.transform); | GameObject obj = Instantiate(prefab, position, Quaternion.identity, rouletteBackImage.transform); | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
2023年12月5日 (火) 11:15時点における版
目次
Prefabからインスタンスを設置する
例としてcubeを設置
- Hieraruchyに3Dオブジェクト/cubeを選択し追加
- Project(Assetsの下に)にPrefabsディレクトリを作成する。(Prefabs名はわかりやすい名前で何でも良い)
- cubeをPrefabsディレクトリへドラッグする
以下コードで設置できる
[SerializeField] GameObject prefab;
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 obj = Instantiate(prefab, Vector3.zero, Quaternion.identity);
// GameObject obj = Instantiate(prefab, Vector3.zero, Quaternion.Euler(0, 0, 180f)); // 回転を含む場合
obj.name = "sphere" + i;
}
上記csを貼り付けた、オブジェクトのinspectorを開いて、prefabに、Prefabsに入れたcubeを設置する。
PrefabをInstantiateで複製する
例としてcubeを複製
- Hieraruchyに3Dオブジェクト/cubeを選択し追加
- Project(Assetsの下に)にResourcesディレクトリを作成する(名前は、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をInstantiateで複製したものを削除
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 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.name = "RObject" + angle;
rObject.transform.SetParent(GameObject.Find("/Canvas").transform);
// rObject.transform.localScale = new Vector3(152.68f, 152.68f, 152.68f); // 場合によっては追加
rObject.transform.localPosition = new Vector3(0f, 0f, - 30f);
SetParentとlocalScaleなどの順番によって表示が異なるので気をつける。基本的にはSetParentを先に追加がわかりやすいと思う。
もしくはInstantiateの第4パラメータを使う。
GameObject obj = Instantiate(prefab, position, Quaternion.identity, rouletteBackImage.transform);
