ResourcesLoaderComponent.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. ListComponent<string> list = ListComponent<string>.Create();
  12. try
  13. {
  14. list.List.AddRange(self.LoadedResource);
  15. self.LoadedResource = null;
  16. // 延迟5秒卸载包,因为包卸载是引用技术,5秒之内假如重新有逻辑加载了这个包,那么可以避免一次卸载跟加载
  17. await TimerComponent.Instance.WaitAsync(5000);
  18. foreach (string abName in list.List)
  19. {
  20. CoroutineLock coroutineLock = null;
  21. try
  22. {
  23. coroutineLock =
  24. await CoroutineLockComponent.Instance.Wait(CoroutineLockType.ResourcesLoader, abName.GetHashCode(), 0);
  25. {
  26. if (ResourcesComponent.Instance == null)
  27. {
  28. return;
  29. }
  30. await ResourcesComponent.Instance.UnloadBundleAsync(abName);
  31. }
  32. }
  33. finally
  34. {
  35. coroutineLock?.Dispose();
  36. }
  37. }
  38. }
  39. finally
  40. {
  41. list.Dispose();
  42. }
  43. }
  44. UnLoadAsync().Coroutine();
  45. }
  46. }
  47. public class ResourcesLoaderComponent: Entity
  48. {
  49. public HashSet<string> LoadedResource = new HashSet<string>();
  50. public async ETTask LoadAsync(string ab)
  51. {
  52. CoroutineLock coroutineLock = null;
  53. try
  54. {
  55. coroutineLock = await CoroutineLockComponent.Instance.Wait(CoroutineLockType.ResourcesLoader, ab.GetHashCode(), 0);
  56. if (this.IsDisposed)
  57. {
  58. Log.Error($"resourceload already disposed {ab}");
  59. return;
  60. }
  61. if (this.LoadedResource.Contains(ab))
  62. {
  63. return;
  64. }
  65. LoadedResource.Add(ab);
  66. await ResourcesComponent.Instance.LoadBundleAsync(ab);
  67. }
  68. finally
  69. {
  70. coroutineLock?.Dispose();
  71. }
  72. }
  73. }
  74. }