using System;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
namespace AibisDream.Framework
{
///
/// 旧版资源加载工具类。新代码请使用 ResourceSystem / SceneResourceLoader。
/// 保留供过渡期使用,后续迁移完成后将移除。
///
public static class ResourceKit
{
public static T LoadAssetSync(string key) where T : UnityEngine.Object
{
var operation = Addressables.LoadAssetAsync(key);
operation.WaitForCompletion();
if (operation.Status == AsyncOperationStatus.Succeeded)
{
return operation.Result;
}
else
{
Debug.Log($"资源{key}不存在");
return default;
}
}
public static void LoadAssetAsync(string key, Action callback) where T : UnityEngine.Object
{
Addressables.LoadAssetAsync(key).Completed += operation =>
{
if (operation.Status == AsyncOperationStatus.Succeeded)
{
callback(operation.Result);
}
else
{
Debug.Log($"资源{key}不存在");
}
};
}
///
/// 异步加载 Addressable 资源,callback 接收完整 handle。调用方需在适当时机调用 Release 释放。
///
public static void LoadAssetAsyncWithHandle(string key, Action> callback) where T : UnityEngine.Object
{
var handle = Addressables.LoadAssetAsync(key);
handle.Completed += operation =>
{
if (operation.Status != AsyncOperationStatus.Succeeded)
{
Debug.Log($"资源{key}不存在");
}
callback(operation);
};
}
///
/// 释放 Addressable 加载的 handle
///
public static void Release(AsyncOperationHandle handle)
{
if (handle.IsValid())
Addressables.Release(handle);
}
}
}