unity動態創建對象,需要使用預設prefab,以下方法可以動態創建。
首先需要在Hierarchy視圖里創建GameObject,然后創建Prefab。
方法一:
public GameObject instantiate;
GameObject go=Instantiate(instantiate,transform.position,transform.rotation) as GameObject;
將做好的prefab拖到Inspector界面中的 instantiate 字段中,獲得原型對象。
方法二:
在Assets目錄下的任意位置創建一個名為resources的文件夾,將做好的prefab放到這個文件夾下,path形式如下:
Assets\....\resources\prefabName.prefab
在代碼里使用Resource.Load 或 LoadAll 函數,獲得原型對象。
GameObject go= Resource.Load("prefabName") as GameObject;
