TweenOffset.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using UnityEngine;
  2. /// <summary>
  3. /// Tween the object's Offset.
  4. /// </summary>
  5. public class TweenOffset : Tweener
  6. {
  7. [System.NonSerialized]
  8. public Vector3 defaultPosition = Vector3.one * 213.57f;
  9. public Vector3 offset;
  10. Transform mTrans;
  11. public Transform cachedTransform {
  12. get {
  13. if (mTrans == null)
  14. mTrans = transform;
  15. return mTrans;
  16. }
  17. }
  18. /// <summary>
  19. /// Tween's current value.
  20. /// </summary>
  21. public Vector3 value {
  22. get {
  23. return cachedTransform.localPosition;
  24. }
  25. set {
  26. cachedTransform.localPosition = value;
  27. }
  28. }
  29. /// <summary>
  30. /// Tween the value.
  31. /// </summary>
  32. protected override void OnUpdate(float factor, bool isFinished)
  33. {
  34. if (defaultPosition == Vector3.one * 213.57f)
  35. defaultPosition = value;
  36. Vector3 v3 = value;
  37. if (offset.x != 0)
  38. v3.x = defaultPosition.x * (1f - factor) + (defaultPosition.x + offset.x) * factor;
  39. if (offset.y != 0)
  40. v3.y = defaultPosition.y * (1f - factor) + (defaultPosition.y + offset.y) * factor;
  41. if (offset.z != 0)
  42. v3.z = defaultPosition.z * (1f - factor) + (defaultPosition.z + offset.z) * factor;
  43. value = v3;
  44. //value = defaultPosition * (1f - factor) + (defaultPosition + offset) * factor;
  45. }
  46. /// <summary>
  47. /// Start the tweening operation.
  48. /// </summary>
  49. static public TweenOffset Begin(GameObject go, float duration, Vector3 offset)
  50. {
  51. TweenOffset comp = Tweener.Begin<TweenOffset>(go, duration);
  52. comp.offset = offset;
  53. if (duration <= 0f)
  54. {
  55. comp.Sample(1f, true);
  56. comp.enabled = false;
  57. }
  58. return comp;
  59. }
  60. }