| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- //----------------------------------------------
- // NGUI: Next-Gen UI kit
- // Copyright © 2011-2014 Tasharen Entertainment
- //----------------------------------------------
- using UnityEngine;
- using UnityEngine.UI;
- /// <summary>
- /// Tween the object's position.
- /// </summary>
- public class TweenPosition : Tweener
- {
- public Vector3 from;
- public Vector3 to;
- [HideInInspector]
- public bool worldSpace = false;
- Transform mTrans;
- RectTransform mRectTrans;
- public Transform cachedTransform { get { if (mTrans == null) mTrans = transform; return mTrans; } }
- public RectTransform cachedRectTransform { get { if (mRectTrans == null) mRectTrans = GetComponent<RectTransform>(); return mRectTrans; } }
- /// <summary>
- /// Tween's current value.
- /// </summary>
- public Vector3 value
- {
- get
- {
- return worldSpace ? cachedTransform.position : cachedTransform.localPosition;
- }
- set
- {
- if (worldSpace) cachedTransform.position = value;
- else cachedTransform.localPosition = value;
- }
- }
- /// <summary>
- /// Tween the value.
- /// </summary>
- protected override void OnUpdate (float factor, bool isFinished) { value = from * (1f - factor) + to * factor; }
- /// <summary>
- /// Start the tweening operation.
- /// </summary>
- static public TweenPosition Begin (GameObject go, float duration, Vector3 pos)
- {
- TweenPosition comp = Tweener.Begin<TweenPosition>(go, duration);
- comp.from = comp.value;
- comp.to = pos;
- if (duration <= 0f)
- {
- comp.Sample(1f, true);
- comp.enabled = false;
- }
- return comp;
- }
- /// <summary>
- /// Start the tweening operation.
- /// </summary>
- static public TweenPosition Begin (GameObject go, float duration, Vector3 pos, bool worldSpace)
- {
- TweenPosition comp = Tweener.Begin<TweenPosition>(go, duration);
- comp.worldSpace = worldSpace;
- comp.from = comp.value;
- comp.to = pos;
- if (duration <= 0f)
- {
- comp.Sample(1f, true);
- comp.enabled = false;
- }
- return comp;
- }
- [ContextMenu("Set 'From' to current value")]
- public override void SetStartToCurrentValue () { from = value; }
- [ContextMenu("Set 'To' to current value")]
- public override void SetEndToCurrentValue () { to = value; }
- [ContextMenu("Assume value of 'From'")]
- void SetCurrentValueToStart () { value = from; }
- [ContextMenu("Assume value of 'To'")]
- void SetCurrentValueToEnd () { value = to; }
- }
|