Unity小功能分享 預製體庫

2021-10-12 01:45:44 字數 2103 閱讀 5015

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

預製體庫繼承自scriptableobject,按本人理解scriptableobject就像個配置檔案,可以被指令碼引用使用。它可以替代resources達到相同的目的。

using system.collections.generic;

using unityengine;

using sirenix.odininspector;

#if unity_editor

using unityeditor;

#endif

namespace zyf

[serializefield

,header

("預製體列表")]

private list prefablist =

newlist

<

prefabinfo

>()

;private dictionary<

string

, prefabinfo> _dic;

public dictionary<

string

, prefabinfo> dic

}return _dic;}}

///

/// 獲得對應key的預製體

///

///

public t getprefab

<

t>

(string key)

where t : object

else

的期望型別為:(),但找到的預製體不匹配"

, info.prefab);}

}else

丟失",

this);

}}else",

this);

}return

null;}

#if unity_editor

[menuitem

("zyf/scriptableobjects/prefabdic")]

public

static

void

addmenu()

#endif

///

/// 例項化scriptableobject到project中

///

///

public

static

void

spawnscriptableobject2project

<

t>

(string rename)

where t : scriptableobject

.asset"

; unityeditor.assetdatabase.

createasset

(config, path)

; unityeditor.selection.activeobject = config;

#endif}}}

1.生成配置

addmenu()會自動在選單欄新增乙個按鈕用來生成乙個新的配置到assets資料夾中。

2.引用生成的配置呼叫getprefab即可。

unity 小功能實現(碰撞檢測)

1.觸發器trigger 勾選istrigger屬性 當player剛進入觸發區域的時刻發生觸發檢測,比如在靠近門的某個區域門一直處於開著狀態 void ontriggerenter collider other 當player在觸發區域的時間內發生觸發檢測,比如進入危險地帶會觸發警報在 void ...

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的子...