TweenRectTransformPosition.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using UnityEngine;
  2. using System.Collections;
  3. public class TweenRectTransformPosition : Tweener
  4. {
  5. public Vector3 from;
  6. public Vector3 to;
  7. RectTransform mRectTrans;
  8. public RectTransform cachedRectTransform { get { if (mRectTrans == null) mRectTrans = GetComponent<RectTransform>(); return mRectTrans; } }
  9. public Vector3 value
  10. {
  11. get
  12. {
  13. return cachedRectTransform.anchoredPosition3D;
  14. }
  15. set
  16. {
  17. cachedRectTransform.anchoredPosition3D = value;
  18. }
  19. }
  20. protected override void OnUpdate(float factor, bool isFinished) { value = from * (1f - factor) + to * factor; }
  21. static public TweenRectTransformPosition Begin(GameObject go, float duration, Vector3 pos)
  22. {
  23. TweenRectTransformPosition comp = Tweener.Begin<TweenRectTransformPosition>(go, duration);
  24. comp.from = comp.value;
  25. comp.to = pos;
  26. if (duration <= 0f)
  27. {
  28. comp.Sample(1f, true);
  29. comp.enabled = false;
  30. }
  31. return comp;
  32. }
  33. [ContextMenu("Set 'From' to current value")]
  34. public override void SetStartToCurrentValue() { from = value; }
  35. [ContextMenu("Set 'To' to current value")]
  36. public override void SetEndToCurrentValue() { to = value; }
  37. [ContextMenu("Assume value of 'From'")]
  38. void SetCurrentValueToStart() { value = from; }
  39. [ContextMenu("Assume value of 'To'")]
  40. void SetCurrentValueToEnd() { value = to; }
  41. }