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
|
|
|
|
|
{
|
2026-03-31 17:25:20 +08:00
|
|
|
private static bool AllowLegacyResourcesFallback => Application.isEditor || Debug.isDebugBuild;
|
|
|
|
|
|
2026-03-26 10:49:41 +08:00
|
|
|
public GameObject LoadPrefab(string location)
|
|
|
|
|
{
|
2026-03-31 17:25:20 +08:00
|
|
|
if (TryGetResourceService(out var resourceService))
|
|
|
|
|
{
|
|
|
|
|
var prefab = resourceService.LoadAsset<GameObject>(location);
|
|
|
|
|
if (prefab != null)
|
|
|
|
|
{
|
|
|
|
|
return prefab;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return AllowLegacyResourcesFallback ? Resources.Load<GameObject>(location) : null;
|
2026-03-26 10:49:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async UniTask<GameObject> LoadPrefabAsync(string location, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
2026-03-31 17:25:20 +08:00
|
|
|
if (TryGetResourceService(out var resourceService))
|
|
|
|
|
{
|
|
|
|
|
var prefab = await resourceService.LoadAssetAsync<GameObject>(location, cancellationToken);
|
|
|
|
|
if (prefab != null)
|
|
|
|
|
{
|
|
|
|
|
return prefab;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return AllowLegacyResourcesFallback
|
|
|
|
|
? await Resources.LoadAsync<GameObject>(location).ToUniTask(cancellationToken: cancellationToken) as GameObject
|
|
|
|
|
: null;
|
2026-03-26 10:49:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public GameObject LoadGameObject(string location, Transform parent = null)
|
|
|
|
|
{
|
2026-03-31 17:25:20 +08:00
|
|
|
if (TryGetResourceService(out var resourceService))
|
|
|
|
|
{
|
|
|
|
|
var managedObject = resourceService.LoadGameObject(location, parent);
|
|
|
|
|
if (managedObject != null)
|
|
|
|
|
{
|
|
|
|
|
return managedObject;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!AllowLegacyResourcesFallback) return null;
|
|
|
|
|
|
2026-03-26 10:49:41 +08:00
|
|
|
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)
|
|
|
|
|
{
|
2026-03-31 17:25:20 +08:00
|
|
|
if (TryGetResourceService(out var resourceService))
|
|
|
|
|
{
|
|
|
|
|
var managedObject = await resourceService.LoadGameObjectAsync(location, parent, cancellationToken);
|
|
|
|
|
if (managedObject != null)
|
|
|
|
|
{
|
|
|
|
|
return managedObject;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!AllowLegacyResourcesFallback) return null;
|
|
|
|
|
|
2026-03-26 10:49:41 +08:00
|
|
|
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-31 17:25:20 +08:00
|
|
|
|
|
|
|
|
private static bool TryGetResourceService(out IResourceService resourceService)
|
|
|
|
|
{
|
|
|
|
|
return AppServices.TryGet(out resourceService);
|
|
|
|
|
}
|
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
|
|
|
{
|
2026-03-26 16:14:05 +08:00
|
|
|
private IResourceService _resourceService;
|
2026-03-26 10:49:41 +08:00
|
|
|
|
2026-03-26 16:14:05 +08:00
|
|
|
IResourceService ResourceService
|
2026-03-26 10:49:41 +08:00
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2026-03-26 16:14:05 +08:00
|
|
|
if (_resourceService == null)
|
2026-03-26 10:49:41 +08:00
|
|
|
{
|
2026-03-26 16:14:05 +08:00
|
|
|
_resourceService = AppServices.Require<IResourceService>();
|
2026-03-26 10:49:41 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 16:14:05 +08:00
|
|
|
return _resourceService;
|
2026-03-26 10:49:41 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public GameObject LoadPrefab(string location)
|
|
|
|
|
{
|
2026-03-26 16:14:05 +08:00
|
|
|
return ResourceService.LoadAsset<GameObject>(location);
|
2026-03-26 10:49:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async UniTask<GameObject> LoadPrefabAsync(string location, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
2026-03-26 16:14:05 +08:00
|
|
|
return await ResourceService.LoadAssetAsync<GameObject>(location, cancellationToken);
|
2026-03-26 10:49:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public GameObject LoadGameObject(string location, Transform parent = null)
|
|
|
|
|
{
|
2026-03-26 16:14:05 +08:00
|
|
|
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)
|
|
|
|
|
{
|
2026-03-26 16:14:05 +08:00
|
|
|
return await ResourceService.LoadGameObjectAsync(location, parent, cancellationToken);
|
2026-03-26 10:49:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UnloadAsset(GameObject gameObject)
|
|
|
|
|
{
|
2026-03-26 16:14:05 +08:00
|
|
|
ResourceService.UnloadAsset(gameObject);
|
2026-03-26 10:49:41 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|