Advertisement
waygeek

Quest3

Apr 17th, 2024
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | Gaming | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Cysharp.Threading.Tasks;
  4. using UnityEngine;
  5. using UnityEngine.AddressableAssets;
  6. using UnityEngine.ResourceManagement.AsyncOperations;
  7.  
  8. namespace Dre0Dru.AddressableAssets.Loaders
  9. {
  10. public class AssetsLabelLoader<TAsset> : IAssetsLoader<string, TAsset> where TAsset : Object
  11. {
  12. private readonly Dictionary<string, AsyncOperationHandle<TAsset>> _operationHandles = new();
  13. private readonly Dictionary<string, List<string>> _mapsLabel = new();
  14.  
  15. public async UniTask PreloadAssetAsync(string label) => await LoadAssetAsync(label);
  16.  
  17. public async UniTask<TAsset> LoadAssetAsync(string label)
  18. {
  19. AsyncOperationHandle<IList<TAsset>> handle = Addressables.LoadAssetsAsync<TAsset>(label, null);
  20.  
  21. await handle.Task;
  22.  
  23. List<string> names = new();
  24.  
  25. // Додати елементи зі списку картинок в словник
  26. if (handle.Result.Count > 0)
  27. {
  28. foreach (TAsset asset in handle.Result)
  29. {
  30. names.Add(asset.name);
  31.  
  32. _operationHandles[asset.name] = Addressables.ResourceManager
  33. .CreateCompletedOperation(asset, null);
  34. }
  35. }
  36.  
  37. _mapsLabel.TryAdd(label, names);
  38.  
  39. // Повертаємо перший елемент для прикладу, але це можна змінити залежно від вашої логіки
  40. return handle.Result.Count > 0 ? handle.Result[0] : null;
  41. }
  42.  
  43. public bool IsAssetLoaded(string label)
  44. {
  45. if (_operationHandles.TryGetValue(label, out var handle))
  46. {
  47. return handle.Status == AsyncOperationStatus.Succeeded;
  48. }
  49.  
  50. return false;
  51. }
  52.  
  53. public TAsset GetAsset(string name)
  54. {
  55. if (_operationHandles.TryGetValue(name, out AsyncOperationHandle<TAsset> handle))
  56. {
  57. return handle.Result;
  58. }
  59.  
  60. return null;
  61. }
  62.  
  63. public bool TryGetAsset(string label, out TAsset asset)
  64. {
  65. asset = default;
  66.  
  67. if (IsAssetLoaded(label))
  68. {
  69. asset = GetAsset(label);
  70. return asset;
  71. }
  72.  
  73. return false;
  74. }
  75.  
  76. public void UnloadAsset(string label)
  77. {
  78. if (_mapsLabel.TryGetValue(label, out List<string> assetNames))
  79. {
  80. foreach (string assetName in assetNames.ToList())
  81. {
  82. Addressables.Release(_operationHandles[assetName]);
  83. _operationHandles.Remove(assetName);
  84. }
  85.  
  86. _mapsLabel.Remove(label);
  87. }
  88. }
  89.  
  90. public void UnloadAllAssets()
  91. {
  92. foreach (var handle in _operationHandles.Values)
  93. {
  94. Addressables.Release(handle);
  95. }
  96.  
  97. _operationHandles.Clear();
  98. _mapsLabel.Clear();
  99. }
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement