com.alicizax.unity.framework/Runtime/ABase/GameObjectPool/IResourceLoader.cs

108 lines
3.6 KiB
C#
Raw Normal View History

2026-03-26 10:49:41 +08:00
using System.Threading;
using AlicizaX.Resource.Runtime;
using Cysharp.Threading.Tasks;
using UnityEngine;
namespace AlicizaX
{
public interface IResourceLoader
{
GameObject LoadPrefab(string location);
UniTask<GameObject> LoadPrefabAsync(string location, CancellationToken cancellationToken = default);
GameObject LoadGameObject(string location, Transform parent = null);
UniTask<GameObject> LoadGameObjectAsync(string location, Transform parent = null, CancellationToken cancellationToken = default);
void UnloadAsset(GameObject gameObject);
}
public class UnityResourcesLoader : IResourceLoader
{
public GameObject LoadPrefab(string location)
{
return Resources.Load<GameObject>(location);
}
public async UniTask<GameObject> LoadPrefabAsync(string location, CancellationToken cancellationToken = default)
{
return await Resources.LoadAsync<GameObject>(location).ToUniTask(cancellationToken: cancellationToken) as GameObject;
}
public GameObject LoadGameObject(string location, Transform parent = null)
{
var prefab = Resources.Load<GameObject>(location);
if (prefab == null) return null;
var instance = GameObject.Instantiate(prefab);
if (instance != null && parent != null)
{
instance.transform.SetParent(parent);
}
return instance;
}
public async UniTask<GameObject> LoadGameObjectAsync(string location, Transform parent = null, CancellationToken cancellationToken = default)
{
var prefab = await Resources.LoadAsync<GameObject>(location).ToUniTask(cancellationToken: cancellationToken) as GameObject;
if (prefab == null) return null;
var instance = GameObject.Instantiate(prefab);
if (instance != null && parent != null)
{
instance.transform.SetParent(parent);
}
return instance;
}
public void UnloadAsset(GameObject gameObject)
{
2026-03-26 13:30:57 +08:00
// Resources.UnloadAsset cannot unload GameObjects.
// The prefab reference is nulled by the caller; Unity handles cleanup via UnloadUnusedAssets.
2026-03-26 10:49:41 +08:00
}
}
2026-03-26 13:30:57 +08:00
public class AssetBundleResourceLoader : IResourceLoader
2026-03-26 10:49:41 +08:00
{
private IResourceModule _resourceModule;
IResourceModule ResourceModule
{
get
{
if (_resourceModule == null)
{
_resourceModule = ModuleSystem.GetModule<IResourceModule>();
}
return _resourceModule;
}
}
public GameObject LoadPrefab(string location)
{
return ResourceModule.LoadAsset<GameObject>(location);
}
public async UniTask<GameObject> LoadPrefabAsync(string location, CancellationToken cancellationToken = default)
{
return await ResourceModule.LoadAssetAsync<GameObject>(location, cancellationToken);
}
public GameObject LoadGameObject(string location, Transform parent = null)
{
return ResourceModule.LoadGameObject(location, parent);
}
public async UniTask<GameObject> LoadGameObjectAsync(string location, Transform parent = null, CancellationToken cancellationToken = default)
{
return await ResourceModule.LoadGameObjectAsync(location, parent, cancellationToken);
}
public void UnloadAsset(GameObject gameObject)
{
ResourceModule.UnloadAsset(gameObject);
}
}
}