業務需求,開始接觸一下抖音小游戲相關的內容,開發過程中記錄一下流程。
使用資源分包可以優化游戲啟動速度,是抖音小游戲推薦的一種方式,抖音云也提供存放資源的CDN服務
抖音云官方文檔:https://developer.open-douyin.com/docs/resource/zh-CN/developer/tools/cloud/develop-guide/cloud-function-debug
1.開通云環境:
詳見這個文章:【Unity】Unity項目轉抖音小游戲(二)云數據庫和云函數
2.使用Addressables插件
1.packamagager安裝addressables
2.對資源進行分組
【Windonw】-【Asset Management】-【Addressables】-【Groups】打開對應窗口
3.新建組并把所需資源放進去
4.修改資源加載代碼
使用Addressables的異步資源加載代碼,在回調中處理資源使用
一些參考代碼:
public void Load<T>(string key, Action<T> callback, bool autoRelease) where T : Object{if (isLoad){if (handle.Status == AsyncOperationStatus.Succeeded){callback?.Invoke(handle.Result as T);if (autoRelease){Release();}return;}}isLoad = true;handle = Addressables.LoadAssetAsync<T>(key);handle.Completed += (p) =>{if (p.Status == AsyncOperationStatus.Succeeded){callback?.Invoke(p.Result as T);if (autoRelease){Release();}}};}public void LoadAll<T>(string key, Action<IList<T>> callback, bool autoRelease) where T : Object{if (isLoad){if (handle.Status == AsyncOperationStatus.Succeeded){callback?.Invoke(handle.Result as IList<T>);if (autoRelease){Release();}return;}}isLoad = true;handle = Addressables.LoadAssetsAsync<T>(key, null);handle.Completed += (p) =>{if (p.Status == AsyncOperationStatus.Succeeded){callback?.Invoke(p.Result as IList<T>);if (autoRelease){Release();}}};}public void Release(){if (isLoad){isLoad = false;Addressables.Release(handle);}}}
3.使用CDN加載資源
1.開通CDN
進入抖音云后臺【組件中心】-【對象存儲】-【開通】開通CDN服務
2.開通之后上傳一個文件到抖音云,并點擊查看詳情獲取CDN地址
3.配置Addressables的加載地址
再次打開Addressables的資源界面
選中一個組,把資源的BuildPath和LocalPath設置成【Remoe】遠程加載
【Windonw】-【Asset Management】-【Addressables】-【Profiles】打開對應窗口
輸入路徑:
Remote BuildPath是你輸出打包資源的路徑,一般是版本+[BuildTarget]
Remote LoadPath是你資源包加載的路徑,一般是CDN路徑+版本+[BuildTarget]
這里添加版本號信息便于版本管理和區分。
回到Groups窗口,點擊【Build】-【New Build】-【Default Build Script】來進行打包資源,打出來的資源會在你上面配置的【Remote BuildPath】當中
回到抖音云平臺,把新的資源上傳到【Remote LoadPath】當中。
完成操作