【Unity】Unity项目转抖音小游戏(三)资源分包,抖音云CDN

业务需求,开始接触一下抖音小游戏相关的内容,开发过程中记录一下流程。

使用资源分包可以优化游戏启动速度,是抖音小游戏推荐的一种方式,抖音云也提供存放资源的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的异步资源加载代码,在回调中处理资源使用

一些参考代码:

csharp 复制代码
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】当中。

完成操作

相关推荐
开发游戏的老王1 天前
虚幻引擎虚拟制片入门教程 之 Sequencer 常用技巧
游戏引擎·虚幻
开发游戏的老王2 天前
虚幻引擎入门教程:虚幻编辑器的基本操作
编辑器·游戏引擎·虚幻
AA陈超2 天前
虚幻引擎5 GAS开发俯视角RPG游戏 P05-08 UI 部件数据表
c++·游戏·ue5·游戏引擎·虚幻
future_studio2 天前
聊聊 Unity(小白专享、C# 小程序 之 播放器)
unity·小程序·c#
向宇it2 天前
【unity实战】MapMagic 2实战例子
游戏·3d·unity·c#·游戏引擎
SlowFeather2 天前
Unity TMP可控角度多色渐变文字
unity·游戏引擎
霜绛2 天前
Unity:UGUI笔记(一)——三大基础控件、组合控件
笔记·学习·unity·游戏引擎
小趴菜82272 天前
Android中加载unity aar包实现方案
android·unity·游戏引擎
今夕资源网3 天前
牛童三国单机游戏Unity源码 免费开源
游戏·unity·单机游戏·游戏源码·unity源码·unity游戏
future_studio3 天前
聊聊 Unity(小白专享、C# 小程序 之 图片播放器)
unity·小程序·c#