com.alicizax.unity.framework/Runtime/ABase/GameObjectPool/IResourceLoader.cs
陈思海 6397cc03b2 框架大更新
1.进步优化UI系统 加载问题 性能问题 Canvas重绘问题 边界处理问题
2.优化对象池和游戏对象池的性能 游戏对象池根据窗口 策略定期清理
3.优化整个AppService 和ServiceWorld结构 固定三大类 具体参考代码
2026-03-31 17:25:20 +08:00

157 lines
5.3 KiB
C#

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
{
private static bool AllowLegacyResourcesFallback => Application.isEditor || Debug.isDebugBuild;
public GameObject LoadPrefab(string location)
{
if (TryGetResourceService(out var resourceService))
{
var prefab = resourceService.LoadAsset<GameObject>(location);
if (prefab != null)
{
return prefab;
}
}
return AllowLegacyResourcesFallback ? Resources.Load<GameObject>(location) : null;
}
public async UniTask<GameObject> LoadPrefabAsync(string location, CancellationToken cancellationToken = default)
{
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;
}
public GameObject LoadGameObject(string location, Transform parent = null)
{
if (TryGetResourceService(out var resourceService))
{
var managedObject = resourceService.LoadGameObject(location, parent);
if (managedObject != null)
{
return managedObject;
}
}
if (!AllowLegacyResourcesFallback) return 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)
{
if (TryGetResourceService(out var resourceService))
{
var managedObject = await resourceService.LoadGameObjectAsync(location, parent, cancellationToken);
if (managedObject != null)
{
return managedObject;
}
}
if (!AllowLegacyResourcesFallback) return null;
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)
{
// Resources.UnloadAsset cannot unload GameObjects.
// The prefab reference is nulled by the caller; Unity handles cleanup via UnloadUnusedAssets.
}
private static bool TryGetResourceService(out IResourceService resourceService)
{
return AppServices.TryGet(out resourceService);
}
}
public class AssetBundleResourceLoader : IResourceLoader
{
private IResourceService _resourceService;
IResourceService ResourceService
{
get
{
if (_resourceService == null)
{
_resourceService = AppServices.Require<IResourceService>();
}
return _resourceService;
}
}
public GameObject LoadPrefab(string location)
{
return ResourceService.LoadAsset<GameObject>(location);
}
public async UniTask<GameObject> LoadPrefabAsync(string location, CancellationToken cancellationToken = default)
{
return await ResourceService.LoadAssetAsync<GameObject>(location, cancellationToken);
}
public GameObject LoadGameObject(string location, Transform parent = null)
{
return ResourceService.LoadGameObject(location, parent);
}
public async UniTask<GameObject> LoadGameObjectAsync(string location, Transform parent = null, CancellationToken cancellationToken = default)
{
return await ResourceService.LoadGameObjectAsync(location, parent, cancellationToken);
}
public void UnloadAsset(GameObject gameObject)
{
ResourceService.UnloadAsset(gameObject);
}
}
}