5.objectのPrefab化
ここでは
オブジェクトの
Prefab化について
解説していきます。
Prefab化とは
所謂テンプレート化です。
たくさん生成したい
- 敵
- 弾
などに有効です。
やり方は至って簡単で
オブジェクトをドラッグ&ドロップするだけです。
下記のようになっていれば、
成功しています。
6.Prefabの生成をスクリプトで行う方法
Prefab化したオブジェクトをコードで管理するのは
- 弾を撃ち出すとき(生成)
ができるので必須のテクニックです。
まずはCreateEmptyで
オブジェクトを一つ作成してください。
(GameContoroller)
その後、Cubeも作成してください。
その後にScript(GameContoroller)を
作成していきましょう
ここまでの作業が済んだら、
- CubeをPrefab化
- ScriptをGameContorollerにアタッチ
してください。
Scriptに下記のコードを記述して下さい
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class GameController : MonoBehaviour | |
{ | |
//cubeの入れ物を用意 | |
public GameObject cube; | |
//ゲーム開始時 | |
void Start() | |
{ //cubeを生成 | |
Instantiate(cube); | |
} | |
} |
記述が終了したら、
GameContorollerを選択して
PrefabのCubeを
ドラッグ&ドロップしましょう
これでPlayボタンを押せば
Cubeが生成されるはずです!