赞
踩
Unity适配小游戏的文档,一般自己看一遍就会了(bushi)。 微信小游戏适配方案
往下翻找到下载Unity插件,点击下载
工具栏打开Window-Packages Manager
Packages 选择 Unity Registry找到Addressables
install安装

切换Build Setting到WebGl
随便搞点资源(xml, png, prefab,scene等…)
点击Window - Asset Management - Groups打开Addressables Group面板
点击Create 新建Addressables Settings
3. 新建之后Addressable Groups面板会新增Default Local Group (Default)分组,Addressables 默认是按Group组进行AssetBundle打包的,将资源放在一个Group组里,那么这些会被打在同一个Group组中
右键面板或者点击左上角的Create按钮可以创建新的分组
**Assets目录下会新增AddressableAssetsData文件夹**

其中Default Local Group是默认的Group组设置文件,自己新建的分组(MyAssets)设置文件也在相同的位置
1. 直接拖
2. 点击资源,勾选Inspector面板下的Addressable,Addressable后面的地址就是资源地址。加载的时候可以根据这个地址直接搜索到资源。
3. Group设置
点击AddressableAssetsData目录下的Default Local Group文件,打开Default Local Group的设置面板
Build Path: 资源包创建的位置
Load Path: 资源包加载的位置
**LocalBuildPath**资源打包的位置在Library\com.unity.addressables\aa\WebGL下
**RemoteBuildPath** 资源打包位置在ServerData\WebGL下
标签Labels设置
点击红色箭头位置,打开标签选项,点击Manage Labels打开标签设置,新建自己的标签
然后给资源细分标签
static AsyncOperationHandle<Sprite> spHandle; // 地址 string keys = "Assets/Resouces/cpy.png"; // Start is called before the first frame update void Start() { StartCoroutine(loadSprite(keys)); } IEnumerator loadSprite(string kety) { spHandle = Addressables.LoadAssetAsync<Sprite>(kety); while (!spHandle.IsDone) { Debug.Log($"加载中{spHandle.PercentComplete.ToString()}"); yield return null; } onSPLoaded(spHandle.Result); } void onSPLoaded(Sprite sp) { transform.GetComponent<Image>().sprite = sp; }
运行结果
static AsyncOperationHandle<GameObject> goHandle; // 地址 string keys = "Assets/Prefabs/Cube.prefab"; // Start is called before the first frame update void Start() { StartCoroutine(loadSprite(keys)); } IEnumerator loadSprite(string kety) { goHandle = Addressables.LoadAssetAsync<GameObject>(kety); while (!goHandle.IsDone) { Debug.Log($"加载中{goHandle.PercentComplete.ToString()}"); yield return null; } onOBLoaded(goHandle.Result); } void onOBLoaded(GameObject sp) { GameObject ob = Instantiate(sp); }
static AsyncOperationHandle<IList<Sprite>> spHandle; static string iconLabels = "icon"; // Start is called before the first frame update void Start() { StartCoroutine(loadAllSprite(iconLabels)); } IEnumerator loadAllSprite(string key) { spHandle = Addressables.LoadAssetsAsync<Sprite>(key, onSpLoaded); yield return null; } void onSpLoaded(Sprite sp) { Debug.Log($"icon name : {sp.name.ToString()}"); }
运行结果如下
static AsyncOperationHandle<SceneInstance> sceneHandle; static string sceneKey = "Assets/Scenes/TestScene.unity"; SceneInstance scene; // Start is called before the first frame update void Start() { StartCoroutine(loadScene(sceneKey)); } IEnumerator loadScene(string key) { // 保留原场景,不保留Addressables.LoadSceneAsync(key, LoadSceneMode.Single, activateOnLoad: true) // 加载模式LoadSceneMode 激活activateOnLoad sceneHandle = Addressables.LoadSceneAsync(key, LoadSceneMode.Additive, activateOnLoad: false); while (!sceneHandle.IsDone) { Debug.Log($"load pcs : {sceneHandle.PercentComplete.ToString()}"); yield return null; } scene = sceneHandle.Result; onSceneLoaded(); } void onSceneLoaded() { scene.ActivateAsync(); }
运行结果
LoadAssetsAsync的三个重载:
// 用于加载单个标签或地址
static AsyncOperationHandle<IList<T>> LoadAssetsAsync<T>(object key, Action<T> callback);
// 用于加载地址+标签, MergeMode 为找到的资源合并模式
static AsyncOperationHandle<IList<T>> LoadAssetsAsync<T>(IList<object> keys, Action<T> callback, MergeMode mode);
static AsyncOperationHandle<IList<T>> LoadAssetsAsync<T>(IList<IResourceLocation> locations, Action<T> callback);
卸载资源
Addressables.Release
导入完成后,菜单栏会有微信小游戏按钮,点击转换小游戏打开工具面板
Color Space 选 Gamma,不然无法打包
Auto Graphics API取消勾选
Graphics API只保留WebGL 1.0 或者WebGL 2.0,不然包体会过大
Lightmap Encoding 选Normal Quality,不然资源会很大
启动背景图
可以修改封面为自己的Logo
加载阶段视频URL
更改加载阶段的视频
首包加载方式
更改首包的加载方式
其他可以自己测试
勾选 Clear Streaming Assets
打开转换出来的文件夹,打开webgl文件夹
StreamingAssets(资源文件夹)
.webgl.data.unityweb.bin.txt(资源信息)
index.html
三个文件放到远程CDN上(剩下的小游戏不需要)
打开game.js文件,F找到"DATA_CDN"修改后面的地址即可
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。