yyl
2025-05-16 f4c97339a01fdd75d8b862be572eefe66bd51d5a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//--------------------------------------------------------
//    [Author]:           第二世界
//    [  Date ]:           Monday, April 09, 2018
//--------------------------------------------------------
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
 
namespace vnxbqy.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);
            }
        }
 
    }
 
}