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
//--------------------------------------------------------
//    [Author]:           第二世界
//    [  Date ]:           Thursday, September 07, 2017
//--------------------------------------------------------
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
 
 
public class ScreenDiffuseMove : MonoBehaviour
{
 
    [SerializeField] DiffuseType m_MoveType = DiffuseType.MoveUp;
    public DiffuseType moveType { get { return m_MoveType; } }
 
    [SerializeField] float m_Speed = 10f;
    public float speed {
        get { return m_Speed; }
        set { m_Speed = value; }
    }
 
    [SerializeField] TweenCurve m_SpeedRatioCurve;
    [SerializeField] float m_Duration = 1f;
    public float duration {
        get { return m_Duration; }
        set { m_Duration = value; }
    }
 
    [SerializeField] float m_Delay = 0f;
    public float delay {
        get { return m_Delay; }
        set { m_Delay = value; }
    }
 
    [SerializeField] bool m_IsLocal = true;
    public bool isLocal {
        get { return m_IsLocal; }
    }
 
    float beginTime = 0f;
    float endTime = 0f;
    Vector2 direction = Vector2.up;
    bool disableOnEnd = true;
 
    public void Begin(bool _disableOnEnd = true)
    {
        beginTime = Time.time + delay;
        endTime = Time.time + delay + duration;
        disableOnEnd = _disableOnEnd;
 
        switch (m_MoveType)
        {
            case DiffuseType.MoveUp:
                direction = Vector2.up * m_Speed;
                break;
            case DiffuseType.MoveDown:
                direction = Vector2.down * m_Speed;
                break;
            case DiffuseType.MoveLeft:
                direction = Vector2.left * m_Speed;
                break;
            case DiffuseType.MoveRight:
                direction = Vector2.right * m_Speed;
                break;
            case DiffuseType.RelativePosition:
                direction = new Vector2(this.transform.position.x, this.transform.position.y).normalized * m_Speed;
                break;
            case DiffuseType.ReversalRelativePosition:
                direction = new Vector2(-this.transform.position.x, this.transform.position.y).normalized * m_Speed;
                break;
        }
 
        this.enabled = true;
        if (!this.gameObject.activeInHierarchy)
        {
            this.SetActive(true);
        }
 
    }
 
    public void Begin(Vector3 _direction, bool _disableOnEnd = true)
    {
        beginTime = Time.time + delay;
        endTime = Time.time + delay + duration;
        disableOnEnd = _disableOnEnd;
 
        switch (m_MoveType)
        {
            case DiffuseType.MoveUp:
                direction = Vector2.up * m_Speed;
                break;
            case DiffuseType.MoveDown:
                direction = Vector2.down * m_Speed;
                break;
            case DiffuseType.MoveLeft:
                direction = Vector2.left * m_Speed;
                break;
            case DiffuseType.MoveRight:
                direction = Vector2.right * m_Speed;
                break;
            case DiffuseType.RelativePosition:
                direction = new Vector2(_direction.x, _direction.y).normalized * m_Speed;
                break;
            case DiffuseType.ReversalRelativePosition:
                direction = new Vector2(_direction.x, -_direction.y).normalized * m_Speed;
                break;
        }
 
        this.enabled = true;
        if (!this.gameObject.activeInHierarchy)
        {
            this.SetActive(true);
        }
    }
 
    private void LateUpdate()
    {
        if (Time.time < beginTime)
        {
            return;
        }
 
        if (Time.time < endTime)
        {
            var delta = direction * Time.deltaTime * m_SpeedRatioCurve.Evaluate((Time.time - beginTime) / duration);
            if (isLocal)
            {
                this.transform.localPosition += new Vector3(delta.x, delta.y, 0);
            }
            else
            {
                this.transform.position += new Vector3(delta.x, delta.y, 0);
            }
        }
        else
        {
            if (disableOnEnd)
            {
                this.enabled = false;
            }
        }
    }
 
 
    public enum DiffuseType
    {
        MoveUp,
        MoveDown,
        MoveLeft,
        MoveRight,
        RelativePosition,
        ReversalRelativePosition,
    }
}