[U3D]资源加载的解决方案-Addressables(可寻址资产系统)

之前大家使用热更的时候都使用的是unity3d自带的assetbundle(AB)包,但是说实话对于使用来说还是存在着非常大的问题的

  1. 打包需要通过写代码
  2. 依赖关系,比较麻烦
    现在有一个特别好的东西可以解决unity之前AB包的一个问题

正常步骤

  1. 资源设置为addressable

  2. 设置资源对应的路径

  3. 设置生成和加载的路径

    在这里插入图片描述

  4. 设置运行模式

    在这里插入图片描述

  5. 把我们标记为address的生成出来.

    在这里插入图片描述

  6. 加载这个资源

    using UnityEngine;
    using UnityEngine.AddressableAssets;
    
    public class samplesuse : MonoBehaviour
    {
          
          
        // Start is called before the first frame update
        void Start()
        {
          
          
            LoadAE();
        }
    
        public void LoadAE()
        {
          
          
            Addressables.LoadAssetAsync<GameObject>("Assets/Cube.prefab").Completed += LoadCubeCompleted;
        }
    
        private void LoadCubeCompleted(UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle<GameObject> obj)
        {
          
          
            Addressables.InstantiateAsync("Assets/Cube.prefab");
            Instantiate(obj.Result);
        }
    }
    

四个样本

Addressables Utility

加载引用资源

Disable AssetImport on Build

build不允许使用引入资源

Import Groups Tool

引入资源
在这里插入图片描述

Prefab Spawner

预制体孵化器,可以直接孵化addressable的预制体,并且可以调整生成时间,存活时间,等参数.

热更例子

是否可以跨项目加载

可以加载gameobject,但是脚本无法加载

在这里插入图片描述

代码

using UnityEngine;
using UnityEngine.AddressableAssets;

public class loadotherae : MonoBehaviour
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        string url = @"E:\WorkStations\Unity\addressenble\Library\com.unity.addressables\aa\Windows\catalog.json";
        Addressables.LoadContentCatalogAsync(url).Completed += (arg) =>
        {
    
    
            Addressables.LoadAssetAsync<GameObject>("Assets/Cube.prefab").Completed += LoadCubeCompleted;
        };
    }

    private void LoadCubeCompleted(UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle<GameObject> obj)
    {
    
    
        Addressables.InstantiateAsync("Assets/Cube.prefab");
        Instantiate(obj.Result);
    }
}

https://docs.unity.cn/Packages/[email protected]/manual/MultiProject.html

猜你喜欢

转载自blog.csdn.net/JianShengShuaiest/article/details/108087650