Addressable1.5.1应用

Addressable是可寻址资源,是一种通过地址加载资产的方法,能够简化内容包创建、部署。和更新。一旦资产被标记为可寻址,就可以从任何地方调用可寻址资源,可以通过AssetReference加载单个可寻址资源,也可以通过自定义组标签加载多个可寻址资产。

操作教程

  • 安装addressable
    Addressable1.5.1应用_第1张图片
    Addressable1.5.1应用_第2张图片

  • 将prefab标记为可寻址对象
    markaddressable
    Addressable1.5.1应用_第3张图片

  • 简化名称
    Addressable1.5.1应用_第4张图片

  • 打标签组
    Addressable1.5.1应用_第5张图片

  • Play Mode Script选择Use Existing Build(require built groups)
    Addressable1.5.1应用_第6张图片

  • Profiles
    Addressable1.5.1应用_第7张图片
    profilepath

  • Build
    Addressable1.5.1应用_第8张图片

  • 载入代码

    var operateHandler = Addressables.LoadAssetsAsync(Label, OnLoadComplete);
    

    完整代码:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.AddressableAssets;
    using UnityEngine.ResourceManagement.AsyncOperations;
    using UnityEngine.UI;
    
    public class CharacterManager : MonoBehaviour
    {
        public string Label="default";
        public Transform LoadingPanelTrans;
        LoadingPanel loadingPanel;
        bool m_AssetsReady = false;
        public Transform ButtonPanelTrans;
        public Dictionary btnDic;
        public Dictionary objDic;
        public Transform spawnParent;
        private void Awake()
        {
            btnDic = new Dictionary();
            objDic = new Dictionary();
            for (int i=0;i< ButtonPanelTrans.childCount; i++)
            {
                Transform child = ButtonPanelTrans.GetChild(i);
                if (!btnDic.ContainsKey(child.name))
                {
                    Button btn = child.GetComponent

    用这种方式载入通过operateHander.PercentComplete可以获取下载进度,还有另一种载入的方式:

    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.AddressableAssets; //TODO: Mention the use of this namespace
    using UnityEngine.ResourceManagement.AsyncOperations; // TODO: Mention that this is needed to do the async operations over the lists?
    
    public class CharacterManager : MonoBehaviour
    {
        public GameObject m_archerObject;
    
        public AssetReference m_ArcherObject;
    
        public List m_Characters;
        bool m_AssetsReady = false;
        int m_ToLoadCount;
        int m_CharacterIndex = 0;
    
        Start is called before the first frame update
        void Start()
        {
            m_ToLoadCount = m_Characters.Count;
    
            foreach (var character in m_Characters)
            {
               character.LoadAssetAsync().Completed += OnCharacterAssetLoaded;
            }
        }
    
    
        public void SpawnCharacter(int characterType)
        {
            Instantiate(m_archerObject);
    
            m_ArcherObject.InstantiateAsync();
    
            if (m_AssetsReady)
            {
               Vector3 position = Random.insideUnitSphere * 5;
               position.Set(position.x, 0, position.z);
               m_Characters[characterType].InstantiateAsync(position, Quaternion.identity);
            }
        }
    
        void OnCharacterAssetLoaded(AsyncOperationHandle obj)
        {
           m_ToLoadCount--;
    
           if (m_ToLoadCount <= 0)
               m_AssetsReady = true;
        }
    
        private void OnDestroy() //TODO: Should we teach instantiate with game objects and then manually release?
        {
           foreach (var character in m_Characters)
           {
               character.ReleaseAsset();
           }
        }
    }
    
    

    这种方式也可以载入资源,但是打出apk会显示获取进度一直卡在50%(进度显示有问题,但是不影响下载)

  • 打完包后的路径位于ServerData对应打包的平台目录下。本例是这个目录:ServerData\Android

  • 将ServerData目录底下的文件上传服务器即可,本例服务器路径如下:
    Addressable1.5.1应用_第9张图片
    自己配置远程服务器需要修改Profiles中对应远程服务器的地址,重新打包并上传自己的服务器

  • 更新资源:
    update

  • 会弹出一个让你选的框
    Addressable1.5.1应用_第10张图片

  • 打包apk
    Addressable1.5.1应用_第11张图片

注意

在android资源打包addressable之后在Editor中运行呈现粉色,但实际打包app没有什么问题
apk下载地址

源码示例

gitee url

你可能感兴趣的:(Unity)