111 lines
3.2 KiB
C#
111 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
#if UNITY_EDITOR
|
|
using UnityEditor.AddressableAssets;
|
|
#endif
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
|
using UnityEngine.ResourceManagement.ResourceProviders;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
namespace DDD
|
|
{
|
|
public class AssetManager : Singleton<AssetManager>, IManager
|
|
{
|
|
public void PreInit()
|
|
{
|
|
|
|
}
|
|
|
|
public async Task Init()
|
|
{
|
|
await Addressables.InitializeAsync().Task;
|
|
}
|
|
|
|
public void PostInit()
|
|
{
|
|
|
|
}
|
|
|
|
public static async Task<T> LoadAsset<T>(string key) where T : UnityEngine.Object
|
|
{
|
|
var handle = Addressables.LoadAssetAsync<T>(key);
|
|
await handle.Task;
|
|
|
|
if (handle.Status == AsyncOperationStatus.Succeeded)
|
|
return handle.Result;
|
|
|
|
Debug.LogError($"Addressable load failed : {key}");
|
|
return null;
|
|
}
|
|
|
|
public static async Task<T> LoadAsset<T>(AssetReference reference) where T : UnityEngine.Object
|
|
{
|
|
if (reference == null)
|
|
{
|
|
Debug.LogError("Null AssetReference");
|
|
return null;
|
|
}
|
|
|
|
var handle = reference.LoadAssetAsync<T>();
|
|
await handle.Task;
|
|
|
|
if (handle.Status == AsyncOperationStatus.Succeeded)
|
|
return handle.Result;
|
|
|
|
Debug.LogError($"AssetReference load failed: {reference.RuntimeKey}");
|
|
return null;
|
|
}
|
|
|
|
public static async Task<List<T>> LoadAssetsByLabel<T>(string label) where T : UnityEngine.Object
|
|
{
|
|
var handle = Addressables.LoadAssetsAsync<T>(label, null);
|
|
await handle.Task;
|
|
|
|
if (handle.Status == AsyncOperationStatus.Succeeded)
|
|
return handle.Result.ToList();
|
|
|
|
Debug.LogError($"[AssetManager] Failed to load assets with label: {label}");
|
|
return new List<T>();
|
|
}
|
|
|
|
public static async Task<SceneInstance> LoadScene(string key, LoadSceneMode mode = LoadSceneMode.Additive)
|
|
{
|
|
var handle = Addressables.LoadSceneAsync(key, mode);
|
|
await handle.Task;
|
|
|
|
if (handle.Status == AsyncOperationStatus.Succeeded)
|
|
return handle.Result;
|
|
|
|
Debug.LogError($"Scene load failed: {key}");
|
|
return default;
|
|
}
|
|
|
|
public static async Task UnloadScene(SceneInstance sceneInstance)
|
|
{
|
|
var handle = Addressables.UnloadSceneAsync(sceneInstance);
|
|
await handle.Task;
|
|
}
|
|
|
|
public static bool HasLabel(string addressKey, string label)
|
|
{
|
|
#if UNITY_EDITOR
|
|
var settings = AddressableAssetSettingsDefaultObject.Settings;
|
|
if (settings == null) return false;
|
|
|
|
var entry = settings.groups
|
|
.SelectMany(g => g.entries)
|
|
.FirstOrDefault(e => e.address == addressKey);
|
|
|
|
if (entry == null) return false;
|
|
|
|
return entry.labels.Contains(label);
|
|
#else
|
|
return true;
|
|
#endif
|
|
}
|
|
}
|
|
} |