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 IResourceService _resourceService;
2026-03-26 10:49:41 +08:00
IResourceService ResourceService
2026-03-26 10:49:41 +08:00
{
get
{
if (_resourceService == null)
2026-03-26 10:49:41 +08:00
{
_resourceService = AppServices.Require<IResourceService>();
2026-03-26 10:49:41 +08:00
}
return _resourceService;
2026-03-26 10:49:41 +08:00
}
}
public GameObject LoadPrefab(string location)
{
return ResourceService.LoadAsset<GameObject>(location);
2026-03-26 10:49:41 +08:00
}
public async UniTask<GameObject> LoadPrefabAsync(string location, CancellationToken cancellationToken = default)
{
return await ResourceService.LoadAssetAsync<GameObject>(location, cancellationToken);
2026-03-26 10:49:41 +08:00
}
public GameObject LoadGameObject(string location, Transform parent = null)
{
return ResourceService.LoadGameObject(location, parent);
2026-03-26 10:49:41 +08:00
}
public async UniTask<GameObject> LoadGameObjectAsync(string location, Transform parent = null, CancellationToken cancellationToken = default)
{
return await ResourceService.LoadGameObjectAsync(location, parent, cancellationToken);
2026-03-26 10:49:41 +08:00
}
public void UnloadAsset(GameObject gameObject)
{
ResourceService.UnloadAsset(gameObject);
2026-03-26 10:49:41 +08:00
}
}
}