yyl
2025-06-09 b9751b2f076ee050fe5b685e91ae4fc4469b1015
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//--------------------------------------------------------
//    [Author]:           第二世界
//    [  Date ]:           Thursday, September 07, 2017
//--------------------------------------------------------
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
 
public class ScreenMoveTo : MonoBehaviour
{
 
    [SerializeField]
    LerpType m_LerpType = LerpType.Linear;
 
    [SerializeField]
    Vector2 m_Destination;
    public Vector2 destination {
        get { return m_Destination; }
        set { m_Destination = value; }
    }
 
    [SerializeField]
    float m_Duration = 0.5f;
    public float duration {
        get { return m_Duration; }
        set { m_Duration = value; }
    }
 
    [SerializeField]
    bool m_IsLocal = true;
    public bool isLocal {
        get { return m_IsLocal; }
    }
 
    bool disableOnEnd = true;
    float endTime = 0f;
 
    Vector3 startPosition = Vector3.zero;
    Vector3 startLocalPosition = Vector3.zero;
    Vector3 refPosition = Vector3.zero;
 
    bool end = false;
    Action endCallBack = null;
 
    public void Begin(bool _deActiveOnEnd = true)
    {
        endTime = Time.time + duration;
        end = false;
        disableOnEnd = _deActiveOnEnd;
        startPosition = this.transform.position;
        startLocalPosition = this.transform.localPosition;
        refPosition = Vector3.zero;
        endCallBack = null;
 
        this.enabled = true;
        if (!this.gameObject.activeInHierarchy)
        {
            this.SetActive(true);
        }
    }
 
    public void Begin(Action _callBack, bool _disableOnEnd = true)
    {
        endTime = Time.time + duration;
        end = false;
        disableOnEnd = _disableOnEnd;
        startPosition = this.transform.position;
        startLocalPosition = this.transform.localPosition;
        refPosition = Vector3.zero;
        endCallBack = _callBack;
 
        this.enabled = true;
        if (!this.gameObject.activeInHierarchy)
        {
            this.SetActive(true);
        }
    }
 
    private void LateUpdate()
    {
        switch (m_LerpType)
        {
            case LerpType.Linear:
                if (Time.time < endTime)
                {
                    var t = Mathf.Clamp01(1f - (endTime - Time.time) / m_Duration);
                    if (isLocal)
                    {
                        this.transform.localPosition = Vector3.Lerp(startLocalPosition, destination, t);
                    }
                    else
                    {
                        this.transform.position = Vector3.Lerp(startPosition, destination, t);
                    }
                }
                else
                {
                    end = true;
                    if (isLocal)
                    {
                        this.transform.localPosition = destination;
                    }
                    else
                    {
                        this.transform.position = destination;
                    }
                }
                break;
            case LerpType.Smooth:
 
                if (isLocal)
                {
                    if (Vector3.Distance(this.transform.localPosition, new Vector3(destination.x, destination.y)) > 1)
                    {
                        this.transform.localPosition = Vector3.SmoothDamp(this.transform.localPosition, destination, ref refPosition, duration);
                    }
                    else
                    {
                        this.transform.localPosition = destination;
                        end = true;
                    }
                }
                else
                {
                    if (Vector3.Distance(this.transform.position, new Vector3(destination.x, destination.y)) > 1)
                    {
                        this.transform.position = Vector3.SmoothDamp(this.transform.position, destination, ref refPosition, duration);
                    }
                    else
                    {
                        this.transform.position = destination;
                        end = true;
                    }
                }
                break;
        }
 
        if (end)
        {
            if (endCallBack != null)
            {
                endCallBack();
                endCallBack = null;
            }
        }
 
        if (end && disableOnEnd)
        {
            this.enabled = false;
        }
 
    }
 
 
    public enum LerpType
    {
        Linear,
        Smooth,
    }
 
}