Unity-WebGL使用可寻址式资源管理器(Addressables)

一、使用PackageManager下载并安装Addressables

安装完成后Assets目录下生成AddressableAssetsData文件夹

将发布平台改为WebGL平台

二、新建组

选择Window-Asset Management-Addressables,在Addressables面板中新建组“Character”或将修改默认组

三、将Prefbs打勾

准备模型制作成Prefbs,并勾选Addressables

勾选完成后,“Character”组可见预设

四、配置并打包

修改AddressableAssetsData文件夹下的AddressableAssetSettings,

RemoteBuildPath:ServerData/[BuildTarget]

RemoteLoadPath:http://[PrivateIpAddress]:[HostingServicePort]

在Labels下新增“CharacterShow”,并将Addressables面板中“Character”组中Prefabs资源的Labels设置为“CharacterShow”

点击Addressables面板中Hosting,点击Enable Service,生成端口,随机五位数

Addressables面板中下拉Play Mode Script选择Packed Play Mode

Addressables面板中下拉Build选择Build Player Content,bundle资源将生成在Assets上级目录ServerData/WebGL

设置AddressableAssetsData/AssetGroups/Character的Use Asset Bundle Cache选项勾去掉。如果启用则在WebGL环境下在IndexedDB中缓存bundle,但第二次启动从缓存中读取bundle资源时会报错,该问题本截至2019.3.0a版本未解决。参考以下:

https://forum.unity.com/threads/webgl-wont-load-assets-after-they-have-been-cached.725261/#post-4853327

http://www.manew.com/thread-146574-1-1.html

五、编写测试脚本

在场景中新建空对象并绑定脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceLocations;

public class SelectCharacter : MonoBehaviour
{
    //bundle资源的label
    public AssetLabelReference characterLabel;
    //获得的bundle资源
    public IList<GameObject> characters;
    //当前显示的角色
    private int index = 0;
    //实例化的角色
    private List<GameObject> charactersShow = new List<GameObject>();
    //选择180度
    private Quaternion rotation = Quaternion.identity;
    //出现的坐标
    private Vector3 position = new Vector3(-1.45f, 1.2f, -4.67f);

    // Start is called before the first frame update
    void Start()
    {
        //Addressables.DownloadDependenciesAsync("CharacterShow");
        Addressables.LoadAssetsAsync<GameObject>(characterLabel, null).Completed += OnLoadDone;
        rotation.eulerAngles = new Vector3(0, 180, 0);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void ChangeCharacter()
    {
        Debug.Log(charactersShow);
        Debug.Log(characters);
        if(characters.GetType() == null)
        {
            Debug.Log("未获取到资源");
        }
        //如果可显示角色的数量还未被实例化
        if (charactersShow.Count < characters.Count)
        {
            if(index > 0)
            {
                charactersShow[index-1].SetActive(false);
            }
            charactersShow.Add(Instantiate(characters[index], position, rotation, null));
        } else
        {
            index = (index == charactersShow.Count ? 0 : index);
            charactersShow[index == 0 ? charactersShow.Count + index - 1 : index - 1].SetActive(false);
            charactersShow[index].transform.position = position;
            charactersShow[index].SetActive(true);
        }
        index++;
    }

    private void OnLoadDone(AsyncOperationHandle<IList<GameObject>> obj)
    {
        Debug.Log("加载资源完成");
        Debug.Log(obj);
        Debug.Log("获得加载资源:");
        Debug.Log(obj.Result);
        // In a production environment, you should add exception handling to catch scenarios such as a null result;
        if (obj.GetType() != null)
        {
            characters = obj.Result;
        }
    }

}

并将脚本的CharacterLabel选择为”CharacterShow“,点击运行测试。(平台不兼容会导致材质丢失,不影响WebGL实际运行时)

六、发布WebGL

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值