SceneChangeComponent.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Threading.Tasks;
  2. using UnityEngine;
  3. using UnityEngine.SceneManagement;
  4. namespace ETModel
  5. {
  6. [ObjectSystem]
  7. public class SceneChangeComponentUpdateSystem: UpdateSystem<SceneChangeComponent>
  8. {
  9. public override void Update(SceneChangeComponent self)
  10. {
  11. if (self.loadMapOperation.isDone)
  12. {
  13. self.tcs.SetResult(true);
  14. }
  15. }
  16. }
  17. public class SceneChangeComponent: Component
  18. {
  19. public AsyncOperation loadMapOperation;
  20. public TaskCompletionSource<bool> tcs;
  21. public float deltaTime;
  22. public int lastProgress = 0;
  23. public Task<bool> ChangeSceneAsync(SceneType sceneEnum)
  24. {
  25. this.tcs = new TaskCompletionSource<bool>();
  26. // 加载map
  27. this.loadMapOperation = SceneManager.LoadSceneAsync(sceneEnum.ToString());
  28. return this.tcs.Task;
  29. }
  30. public int Process
  31. {
  32. get
  33. {
  34. if (this.loadMapOperation == null)
  35. {
  36. return 0;
  37. }
  38. return (int)(this.loadMapOperation.progress * 100);
  39. }
  40. }
  41. public void Finish()
  42. {
  43. this.tcs.SetResult(true);
  44. }
  45. public override void Dispose()
  46. {
  47. if (this.IsDisposed)
  48. {
  49. return;
  50. }
  51. base.Dispose();
  52. }
  53. }
  54. }