-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAASLoader.cs
More file actions
141 lines (137 loc) · 4.69 KB
/
AASLoader.cs
File metadata and controls
141 lines (137 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.Assertions;
using UnityEngine.ResourceManagement.AsyncOperations;
namespace Emptybraces
{
public static class AASLoader
{
static Dictionary<string, AsyncOperationHandle<Object>> _cache = new(16);
static Dictionary<string, AsyncOperationHandle<Sprite>> _cacheSprite = new(16);
static bool IsInvalidKey(string key) => key == "" || key == "none";
// 同期ロード
public static T Load<T>(string key) where T : Object
{
Assert.IsFalse(typeof(T) == typeof(Sprite));
if (IsInvalidKey(key))
return null;
if (_cache.TryGetValue(key, out var handle))
return handle.Status == AsyncOperationStatus.Succeeded ? (T)handle.Result : null;
Debug.Log($"{nameof(AASLoader)}: Load() key={key}");
handle = Addressables.LoadAssetAsync<Object>(key);
_cache[key] = handle;
handle.WaitForCompletion();
return (T)handle.Result;
}
// 同期ロード
public static Sprite LoadSprite(string key)
{
if (IsInvalidKey(key))
return null;
if (_cacheSprite.TryGetValue(key, out var handle))
return handle.Status == AsyncOperationStatus.Succeeded ? handle.Result : null;
Debug.Log($"{nameof(AASLoader)}: LoadSprite() key={key}");
handle = Addressables.LoadAssetAsync<Sprite>(key);
_cacheSprite[key] = handle;
handle.WaitForCompletion();
return handle.Result;
}
// 非同期ロード
public static async UniTask<T> LoadAsync<T>(string key) where T : UnityEngine.Object
{
Assert.IsFalse(typeof(T) == typeof(Sprite));
if (IsInvalidKey(key))
return null;
if (_cache.TryGetValue(key, out var handle))
{
Debug.Log($"{nameof(AASLoader)}: LoadAsync<T>() key={key}, type={typeof(T).Name}, fromCache={true}");
if (!handle.IsDone)
await handle;
return handle.Status == AsyncOperationStatus.Succeeded ? (T)handle.Result : null;
}
Debug.Log($"{nameof(AASLoader)}: LoadAsync<T>() key={key}, type={typeof(T).Name}, fromCache={false}");
handle = Addressables.LoadAssetAsync<Object>(key);
_cache[key] = handle;
await handle;
return handle.Status == AsyncOperationStatus.Succeeded ? (T)handle.Result : null;
}
// 非同期ロード
public static async UniTask<Sprite> LoadSpriteAsync(string key, System.Action<Sprite> onLoaded)
{
if (IsInvalidKey(key))
return null;
if (_cacheSprite.TryGetValue(key, out var handle))
{
Debug.Log($"{nameof(AASLoader)}: LoadSpriteAsync<T>() key={key}, fromCache={true}");
if (!handle.IsDone)
await handle;
return handle.Status == AsyncOperationStatus.Succeeded ? handle.Result : null;
}
Debug.Log($"{nameof(AASLoader)}: LoadSpriteAsync<T>() key={key}, fromCache={false}");
handle = Addressables.LoadAssetAsync<Sprite>(key);
_cacheSprite[key] = handle;
await handle;
return handle.Status == AsyncOperationStatus.Succeeded ? handle.Result : null;
}
public static GameObject Instantiate(string key, Transform parent = null)
{
var p = Load<GameObject>(key);
if (p != null)
return GameObject.Instantiate(p, parent);
return null;
}
public static T Instantiate<T>(Transform parent = null) where T : Component
{
var p = Load<GameObject>(typeof(T).Name);
if (p != null)
return GameObject.Instantiate(p, parent).GetComponent<T>();
return null;
}
public static async UniTask<GameObject> InstantiateAsync(string key, Transform parent = null)
{
var p = await LoadAsync<GameObject>(key);
if (p != null)
return GameObject.Instantiate(p, parent);
return null;
}
public static async UniTask<T> InstantiateAsync<T>(string key, Transform parent = null)
{
var p = await LoadAsync<GameObject>(key);
if (p != null)
return GameObject.Instantiate(p, parent).GetComponent<T>();
return default;
}
public static async UniTask<T> InstantiateAsync<T>(Transform parent = null) where T : Component
{
var p = await LoadAsync<GameObject>(typeof(T).Name);
if (p != null)
return GameObject.Instantiate(p, parent).GetComponent<T>();
return null;
}
public static void Release()
{
Debug.Log($"{nameof(AASLoader)}: Release()");
foreach (var i in _cache)
Addressables.Release(i.Value);
foreach (var i in _cacheSprite)
Addressables.Release(i.Value);
_cache.Clear();
_cacheSprite.Clear();
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void OnBeforeSceneLoad()
{
// アプリケーションが終了したときに解放する
Application.quitting += Release;
}
#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void _DomainReset()
{
Application.quitting -= Release;
}
#endif
}
}