Unity Addressable内存管理

先看一个真机测试实例:

两个物体放在同一个Group里面

只创建Cube时,内存资源中只有Cube的材质球和贴图

同时创建了Cube和Sphere,内存资源中拥有两个材质球和贴图

此时只删除Cube,Cube的材质球和贴图不会被释放,当然场景内存(SceneMemory)肯定是释放了的

只有把Sphere也释放掉才会同时将两个材质球和贴图都从资源内存中释放。

所以一个Group中的资源不会一起加载进资源,但是只要加载了进来资源,只有该Group全部引用都被删除之后,

资源才会被一起释放掉

测试代码:


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

public class testaa : MonoBehaviour
{
    private List<GameObject> Cubelist = new List<GameObject>();
    private List<GameObject> Spherelist = new List<GameObject>();

    public void CreateCube()
    {
        Addressables.InstantiateAsync("Cube").Completed += delegate(AsyncOperationHandle<GameObject> handle)
        {  
            Cubelist.Add(handle.Result);
        };
    }

    public void CreateSphere()
    {
        Addressables.InstantiateAsync("Sphere").Completed += delegate(AsyncOperationHandle<GameObject> handle)
        {  
            Spherelist.Add(handle.Result);
        };
    }

    public void ClearCube()
    {
        for (int i = 0; i < Cubelist.Count; i++)
        {
            Addressables.Release(Cubelist[i]);
        }
        Cubelist.Clear();
    }
    
    public void ClearSphere()
    {
        for (int i = 0; i < Spherelist.Count; i++)
        {
            Addressables.Release(Spherelist[i]);
        }
        Spherelist.Clear();
    }
}

 

下面是官方文档对Addressable内存的讲解,自己根据理解翻译了一下。

Mirroring load and unload

When working with Addressable Assets, the primary way to ensure proper memory management is to mirror your load and unload calls correctly. How you do so depends on your asset types and load methods. In all cases, however, the release method can either take the loaded asset, or an operation handle returned by the load. For example, during Scene creation (described below) the load returns a AsyncOperationHandle<SceneInstance>, which you can release via this returned handle, or by keeping up with the handle.Result (in this case, a SceneInstance).

 

镜像加载和卸载(有加载就要有卸载)

在使用Addressable时,正确的内存管理主要方法是成对儿地调用加载和卸载。如何这样做取决于您的资源类型和加载方法。但是,在所有情况下,release方法接受的参数既可以是加载好的资源,也可以是加载资源时的句柄。例如,在场景创建期间(如下所述),加载返回一个AsyncOperationHandle<SceneInstance>,您可以通过这个返回的句柄来释放它,或者通过这个句柄包含的实例(handle.Result) 释放它(在本例中是一个SceneInstance)。

 

Asset loading

To load an asset, use Addressables.LoadAssetAsync or Addressables.LoadAssetsAsync.

 

This loads the asset into memory without instantiating it. Every time the load call executes, it adds one to the ref-count for each asset loaded. If you call LoadAssetAsync three times with the same address, you will get three different instances of an AsyncOperationHandle struct back, all referencing the same underlying operation. That operation has a ref-count of three for the corresponding asset. If the load succeeds, the resulting Asyn

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值