SimpleMove.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Threading.Tasks;
  2. using Fort23.Core;
  3. using Fort23.UTool;
  4. using UnityEngine;
  5. namespace Fort23.Mono
  6. {
  7. public class SimpleMove
  8. {
  9. private float flyingTime;
  10. private float delayTime;
  11. private float duration;
  12. private float delay;
  13. private Vector2 startingPosition;
  14. private Vector2 targetPoint;
  15. private RectTransform targetRT;
  16. public SimpleMove(RectTransform targetRT, Vector2 startingPosition, Vector2 targetPoint, float duration = 0.3f)
  17. {
  18. flyingTime = 0;
  19. delayTime = 0;
  20. this.targetRT = targetRT;
  21. this.startingPosition = startingPosition;
  22. this.targetPoint = targetPoint;
  23. this.duration = duration;
  24. targetRT.position = this.startingPosition;
  25. // PrintInfo(1);
  26. // rt.gameObject.SetActive(false);
  27. }
  28. // private void PrintInfo(byte typ)
  29. // {
  30. // LogTool.Log("【" + typ + "】startPos: " + startingPosition + " | " + "targetPos:" + targetPoint);
  31. // }
  32. // public void SetRectTransform(RectTransform targetRT)
  33. // {
  34. // this.targetRT = targetRT;
  35. // targetRT.position = this.startingPosition;
  36. // PrintInfo(2);
  37. // }
  38. //
  39. // public void Reverse()
  40. // {
  41. // // ReSharper disable once SwapViaDeconstruction
  42. // var temp = startingPosition;
  43. // startingPosition = targetPoint;
  44. // targetPoint = temp;
  45. // flyingTime = 0;
  46. // delayTime = 0;
  47. // }
  48. public async void Start(float delay)
  49. {
  50. // await TimerComponent.Instance.WaitAsync(1000);
  51. this.delay = delay;
  52. // rt.gameObject.SetActive(true);
  53. StaticUpdater.Instance.AddRenderUpdateCallBack(Move);
  54. }
  55. private void Move()
  56. {
  57. delayTime += Time.deltaTime;
  58. if (delayTime < delay)
  59. {
  60. return;
  61. }
  62. flyingTime += Time.deltaTime;
  63. if (duration >= flyingTime)
  64. {
  65. targetRT.position = Vector2.Lerp(startingPosition, targetPoint, flyingTime / duration);
  66. }
  67. else
  68. {
  69. targetRT.position = targetPoint;
  70. StaticUpdater.Instance.RemoveRenderUpdateCallBack(Move);
  71. }
  72. }
  73. }
  74. }