文章

顯示從 10月, 2021 起發佈的文章

Unity StreamingAssets文件加載的注意事項

 當我們從StreamingAssets中加載資源時,需要注意的一點是,Windows平台與macOS平台的資源路徑格式並不相同,比如在macOS中使用StreamingAssets路徑時,需要注意在路徑前加入“file://”,否則Unity將不會正常加載資源(Windows平台不需要,但加入 "file://" 後不會影響Windows平台正常訪問該路徑)。 string. Format ( "{0}/{1}" ,   Application . streamingAssetsPath , filePath ) string. Format ( " file:// {0} / {1}" ,  Application . streamingAssetsPath ,  filePath ) OR $ "file://{ Application . streamingAssetsPath }/{ filePath }"  (Unity官方在文檔中並沒有提到這種格式差異。 )

Unity PlayerPrefs 功能拓展

圖片
 Unity版本:2020.3.12f1 Unity自帶的PlayerPrefs默認情況下並不支持本地持久化除String、Int、Float之外的數據類型,如果我們需要通過PlayerPrefs保存其它的任何數據類型,可以通過新建一個自定類並繼承PlayerPrefs來對其進行功能拓展。 這裡就以Bool和Color爲例進行拓展。 這些拓展方法本質上還是基於了它原本自帶String、Int、Float的Get/Set,例如,可以將Bool轉換爲Int整型(false爲0,true爲1)並使用SetInt進行保存。 由於此Class繼承了PlayerPrefs,因此調用SetInt方法時可以省略“PlayerPrefs.” 拓展Bool 拓展Color方法與Bool思路類似,只是將Color轉換成Color的Hex十六進制值並使用SetString進行持久化保存。 拓展Color 使用方法與GetSetFloat/Int/String等自帶方法相同。 示例: 使用方法示例 注意:這裡應當調用PlayerPrefsEX類,而不是自帶的PlayerPrefs。 參考資料 【Unity官方文檔】PlayerPrefs

Unity Mesh Static Batching 網格靜態合批(批處理)

圖片
Unity版本:2018.3.5f1 勾選Auto Combine On Start後此腳本將在初始化(Awake)時將Batch Objects中的Mesh網格進行合併。 腳本程式碼 腳本Inspector 參考資料 【Unity官方文檔】 StaticBatchingUtility .Combine