using System; using UnityEngine; [AddComponentMenu("NGUI/Tween/Tween Position X")] public class TweenPositionX : UITweener { public float from; public float to; [HideInInspector] public bool worldSpace; private Transform mTrans; private UIRect mRect; public Transform cachedTransform { get { if (mTrans == null) { mTrans = base.transform; } return mTrans; } } [Obsolete("Use 'value' instead")] public float positionX { get { return Xvalue; } set { Xvalue = value; } } public float Xvalue { get { if (!worldSpace) { return cachedTransform.localPosition.x; } return cachedTransform.position.x; } set { if (mRect == null || !mRect.isAnchored || worldSpace) { if (worldSpace) { cachedTransform.position = new Vector3(value, cachedTransform.position.y, cachedTransform.position.z); } else { cachedTransform.localPosition = new Vector3(value, cachedTransform.localPosition.y, cachedTransform.localPosition.z); } } else { value -= cachedTransform.localPosition.x; NGUIMath.MoveRect(mRect, value, 0f); } } } private void Awake() { mRect = GetComponent(); } protected override void OnUpdate(float factor, bool isFinished) { Xvalue = from * (1f - factor) + to * factor; } public static TweenPosition Begin(GameObject go, float duration, Vector3 pos) { TweenPosition tweenPosition = UITweener.Begin(go, duration); tweenPosition.from = tweenPosition.value; tweenPosition.to = pos; if (duration <= 0f) { tweenPosition.Sample(1f, isFinished: true); tweenPosition.enabled = false; } return tweenPosition; } public static TweenPosition Begin(GameObject go, float duration, Vector3 pos, bool worldSpace) { TweenPosition tweenPosition = UITweener.Begin(go, duration); tweenPosition.worldSpace = worldSpace; tweenPosition.from = tweenPosition.value; tweenPosition.to = pos; if (duration <= 0f) { tweenPosition.Sample(1f, isFinished: true); tweenPosition.enabled = false; } return tweenPosition; } [ContextMenu("Set 'From' to current value")] public override void SetStartToCurrentValue() { from = Xvalue; } [ContextMenu("Set 'To' to current value")] public override void SetEndToCurrentValue() { to = Xvalue; } [ContextMenu("Assume value of 'From'")] private void SetCurrentValueToStart() { Xvalue = from; } [ContextMenu("Assume value of 'To'")] private void SetCurrentValueToEnd() { Xvalue = to; } }