SimpleMove.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Threading.Tasks;
  2. using Fort23.Core;
  3. using UnityEngine;
  4. namespace Fort23.Mono
  5. {
  6. public class SimpleMove
  7. {
  8. private float flyingTime;
  9. private float delayTime;
  10. private float duration;
  11. private float delay;
  12. private Vector2 startingPosition;
  13. private Vector2 targetPoint;
  14. private RectTransform rt;
  15. public SimpleMove(RectTransform rt, Vector2 startingPosition, Vector2 targetPoint, float duration = 0.3f)
  16. {
  17. flyingTime = 0;
  18. this.rt = rt;
  19. this.startingPosition = startingPosition;
  20. this.targetPoint = targetPoint;
  21. this.duration = duration;
  22. rt.position = this.startingPosition;
  23. }
  24. public async void Start(float delay)
  25. {
  26. // await TimerComponent.Instance.WaitAsync(1000);
  27. this.delay = delay;
  28. StaticUpdater.Instance.AddRenderUpdateCallBack(Move);
  29. }
  30. private void Move()
  31. {
  32. delayTime += Time.deltaTime;
  33. if (delayTime < delay)
  34. {
  35. return;
  36. }
  37. flyingTime += Time.deltaTime;
  38. if (duration >= flyingTime)
  39. {
  40. rt.position = Vector2.Lerp(startingPosition, targetPoint, flyingTime / duration);
  41. }
  42. else
  43. {
  44. rt.position = targetPoint;
  45. StaticUpdater.Instance.RemoveRenderUpdateCallBack(Move);
  46. }
  47. }
  48. }
  49. }