ResourcesLoaderComponent.cs 2.6 KB

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