Unity Addressable:使用代码移除Group下的所有资源并保留Group本身

要移除Group下的所有资源,但保留Group本身,可以使用Addressable的API来实现。以下是一个示例代码:

using UnityEngine;using UnityEngine.AddressableAssets;using UnityEngine.ResourceManagement.AsyncOperations;public class RemoveAllResourcesInGroup : MonoBehaviour{    // 要移除资源的Group名称    public string groupName;    void Start()    {        // 加载指定Group中的所有资源        AsyncOperationHandle<IList<AsyncOperationHandle>> loadGroupOp = Addressables.LoadResourceLocationsAsync(groupName);        // 在资源加载完成后,移除所有资源        loadGroupOp.Completed += (op) =>        {            if (op.Status == AsyncOperationStatus.Succeeded)            {                foreach (AsyncOperationHandle locationOp in op.Result)                {                    // 移除资源                    Addressables.Release(locationOp);                }            }            // 释放加载Group资源的操作            Addressables.Release(loadGroupOp);        };    }}

以上代码首先使用Addressables.LoadResourceLocationsAsync()方法加载指定Group的所有资源。然后在资源加载完成后,遍历所有资源的AsyncOperationHandle,并使用Addressables.Release()方法来移除每个资源。

最后,释放加载Group资源的操作,以确保资源的清理完全完成。


原文地址: https://www.cveoy.top/t/topic/pRpl 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录