108 lines
3.6 KiB
C#
108 lines
3.6 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
|
|
{
|
|
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)
|
|
{
|
|
// Resources.UnloadAsset cannot unload GameObjects.
|
|
// The prefab reference is nulled by the caller; Unity handles cleanup via UnloadUnusedAssets.
|
|
}
|
|
}
|
|
|
|
public class AssetBundleResourceLoader : IResourceLoader
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
}
|