SceneChangeComponent.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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(string sceneName)
  24. {
  25. this.tcs = new TaskCompletionSource<bool>();
  26. // 加载map
  27. this.loadMapOperation = SceneManager.LoadSceneAsync(sceneName);
  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. if (this.Entity.IsDisposed)
  53. {
  54. return;
  55. }
  56. this.Entity.RemoveComponent<SceneChangeComponent>();
  57. }
  58. }
  59. }