ResourcesLoaderComponent.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Collections.Generic;
  2. namespace ET
  3. {
  4. [ObjectSystem]
  5. public class ResourcesLoaderComponentDestroySystem: DestroySystem<ResourcesLoaderComponent>
  6. {
  7. public override void Destroy(ResourcesLoaderComponent self)
  8. {
  9. async ETTask UnLoadAsync()
  10. {
  11. using ListComponent<string> list = ListComponent<string>.Create();
  12. list.List.AddRange(self.LoadedResource);
  13. self.LoadedResource = null;
  14. // 延迟5秒卸载包,因为包卸载是引用技术,5秒之内假如重新有逻辑加载了这个包,那么可以避免一次卸载跟加载
  15. await TimerComponent.Instance.WaitAsync(5000);
  16. foreach (string abName in list.List)
  17. {
  18. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.ResourcesLoader, abName.GetHashCode(), 0))
  19. {
  20. if (ResourcesComponent.Instance == null)
  21. {
  22. return;
  23. }
  24. await ResourcesComponent.Instance.UnloadBundleAsync(abName);
  25. }
  26. }
  27. }
  28. UnLoadAsync().Coroutine();
  29. }
  30. }
  31. public class ResourcesLoaderComponent: Entity
  32. {
  33. public HashSet<string> LoadedResource = new HashSet<string>();
  34. public async ETTask LoadAsync(string ab)
  35. {
  36. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.ResourcesLoader, ab.GetHashCode(), 0))
  37. {
  38. if (this.IsDisposed)
  39. {
  40. Log.Error($"resourceload already disposed {ab}");
  41. return;
  42. }
  43. if (this.LoadedResource.Contains(ab))
  44. {
  45. return;
  46. }
  47. LoadedResource.Add(ab);
  48. await ResourcesComponent.Instance.LoadBundleAsync(ab);
  49. }
  50. }
  51. }
  52. }