unity中对Addressable进行资源加载封装

using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using System;
using System.Collections.Generic;
using System.Collections;
using Unity.VisualScripting;
using UnityEngine.Serialization;

public class AssetLoader : UnitySingleton<AssetLoader>
{

   public Dictionary<string, Stack<AsyncOperationHandle>> _assetHandles = new Dictionary<string, Stack<AsyncOperationHandle>>();
    public void Awake()
    {

    }

    // 加载资源的方法
    public void LoadAsset<T>(string assetKey, Action<T> onAssetLoaded = null)
    {
        var handle = Addressables.LoadAssetAsync<T>(assetKey);

        handle.Completed += (operationHandle) =>
        {
            if (operationHandle.Status == AsyncOperationStatus.Succeeded)
            {
                _assetHandles.TryGetValue(assetKey, out var handles);
                if (handles == null)
                {
                    handles = new Stack<AsyncOperationHandle>();
                    _assetHandles.Add(assetKey, handles);
                }

                handles.Push(operationHandle);
                onAssetLoaded?.Invoke(operationHandle.Result);
            }
            else
            {
                Debug.LogError("Failed to load asset: " + operationHandle.DebugName);
            }

            // Ensure the handle is removed from the dictionary even if an exception occurred.

        };
    }
    public AsyncOperationHandle LoadAssetReturnHandle<T>(string assetKey, Action<T> onAssetLoaded)
    {
        var handle = Addressables.LoadAssetAsync<T>(assetKey);

        handle.Completed += (operationHandle) =>
        {
            if (operationHandle.Status == AsyncOperationStatus.Succeeded)
            {
                onAssetLoaded?.Invoke(operationHandle.Result);
            }
            else
            {
                Debug.LogError("Failed to load asset: " + operationHandle.OperationException);
            }

            // Ensure the handle is removed from the dictionary even if an exception occurred.
        };
        return handle;
    }

    // 释放指定的单个资源
    public void ReleaseAsset(string assetKey)
    {
        _assetHandles.TryGetValue(assetKey, out var handles);
        if (handles == null)
            return;
        if (handles.Count > 0)
        {
            var handle = handles.Pop();
            if (handle.IsValid())
            {
                ReleaseAsset(handle);
                _assetHandles.Remove(assetKey);
            }
        }
    }
    
    public void ReleaseAsset(AsyncOperationHandle handle)
    {
        Addressables.Release(handle);
    }

    // 释放所有加载的资源
    public void ReleaseAllAssets()
    {

    }

    // 在 MonoBehaviour 的 OnDisable 或 OnDestroy 方法中释放资源
    void OnDisable()
    {
        ReleaseAllAssets();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值