ResourcesComponent.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System.Collections.Generic;
  2. using UnityEngine.AddressableAssets;
  3. namespace ET
  4. {
  5. [ComponentOf(typeof(Scene))]
  6. public class ResourcesComponent: Singleton<ResourcesComponent>, ISingletonAwake
  7. {
  8. private readonly Dictionary<string, UnityEngine.Object> resources = new();
  9. public void Awake()
  10. {
  11. }
  12. protected override void Destroy()
  13. {
  14. foreach (var kv in this.resources)
  15. {
  16. Addressables.Release(kv.Value);
  17. }
  18. }
  19. public bool TryGetAssets(string assetsName, out UnityEngine.Object o)
  20. {
  21. return this.resources.TryGetValue(assetsName, out o);
  22. }
  23. public UnityEngine.Object GetAssets(string assetsName)
  24. {
  25. this.resources.TryGetValue(assetsName, out UnityEngine.Object o);
  26. return o;
  27. }
  28. public async ETTask<UnityEngine.Object> LoadAssetAsync(string assetsName)
  29. {
  30. UnityEngine.Object o = await Addressables.LoadAssetAsync<UnityEngine.Object>(assetsName).Task;
  31. this.resources.Add(assetsName, o);
  32. return o;
  33. }
  34. public void ReleaseAssets(string assetsName)
  35. {
  36. if (!this.resources.TryGetValue(assetsName, out UnityEngine.Object o))
  37. {
  38. return;
  39. }
  40. Addressables.Release(o);
  41. }
  42. }
  43. }