unity 多個預製體的時候,隨機生成某個預製物體

2022-09-03 21:57:11 字數 825 閱讀 3029

方法1:

//建立乙個陣列,在介面那裡把預製物體拖進fishprefab裡

public gameobject fishprefab;

//計時器

float timer = 0;

void update()

方法2//在project面板建乙個resources資料夾,注意:名字不要寫錯,大小寫完也是

//把預製體全部放到這個資料夾裡,然後給預製體改名,名字後面**編號,例:fish0,fish1,fish2.....

void update(){

timer += time.deltatime;

if (timer>=2)

{timer = 0;

//找到預製體

gameobject fishprefab= resources.load("fish"+random.range(0,3));

//隨機預製體的位置

vector3 poiont= camera.main.viewporttoworldpoint(new vector3(random.value,random.value,-camera.main.transform.position.z));

//生成預製體

gameobject fish= instantiate(fishprefab,poiont,fishprefab.transform.rotation)as gameobject;

//五秒銷毀預製體

destroy(fish,5);

Unity小功能分享 預製體庫

在平時開發的時候經常會用到預製體,還會結合resources在執行時載入預製體,不過使用resources的方式會把所有放在resources目錄下的東西都打包到build的程式包中,這樣就會把測試的內容也打包進去,而且resources資料夾可以在assets的任意子目錄中不方便管理,這並不是我們...

Unity 中 Prefab(預製體)的建立工具

很多時候會用到 prefab,因為這個的確使用方便,廢話不多說,直接上 放置在editor目錄中 using unityengine using unityeditor using system using unityeditor.scenemanagement using system.io na...

Unity2 學習 製作和動態載入預製體

1.製作預製體 把要製做預製體的物體home托到resources資料夾中,然後刪除hierarchy中的物體home即可。注意為了保持比例一致,設定好後,home在託到resources中時不能是其它物體的子物體。2,在程式執行時通過 例項化出預製體home,把乙個物體home做為另乙個物體b的子...