ResourcesLoaderComponent.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Collections.Generic;
  2. namespace ET.Client
  3. {
  4. [FriendOf(typeof(ResourcesLoaderComponent))]
  5. public static class ResourcesLoaderComponentSystem
  6. {
  7. [ObjectSystem]
  8. public class ResourcesLoaderComponentDestroySystem: DestroySystem<ResourcesLoaderComponent>
  9. {
  10. protected override void Destroy(ResourcesLoaderComponent self)
  11. {
  12. async ETTask UnLoadAsync()
  13. {
  14. using (ListComponent<string> list = ListComponent<string>.Create())
  15. {
  16. list.AddRange(self.LoadedResource);
  17. self.LoadedResource = null;
  18. if (TimerComponent.Instance == null)
  19. {
  20. return;
  21. }
  22. // 延迟5秒卸载包,因为包卸载是引用计数,5秒之内假如重新有逻辑加载了这个包,那么可以避免一次卸载跟加载
  23. await TimerComponent.Instance.WaitAsync(5000);
  24. foreach (string abName in list)
  25. {
  26. CoroutineLock coroutineLock = null;
  27. try
  28. {
  29. coroutineLock =
  30. await CoroutineLockComponent.Instance.Wait(CoroutineLockType.ResourcesLoader, abName.GetHashCode(), 0);
  31. {
  32. if (ResourcesComponent.Instance == null)
  33. {
  34. return;
  35. }
  36. await ResourcesComponent.Instance.UnloadBundleAsync(abName);
  37. }
  38. }
  39. finally
  40. {
  41. coroutineLock?.Dispose();
  42. }
  43. }
  44. }
  45. }
  46. UnLoadAsync().Coroutine();
  47. }
  48. }
  49. public static async ETTask LoadAsync(this ResourcesLoaderComponent self, string ab)
  50. {
  51. CoroutineLock coroutineLock = null;
  52. try
  53. {
  54. coroutineLock = await CoroutineLockComponent.Instance.Wait(CoroutineLockType.ResourcesLoader, ab.GetHashCode(), 0);
  55. if (self.IsDisposed)
  56. {
  57. Log.Error($"resourceload already disposed {ab}");
  58. return;
  59. }
  60. if (self.LoadedResource.Contains(ab))
  61. {
  62. return;
  63. }
  64. self.LoadedResource.Add(ab);
  65. await ResourcesComponent.Instance.LoadBundleAsync(ab);
  66. }
  67. finally
  68. {
  69. coroutineLock?.Dispose();
  70. }
  71. }
  72. }
  73. [ComponentOf(typeof(Scene))]
  74. public class ResourcesLoaderComponent: Entity, IAwake, IDestroy
  75. {
  76. public HashSet<string> LoadedResource = new HashSet<string>();
  77. }
  78. }