Advertisement
waygeek

Untitled

Oct 2nd, 2024
31
0
278 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.54 KB | Gaming | 0 0
  1. using System.Collections.Generic;
  2. using Cysharp.Threading.Tasks;
  3. using UnityEngine;
  4. using Dre0Dru.AddressableAssets.Loaders;
  5. using UnityEngine.AddressableAssets;
  6. using UnityEngine.ResourceManagement.AsyncOperations;
  7.  
  8. namespace Dre0Dru.AddressableAssets.Loader
  9. {
  10.     public class AssetsNameLoader<TAsset>  : IAssetsLoader<string, TAsset> where TAsset : Object
  11.     {
  12.         private readonly Dictionary<int, AsyncOperationHandle<TAsset>> _operationHandles = new();
  13.  
  14.         public async void InitializeAsync()
  15.         {
  16.             Debug.Log("Start Addressables initializ.");
  17.  
  18.             var initHandle = Addressables.InitializeAsync();
  19.             await initHandle.ToUniTask();
  20.  
  21.             if (initHandle.Status == AsyncOperationStatus.Succeeded)
  22.             {
  23.                 Debug.Log("Addressables initialized successfully.");
  24.             }
  25.             else
  26.             {
  27.                 Debug.LogError("Failed to initialize Addressables.");
  28.             }
  29.         }
  30.  
  31.         public UniTask PreloadAssetAsync(string name) => LoadAssetAsync(name);
  32.  
  33.         // ОСЬ ЦЕЙ МЕТОД ВСЕ МАЄ ЗАВАНТАЖУВАТИ
  34.         public async UniTask<TAsset> LoadAssetAsync(string name)
  35.         {
  36.             try
  37.             {
  38.                 Debug.LogError($"Start load sprite: {name}");
  39.  
  40.                 AsyncOperationHandle<TAsset> handle = Addressables.LoadAssetAsync<TAsset>(name);
  41.  
  42.                 Debug.LogError($"handle!!!");
  43.  
  44.                 await handle.ToUniTask();
  45.  
  46.                 Debug.LogError($"handle.ToUniTask!!!");
  47.  
  48.                 if (handle.Status == AsyncOperationStatus.Succeeded)
  49.                 {
  50.                     Debug.LogError($"AsyncOperationStatus.Succeeded!!!");
  51.  
  52.                     if (!_operationHandles.TryAdd(name.GetHashCode(), handle))
  53.                     {
  54.                         Addressables.Release(_operationHandles[name.GetHashCode()]); // Розвантажити попередню операцію
  55.                         _operationHandles[name.GetHashCode()] = handle; // Оновити дескриптор операції
  56.                     }
  57.  
  58.                     Debug.LogError($"end load sprite: {name}");
  59.  
  60.                     return handle.Result;
  61.                 }
  62.                 else
  63.                 {
  64.                     Debug.LogError($"Не вдалося завантажити ресурс: {name}");
  65.                 }
  66.             }
  67.             catch (System.Exception e)
  68.             {
  69.                 Debug.LogError($"Сталася помилка під час завантаження ресурсу: {e.Message}");
  70.             }
  71.  
  72.             return null;
  73.         }
  74.  
  75.         public bool IsAssetLoaded(string name)
  76.         {
  77.             if (_operationHandles.TryGetValue(name.GetHashCode(), out AsyncOperationHandle<TAsset> handle))
  78.             {
  79.                 return handle.Status == AsyncOperationStatus.Succeeded;
  80.             }
  81.  
  82.             return false;
  83.         }
  84.  
  85.         public TAsset GetAsset(string name)
  86.         {
  87.             return _operationHandles[name.GetHashCode()].Result;
  88.         }
  89.  
  90.         public bool TryGetAsset(string name, out TAsset asset)
  91.         {
  92.             asset = default;
  93.  
  94.             if (IsAssetLoaded(name))
  95.             {
  96.                 asset = GetAsset(name);
  97.  
  98.                 return asset;
  99.             }
  100.  
  101.             return false;
  102.         }
  103.  
  104.         public void UnloadAsset(string name)
  105.         {
  106.             int hashKey = name.GetHashCode();
  107.  
  108.             if (_operationHandles.TryGetValue(hashKey, out AsyncOperationHandle<TAsset> handle))
  109.             {
  110.                 Addressables.Release(handle);
  111.                 _operationHandles.Remove(hashKey);
  112.             }
  113.         }
  114.  
  115.         public void UnloadAllAssets()
  116.         {
  117.             foreach (var handle in _operationHandles.Values)
  118.             {
  119.                 Addressables.Release(handle);
  120.             }
  121.  
  122.             _operationHandles.Clear();
  123.         }
  124.     }
  125. }
  126.  
  127.  
  128. ПОМИЛКА
  129. 2024/10/02 17:29:51.102 4398 4417 Error Unity Start load sprite: Genres // мій дебаг
  130. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #0 0x7bbbc6cc3c (libunity.so) ? 0x0
  131. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #1 0x7bbc13d0e0 (libunity.so) ? 0x0
  132. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #2 0x7bbb9410e0 (libunity.so) ? 0x0
  133. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #3 0x7bbb940fd0 (libunity.so) ? 0x0
  134. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #4 0x7bb6d10620 (libil2cpp.so) ? 0x0
  135. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #5 0x7bb6b7f768 (libil2cpp.so) ? 0x0
  136. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #6 0x7bb6ec813c (libil2cpp.so) ? 0x0
  137. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #7 0x7bb6ec8068 (libil2cpp.so) ? 0x0
  138. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #8 0x7bb6b7a490 (libil2cpp.so) ? 0x0
  139. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #9 0x7bb6ad3514 (libil2cpp.so) ? 0x0
  140. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #10 0x7bb6b7a2c0 (libil2cpp.so) ? 0x0
  141. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #11 0x7bb6b8ba24 (libil2cpp.so) ? 0x0
  142. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #12 0x7bb6ad3514 (libil2cpp.so) ? 0x0
  143. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #13 0x7bb6b8af60 (libil2cpp.so) ? 0x0
  144. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #14 0x7bb6b8b26c (libil2cpp.so) ? 0x0
  145. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #15 0x7bb6edc3d0 (libil2cpp.so) ? 0x0
  146. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #16 0x7bb68767f4 (libil2cpp.so) ? 0x0
  147. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #17 0x7bb6d651a0 (libil2cpp.so) ? 0x0
  148. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #18 0x7bb6edc558 (libil2cpp.so) ? 0x0
  149. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #19 0x7bb6af0ae4 (libil2cpp.so) ? 0x0
  150. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #20 0x7bb6edc3d0 (libil2cpp.so) ? 0x0
  151. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #21 0x7bb68767f4 (libil2cpp.so) ? 0x0
  152. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #22 0x7bb6d651a0 (libil2cpp.so) ? 0x0
  153. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #23 0x7bb777b720 (libil2cpp.so) ? 0x0
  154. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #24 0x7bb77ada88 (libil2cpp.so) ? 0x0
  155. 2024/10/02 17:29:51.102 4398 4417 Error Unity  #25 0x7bb69c75b8 (libil2cpp.so) ? 0
  156. 2024/10/02 17:29:51.105 4398 4417 Error Unity handle!!! // це мої дебаги
  157. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #0 0x7bbbc6cc3c (libunity.so) ? 0x0
  158. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #1 0x7bbc13d0e0 (libunity.so) ? 0x0
  159. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #2 0x7bbb9410e0 (libunity.so) ? 0x0
  160. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #3 0x7bbb940fd0 (libunity.so) ? 0x0
  161. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #4 0x7bb6d10688 (libil2cpp.so) ? 0x0
  162. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #5 0x7bb6b7f768 (libil2cpp.so) ? 0x0
  163. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #6 0x7bb6ec813c (libil2cpp.so) ? 0x0
  164. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #7 0x7bb6ec8068 (libil2cpp.so) ? 0x0
  165. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #8 0x7bb6b7a490 (libil2cpp.so) ? 0x0
  166. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #9 0x7bb6ad3514 (libil2cpp.so) ? 0x0
  167. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #10 0x7bb6b7a2c0 (libil2cpp.so) ? 0x0
  168. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #11 0x7bb6b8ba24 (libil2cpp.so) ? 0x0
  169. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #12 0x7bb6ad3514 (libil2cpp.so) ? 0x0
  170. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #13 0x7bb6b8af60 (libil2cpp.so) ? 0x0
  171. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #14 0x7bb6b8b26c (libil2cpp.so) ? 0x0
  172. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #15 0x7bb6edc3d0 (libil2cpp.so) ? 0x0
  173. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #16 0x7bb68767f4 (libil2cpp.so) ? 0x0
  174. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #17 0x7bb6d651a0 (libil2cpp.so) ? 0x0
  175. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #18 0x7bb6edc558 (libil2cpp.so) ? 0x0
  176. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #19 0x7bb6af0ae4 (libil2cpp.so) ? 0x0
  177. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #20 0x7bb6edc3d0 (libil2cpp.so) ? 0x0
  178. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #21 0x7bb68767f4 (libil2cpp.so) ? 0x0
  179. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #22 0x7bb6d651a0 (libil2cpp.so) ? 0x0
  180. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #23 0x7bb777b720 (libil2cpp.so) ? 0x0
  181. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #24 0x7bb77ada88 (libil2cpp.so) ? 0x0
  182. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #25 0x7bb69c75b8 (libil2cpp.so) ? 0x0
  183. 2024/10/02 17:29:51.105 4398 4417 Error Unity  #26 0x7bb69c
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement