ResourcesLoaderComponent.cs 2.1 KB

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