ResourcesLoaderComponent.cs 2.7 KB

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