ProjectDDD/Assets/_DDD/_Scripts/AssetManagement/AssetManager.cs

133 lines
3.9 KiB
C#
Raw Normal View History

2025-07-10 05:48:44 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2025-07-15 03:59:53 +00:00
#if UNITY_EDITOR
using UnityEditor.AddressableAssets;
2025-07-15 03:59:53 +00:00
#endif
2025-07-09 09:45:11 +00:00
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceProviders;
using UnityEngine.SceneManagement;
2025-07-09 09:45:11 +00:00
namespace DDD
{
public class AssetManager : Singleton<AssetManager>, IManager
{
2025-07-29 18:52:13 +00:00
private static readonly Dictionary<string, AsyncOperationHandle> _cachedHandles = new();
protected override void OnApplicationQuit()
{
base.OnApplicationQuit();
ReleaseAllCached();
}
public void PreInit()
2025-07-09 09:45:11 +00:00
{
}
public async Task Init()
2025-07-09 09:45:11 +00:00
{
await Addressables.InitializeAsync().Task;
}
public void PostInit()
{
2025-07-10 05:48:44 +00:00
}
public static async Task<T> LoadAsset<T>(string key) where T : UnityEngine.Object
2025-07-10 05:48:44 +00:00
{
2025-07-29 18:52:13 +00:00
if (_cachedHandles.TryGetValue(key, out var handle))
{
2025-07-29 18:52:13 +00:00
if (handle.IsValid() && handle.Result is T result)
return result;
Debug.LogWarning($"[AssetManager] Type mismatch or invalid handle for key: {key}");
return handle.Result as T;
}
2025-07-29 18:52:13 +00:00
// ✅ 새로 로드
var newHandle = Addressables.LoadAssetAsync<T>(key);
await newHandle.Task;
2025-07-29 18:52:13 +00:00
if (newHandle.Status == AsyncOperationStatus.Succeeded)
{
_cachedHandles[key] = newHandle;
return newHandle.Result;
}
2025-07-29 18:52:13 +00:00
Debug.LogError($"[AssetManager] Failed to load asset: {key}");
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;
}
2025-07-29 18:52:13 +00:00
public static void ReleaseAllCached()
{
if (_cachedHandles.Count == 0) return;
foreach (var kvp in _cachedHandles)
{
var handle = kvp.Value;
if (handle.IsValid())
{
Addressables.Release(handle);
2025-07-29 20:57:19 +00:00
//Debug.Log($"[AssetManager] Released handle for key: {kvp.Key}");
2025-07-29 18:52:13 +00:00
}
}
_cachedHandles.Clear();
Debug.Log("[AssetManager] 모든 캐시된 Addressable 리소스를 해제했습니다.");
}
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
}
2025-07-09 09:45:11 +00:00
}
}