wangguan
2020-12-29 452c75675679c44cc39b04bdb7d330d7c5c14d5c
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
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
 
namespace EnhancedScrollerDemos.SnappingDemo
{
    /// <summary>
    /// This class displays a popup panel when the user wins some credits
    /// </summary>
    public class PlayWin : MonoBehaviour
    {
        /// <summary>
        /// Cached transform to speed up processing
        /// </summary>
        private Transform _transform;
 
        /// <summary>
        /// The amount of time left before moving to the next state
        /// </summary>
        private float _timeLeft;
 
        /// <summary>
        /// This is the UI text element to show the score
        /// </summary>
        public Text scoreText;
 
        /// <summary>
        /// The amount of time to zoom in
        /// </summary>
        public float zoomTime;
 
        /// <summary>
        /// The amount of time to hold in place
        /// </summary>
        public float holdTime;
 
        /// <summary>
        /// The amount of time to disappear
        /// </summary>
        public float unZoomTime;
 
        void Awake()
        {
            // cache the transform and hide the panel
            _transform = transform;
            _transform.localScale = Vector3.zero;
        }
 
        /// <summary>
        /// This function intiates the playing of the panel
        /// </summary>
        /// <param name="score"></param>
        public void Play(int score)
        {
            // set the score text
            scoreText.text = string.Format("{0:n0}", score);
 
            // hide the panel to start
            transform.localScale = Vector3.zero;
 
            // reset the timer to the zoom time and start the play zoom
            _timeLeft = zoomTime;
            StartCoroutine(PlayZoom());
        }
 
        /// <summary>
        /// This function makes the panel larger over time
        /// </summary>
        /// <returns></returns>
        IEnumerator PlayZoom()
        {
            while (_timeLeft > 0)
            {
                // decrement the timer
                _timeLeft -= Time.deltaTime;
 
                // set the scale of the transform between hidden and showing based on the amount of time left
                _transform.localScale = Vector3.Lerp(Vector3.zero, Vector3.one, (zoomTime - _timeLeft) / zoomTime);
 
                yield return null;
            }
 
            // set the transform to full showing (in case our Lerp didn't quite finish at 1)
            transform.localScale = Vector3.one;
 
            // reset the timer to the hold time and play the hold
            _timeLeft = holdTime;
            StartCoroutine(PlayHold());
        }
 
        /// <summary>
        /// This function waits for a set amount of time
        /// </summary>
        /// <returns></returns>
        IEnumerator PlayHold()
        {
            while (_timeLeft > 0)
            {
                // decrement the timer
                _timeLeft -= Time.deltaTime;
                yield return null;
            }
 
            // reset the timer to the unzoom time and play the unzoom
            _timeLeft = unZoomTime;
            StartCoroutine(PlayUnZoom());
        }
 
        /// <summary>
        /// This function hides the panel over time
        /// </summary>
        /// <returns></returns>
        IEnumerator PlayUnZoom()
        {
            while (_timeLeft > 0)
            {
                // decrement the timer
                _timeLeft -= Time.deltaTime;
 
                // set the scale of the transform between showing and hidden based on the amount of time left
                _transform.localScale = Vector3.Lerp(Vector3.one, Vector3.zero, (unZoomTime - _timeLeft) / unZoomTime);
 
                yield return null;
            }
 
            // set the transform to full hidden (in case our Lerp didn't quite finish at 0)
            transform.localScale = Vector3.zero;
        }
    }
}