TweenPosition.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. namespace Rokid.MRC
  5. {
  6. public class TweenPosition : Tweener
  7. {
  8. public Vector3 from;
  9. public Vector3 to;
  10. public Vector3 tweenDirection = Vector3.one;
  11. RectTransform mRectTransform = null;
  12. Transform mTransform = null;
  13. bool mIs3D = true;
  14. private bool is3D
  15. {
  16. //get
  17. //{
  18. // if (mTransform == null)
  19. // {
  20. // mTransform = transform;
  21. // RectTransform rect = cachedTransform as RectTransform;
  22. // mIs3D = (rect != null) ? false : true;
  23. // }
  24. // return mIs3D;
  25. //}
  26. get
  27. {
  28. return mIs3D;
  29. }
  30. }
  31. Transform cachedTransform
  32. {
  33. get
  34. {
  35. if(mTransform == null)
  36. {
  37. mTransform = transform;
  38. }
  39. return mTransform;
  40. }
  41. }
  42. RectTransform cachedRectTransform
  43. {
  44. get
  45. {
  46. if(mRectTransform == null)
  47. {
  48. mRectTransform = cachedTransform as RectTransform;
  49. }
  50. return mRectTransform;
  51. }
  52. }
  53. private Vector3 GetCurrentPosition(Vector3 pos)
  54. {
  55. Vector3 tmptargetpos = is3D ? cachedTransform.localPosition : cachedRectTransform.anchoredPosition3D;
  56. Vector3 tmpvalue = pos;
  57. if(tweenDirection.x == 0)
  58. {
  59. tmpvalue.x = tmptargetpos.x;
  60. }
  61. if(tweenDirection.y == 0)
  62. {
  63. tmpvalue.y = tmptargetpos.y;
  64. }
  65. if(tweenDirection.z == 0)
  66. {
  67. tmpvalue.z = tmptargetpos.z;
  68. }
  69. return tmpvalue;
  70. }
  71. public Vector3 value
  72. {
  73. get
  74. {
  75. if(is3D)
  76. {
  77. return cachedTransform.localPosition;
  78. }
  79. else
  80. {
  81. return cachedRectTransform.anchoredPosition;
  82. }
  83. }
  84. set
  85. {
  86. if(is3D)
  87. {
  88. cachedTransform.localPosition = GetCurrentPosition(value);
  89. }
  90. else
  91. {
  92. cachedRectTransform.anchoredPosition = GetCurrentPosition(value);
  93. }
  94. }
  95. }
  96. protected override void OnUpdate(float factor, bool isFinished)
  97. {
  98. value = from + factor * (to - from);
  99. }
  100. //调用该接口,进行缓动
  101. public static TweenPosition Begin(GameObject go, Vector3 from, Vector3 to, float duration = 1f, float delay = 0f)
  102. {
  103. TweenPosition comp = Tweener.Begin<TweenPosition>(go, duration);
  104. comp.value = from;
  105. comp.from = from;
  106. comp.to = to;
  107. comp.duration = duration;
  108. comp.delay = delay;
  109. if(duration <= 0)
  110. {
  111. comp.Sample(1, true);
  112. comp.enabled = false;
  113. }
  114. return comp;
  115. }
  116. [ContextMenu("Set 'From' to current value")]
  117. protected override void SetStartToCurrentValue()
  118. {
  119. from = value;
  120. }
  121. [ContextMenu("Set 'To' to current value")]
  122. protected override void SetEndToCurrentValue()
  123. {
  124. to = value;
  125. }
  126. [ContextMenu("Assume value of 'From'")]
  127. protected override void SetCurrentValueToStart()
  128. {
  129. value = from;
  130. }
  131. [ContextMenu("Assume value of 'To'")]
  132. protected override void SetCurrentValueToEnd()
  133. {
  134. value = to;
  135. }
  136. #if UNITY_EDITOR
  137. [ContextMenu("Horizontal PingPong 6 pixel")]
  138. protected void HorizontalaPingPong()
  139. {
  140. Vector3 pos = cachedRectTransform.anchoredPosition3D;
  141. from = new Vector3(pos.x + 3f, pos.y, pos.z);
  142. to = new Vector3(pos.x - 3f, pos.y, pos.z);
  143. style = iTween.LoopType.pingPong;
  144. method = iTween.EaseType.linear;
  145. duration = 1f;
  146. }
  147. [ContextMenu("Vertical PingPong 6 pixel")]
  148. protected void VerticalPingPong()
  149. {
  150. Vector3 pos = cachedRectTransform.anchoredPosition3D;
  151. from = new Vector3(pos.x, pos.y + 3f, pos.z);
  152. to = new Vector3(pos.x, pos.y - 3f, pos.z);
  153. style = iTween.LoopType.pingPong;
  154. method = iTween.EaseType.linear;
  155. duration = 1f;
  156. }
  157. #endif
  158. }
  159. }