123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- namespace Rokid.MRC
- {
- public class TweenPosition : Tweener
- {
- public Vector3 from;
- public Vector3 to;
- public Vector3 tweenDirection = Vector3.one;
- RectTransform mRectTransform = null;
- Transform mTransform = null;
- bool mIs3D = true;
- private bool is3D
- {
- //get
- //{
- // if (mTransform == null)
- // {
- // mTransform = transform;
- // RectTransform rect = cachedTransform as RectTransform;
- // mIs3D = (rect != null) ? false : true;
- // }
- // return mIs3D;
- //}
- get
- {
- return mIs3D;
- }
- }
- Transform cachedTransform
- {
- get
- {
- if(mTransform == null)
- {
- mTransform = transform;
- }
- return mTransform;
- }
- }
- RectTransform cachedRectTransform
- {
- get
- {
- if(mRectTransform == null)
- {
- mRectTransform = cachedTransform as RectTransform;
- }
- return mRectTransform;
- }
- }
- private Vector3 GetCurrentPosition(Vector3 pos)
- {
- Vector3 tmptargetpos = is3D ? cachedTransform.localPosition : cachedRectTransform.anchoredPosition3D;
- Vector3 tmpvalue = pos;
- if(tweenDirection.x == 0)
- {
- tmpvalue.x = tmptargetpos.x;
- }
- if(tweenDirection.y == 0)
- {
- tmpvalue.y = tmptargetpos.y;
- }
- if(tweenDirection.z == 0)
- {
- tmpvalue.z = tmptargetpos.z;
- }
- return tmpvalue;
- }
- public Vector3 value
- {
- get
- {
- if(is3D)
- {
- return cachedTransform.localPosition;
- }
- else
- {
- return cachedRectTransform.anchoredPosition;
- }
- }
- set
- {
- if(is3D)
- {
- cachedTransform.localPosition = GetCurrentPosition(value);
- }
- else
- {
- cachedRectTransform.anchoredPosition = GetCurrentPosition(value);
- }
- }
- }
- protected override void OnUpdate(float factor, bool isFinished)
- {
- value = from + factor * (to - from);
- }
- //调用该接口,进行缓动
- public static TweenPosition Begin(GameObject go, Vector3 from, Vector3 to, float duration = 1f, float delay = 0f)
- {
- TweenPosition comp = Tweener.Begin<TweenPosition>(go, duration);
- comp.value = from;
- comp.from = from;
- comp.to = to;
- comp.duration = duration;
- comp.delay = delay;
- if(duration <= 0)
- {
- comp.Sample(1, true);
- comp.enabled = false;
- }
- return comp;
- }
- [ContextMenu("Set 'From' to current value")]
- protected override void SetStartToCurrentValue()
- {
- from = value;
- }
- [ContextMenu("Set 'To' to current value")]
- protected override void SetEndToCurrentValue()
- {
- to = value;
- }
- [ContextMenu("Assume value of 'From'")]
- protected override void SetCurrentValueToStart()
- {
- value = from;
- }
- [ContextMenu("Assume value of 'To'")]
- protected override void SetCurrentValueToEnd()
- {
- value = to;
- }
- #if UNITY_EDITOR
- [ContextMenu("Horizontal PingPong 6 pixel")]
- protected void HorizontalaPingPong()
- {
- Vector3 pos = cachedRectTransform.anchoredPosition3D;
- from = new Vector3(pos.x + 3f, pos.y, pos.z);
- to = new Vector3(pos.x - 3f, pos.y, pos.z);
- style = iTween.LoopType.pingPong;
- method = iTween.EaseType.linear;
- duration = 1f;
- }
- [ContextMenu("Vertical PingPong 6 pixel")]
- protected void VerticalPingPong()
- {
- Vector3 pos = cachedRectTransform.anchoredPosition3D;
- from = new Vector3(pos.x, pos.y + 3f, pos.z);
- to = new Vector3(pos.x, pos.y - 3f, pos.z);
- style = iTween.LoopType.pingPong;
- method = iTween.EaseType.linear;
- duration = 1f;
- }
- #endif
- }
- }
|