DOTweenAnimCurve.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. // 1.插值 某个数值 。 2.生成动画。 (开始 动画的回调 ,完成动画的回调) 3.关闭动画
  6. namespace ScrollViewUI
  7. {
  8. public enum EaseType
  9. {
  10. Normal
  11. }
  12. public class DGTween
  13. {
  14. public Action actStartFun = null;
  15. public Action actFinishFun = null;
  16. public EaseType type = EaseType.Normal;
  17. public IEnumerator ienumLerp;
  18. public bool isKill;
  19. }
  20. public static class DGTweenSystem
  21. {
  22. public static DGTween SetEase(this DGTween self, EaseType type)
  23. {
  24. self.type = type;
  25. return self;
  26. }
  27. public static DGTween OnStart(this DGTween self, Action actStartFun)
  28. {
  29. self.actStartFun = actStartFun;
  30. return self;
  31. }
  32. public static DGTween OnComplete(this DGTween self, Action actFinishFun)
  33. {
  34. self.actFinishFun = actFinishFun;
  35. return self;
  36. }
  37. }
  38. public class DOTweenAnimCurve : MonoBehaviour
  39. {
  40. private static object lockHelp = new object();
  41. private static DOTweenAnimCurve _instance;
  42. public static DOTweenAnimCurve Instance
  43. {
  44. get
  45. {
  46. lock (lockHelp)
  47. {
  48. if (_instance == null)
  49. {
  50. GameObject go = new GameObject("DOTweenAnimCurve");
  51. _instance = go.AddComponent<DOTweenAnimCurve>();
  52. _instance.Init();
  53. DontDestroyOnLoad(go);
  54. }
  55. }
  56. return _instance;
  57. }
  58. }
  59. //0-1的变动
  60. public Dictionary<EaseType, AnimationCurve> animCurveDic = new Dictionary<EaseType, AnimationCurve>();
  61. public void Init()
  62. {
  63. animCurveDic.Add(EaseType.Normal, AnimCurve_Normal());
  64. }
  65. #region 曲线
  66. public AnimationCurve AnimCurve_Normal()
  67. {
  68. AnimationCurve animCurve = new AnimationCurve();
  69. animCurve.AddKey(0, 0);
  70. animCurve.AddKey(1, 1);
  71. return animCurve;
  72. }
  73. #endregion
  74. #region 移动 动画
  75. public DGTween DOAnchorPosY(RectTransform rtf, float endV, float time)
  76. {
  77. //TODO: DGTween 对象池子生成。 回收要重置
  78. return To(rtf.anchoredPosition.y, endV, time, progress =>
  79. {
  80. rtf.anchoredPosition = new Vector2(rtf.anchoredPosition.x, progress);
  81. });
  82. }
  83. public DGTween DOAnchorPosX(RectTransform rtf, float endV, float time)
  84. {
  85. //TODO: DGTween 对象池子生成。 回收要重置
  86. return To(rtf.anchoredPosition.x, endV, time, progress =>
  87. {
  88. rtf.anchoredPosition = new Vector2(progress, rtf.anchoredPosition.y);
  89. });
  90. }
  91. #endregion
  92. public DGTween To(float startV, float endV, float time, Action<float> actUpdateFun)
  93. {
  94. //TODO: DGTween 对象池子生成。 回收要重置
  95. DGTween animCurveClass = new DGTween();
  96. animCurveClass.ienumLerp = StartLerp(animCurveClass, startV, endV, time, actUpdateFun);
  97. StartCoroutine(animCurveClass.ienumLerp);
  98. return animCurveClass;
  99. }
  100. public void KillAnimCurve(DGTween animCurve)
  101. {
  102. if (!animCurve.isKill && animCurve.ienumLerp != null)
  103. {
  104. StopCoroutine(animCurve.ienumLerp);
  105. }
  106. animCurve.ienumLerp = null;
  107. }
  108. IEnumerator StartLerp(DGTween ani, float startV, float endV, float time, Action<float> actUpdateFun)
  109. {
  110. float aa = 0;
  111. AnimationCurve trt = animCurveDic[ani.type];
  112. yield return null;
  113. ani.actStartFun?.Invoke();
  114. while (aa < time)
  115. {
  116. aa += Time.deltaTime;
  117. float ass = trt.Evaluate(aa / time);
  118. float curV = Mathf.Lerp(startV, endV, ass);
  119. actUpdateFun?.Invoke(curV);
  120. yield return null;
  121. }
  122. actUpdateFun?.Invoke(endV);
  123. ani.actFinishFun?.Invoke();
  124. ani.isKill = true;
  125. }
  126. }
  127. }