| //-------------------------------------------------------- | 
| //    [Author]:           玩个游戏 | 
| //    [  Date ]:           Monday, April 09, 2018 | 
| //-------------------------------------------------------- | 
| using UnityEngine; | 
| using System.Collections; | 
| using UnityEngine.UI; | 
|   | 
| [RequireComponent(typeof(RectTransform))] | 
| public class UILinerMove : MonoBehaviour | 
| { | 
|     [SerializeField] float m_Duration; | 
|     public float duration { | 
|         get { return m_Duration; } | 
|         set { m_Duration = value; } | 
|     } | 
|   | 
|     [SerializeField] Vector2 m_From; | 
|     public Vector2 from { | 
|         get { return m_From; } | 
|         set { m_From = value; } | 
|     } | 
|   | 
|     [SerializeField] Vector2 m_To; | 
|     public Vector2 to { | 
|         get { return m_To; } | 
|         set { m_To = value; } | 
|     } | 
|   | 
|     float timer = float.MaxValue; | 
|     RectTransform rectTransform { get { return this.transform as RectTransform; } } | 
|   | 
|     public void Begin() | 
|     { | 
|         if (duration < 0f) | 
|         { | 
|             return; | 
|         } | 
|   | 
|         if (!this.gameObject.activeInHierarchy) | 
|         { | 
|             Stop(); | 
|         } | 
|         else | 
|         { | 
|             timer = 0f; | 
|             rectTransform.anchoredPosition = from; | 
|         } | 
|     } | 
|   | 
|     public void Stop() | 
|     { | 
|         timer = float.MaxValue; | 
|         rectTransform.anchoredPosition = to; | 
|     } | 
|   | 
|     private void OnDisable() | 
|     { | 
|         if (timer < duration) | 
|         { | 
|             Stop(); | 
|         } | 
|     } | 
|   | 
|     private void LateUpdate() | 
|     { | 
|         if (timer < duration) | 
|         { | 
|             timer += Time.deltaTime; | 
|             var t = Mathf.Clamp01(timer / duration); | 
|             rectTransform.anchoredPosition = Vector2.Lerp(from, to, t); | 
|         } | 
|     } | 
|   | 
| } |