Tips

文档

Unity适配小游戏的文档,一般自己看一遍就会了(bushi)。

  1. 下载完备用

安装Addressables包

  1. 先搞基本操作,详细以后在补充
  2. 新建小游戏项目 unity版本 2020.3.46
加载Addressable包
工具栏打开Window-Packages Manager
Packages 选择 Unity Registry找到Addressables
install安装
![在这里插入图片描述]()
  1. 切换Build Setting到WebGl
  2. Unity发布微信小游戏 unity可以做微信小游戏_游戏

  3. 随便搞点资源(xml, png, prefab,scene等…)
  4. Unity发布微信小游戏 unity可以做微信小游戏_Unity发布微信小游戏_02

使用Addressables处理资源

点击Window - Asset Management - Groups打开Addressables Group面板

Unity发布微信小游戏 unity可以做微信小游戏_加载_03

点击Create 新建Addressables Settings

Unity发布微信小游戏 unity可以做微信小游戏_Unity发布微信小游戏_04


3. 新建之后Addressable Groups面板会新增Default Local Group (Default)分组,Addressables 默认是按Group组进行AssetBundle打包的,将资源放在一个Group组里,那么这些会被打在同一个Group组中

右键面板或者点击左上角的Create按钮可以创建新的分组

Unity发布微信小游戏 unity可以做微信小游戏_游戏_05


Unity发布微信小游戏 unity可以做微信小游戏_微信小程序_06

**Assets目录下会新增AddressableAssetsData文件夹**
![在这里插入图片描述]()

其中Default Local Group是默认的Group组设置文件,自己新建的分组(MyAssets)设置文件也在相同的位置
添加资源
1. 直接拖

Unity发布微信小游戏 unity可以做微信小游戏_加载_07


2. 点击资源,勾选Inspector面板下的Addressable,Addressable后面的地址就是资源地址。加载的时候可以根据这个地址直接搜索到资源。

Unity发布微信小游戏 unity可以做微信小游戏_unity_08


3. Group设置

点击AddressableAssetsData目录下的Default Local Group文件,打开Default Local Group的设置面板

Unity发布微信小游戏 unity可以做微信小游戏_加载_09

Build Path: 资源包创建的位置
	Load Path: 资源包加载的位置
	**LocalBuildPath**资源打包的位置在Library\com.unity.addressables\aa\WebGL下
	**RemoteBuildPath** 资源打包位置在ServerData\WebGL下
  1. 标签Labels设置
    点击红色箭头位置,打开标签选项,点击Manage Labels打开标签设置,新建自己的标签

    然后给资源细分标签
  1. 打包资源
    点击AddressableAssetsData目录下的AddressableAssetSettings文件

    点击Manage Groups打开Groups管理界面,点击Build-New Build- Default Build Script开始创建

    创建完成

Addressable加载资源

搞代码
根据地址加载图片

Unity发布微信小游戏 unity可以做微信小游戏_微信小程序_10

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;
    }
运行结果

Unity发布微信小游戏 unity可以做微信小游戏_游戏_11

加载预设
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);
    }

Unity发布微信小游戏 unity可以做微信小游戏_unity_12

按标签批量加载

Unity发布微信小游戏 unity可以做微信小游戏_加载_13

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()}");
    }

运行结果如下

Unity发布微信小游戏 unity可以做微信小游戏_Unity发布微信小游戏_14

加载场景
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();
    }

运行结果

Unity发布微信小游戏 unity可以做微信小游戏_加载_15

API战士
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

  1. 从内存中卸载单独的资源
  2. 释放handle
文件预加载

转微信小游戏

导入最开始下载的插件

导入完成后,菜单栏会有微信小游戏按钮,点击转换小游戏打开工具面板

Unity发布微信小游戏 unity可以做微信小游戏_游戏_16

修改Player Setting

Unity发布微信小游戏 unity可以做微信小游戏_Unity发布微信小游戏_17


Color Space 选 Gamma,不然无法打包

Auto Graphics API取消勾选

Graphics API只保留WebGL 1.0 或者WebGL 2.0,不然包体会过大

Lightmap Encoding 选Normal Quality,不然资源会很大

工具面板设置

Unity发布微信小游戏 unity可以做微信小游戏_游戏_18

基本信息
  1. 游戏APPID,自行到微信小程序中心申请。或者在微信开发者工具中拿一个测试号
  2. 游戏资源CDN,存放游戏资源的CDN地址。可以不写,晚点在项目中更改。
  3. 倒出路径,微信小游戏导出的位置
启动Loading设置
  1. 启动背景图
    可以修改封面为自己的Logo
  2. Unity发布微信小游戏 unity可以做微信小游戏_游戏_19

  3. 加载阶段视频URL
    更改加载阶段的视频
  4. 首包加载方式
    更改首包的加载方式
  5. 其他可以自己测试
调试编译选项

勾选 Clear Streaming Assets

微信调试
资源

打开转换出来的文件夹,打开webgl文件夹

StreamingAssets(资源文件夹)

.webgl.data.unityweb.bin.txt(资源信息)

index.html

三个文件放到远程CDN上(剩下的小游戏不需要)

Unity发布微信小游戏 unity可以做微信小游戏_unity_20

修改CDN地址

打开game.js文件,F找到"DATA_CDN"修改后面的地址即可

Unity发布微信小游戏 unity可以做微信小游戏_unity_21

用微信开发者工具打开项目

Unity发布微信小游戏 unity可以做微信小游戏_微信小程序_22

其他问题
  1. 中文不显示
    将Unity默认字体全部更改为新的字体
  2. 打开时报编译出错,找不到game.json文件
    这种一般删除源文件重新设置APPID,然后导入即可
    或者找到project.config.json文件,在APPID上面一行添加
    “miniprogramRoot”: “minigame/”,