/
LoadingScreen.cs
155 lines (128 loc) · 3.94 KB
/
LoadingScreen.cs
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
// Point Cloud Binary Viewer DX11 for runtime parsing
// http://unitycoder.com
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using TMPro;
// class created for RuntimeViewerDX11.cs
// namespace PointCloudRuntimeViewer {
//internal
class LoadingScreen : MonoBehaviour {
/// <summary>
/// progress bar
/// </summary>
public Slider slider;
/// <summary>
/// status text, TextMeshPro Object
/// </summary>
public TMP_Text statusText;
private bool disablePending = false;
private float pendingSeconds;
private bool newStatusPending = false;
private string newStatusText;
/// <summary>
/// progress bar fill. Can be found under FillArea
/// </summary>
public Image barColorImage;
public GameObject loadingPanel;
/// <summary>
/// Target progress bar value
/// </summary>
private float targetProgress;
/// <summary>
/// speed at wich the progress vbar moves towards target progress. 0.5 means it takes 2 seconds to get from 0 to full. So 1/fillspeed = seconds
/// </summary>
public float fillSpeed = 0.5f;
/// <summary>
/// Default green color for the progress bar
/// </summary>
private Color defaultColor = new Color(0, 255, 0);
private void OnDisable() {
targetProgress = 0;
slider.value = 0;
statusText.text = "None";
barColorImage.color = defaultColor;
}
private void Start() {
// resets all aspects of the loading screen
targetProgress = 0;
slider.value = 0;
barColorImage.color = defaultColor;
statusText.text = "None";
}
void Update() {
// Moves slider towards target progress based on speed and delta time
if (slider.value < targetProgress) {
slider.value += fillSpeed * Time.deltaTime;
} else if (slider.value == 1) {
// doesn't need to do anything at 100% this jsut dispalys finished
//SetStatusText("Finished");
}
if (newStatusPending)
SetStatusText(newStatusText);
if (disablePending)
DisableAfterSeconds(pendingSeconds);
}
/// <summary>
/// This function sets the targetprogress to the given value which can range from 0 to 1
/// </summary>
/// <param name="Value"></param>
public void SetTargetProgressBarValue(float Value) {
targetProgress = Value;
}
/// <summary>
/// Use this function to set a new status text on the loading screen
/// </summary>
/// <param name="status"></param>
public void SetStatusText(string status) {
statusText.text = status;
}
/// <summary>
/// TextMeshPro UI objects cant be modified from a thread, so to circumvent that this function
/// sets a bool to signal the loading Screen to update the status text in the next Update() call
/// </summary>
/// <param name="statusString"></param>
public void SetStatusTextThread(string statusString) {
newStatusText = statusString;
newStatusPending = true;
}
/// <summary>
/// Sets progressbar fill color
/// </summary>
/// <param name="newColor"></param>
public void SetBarFillColor(Color newColor) {
barColorImage.color = newColor;
}
/// <summary>
/// Use this function to start a coroutine that disables the Loading Screen after 'x' amount of seconds
/// </summary>
/// <param name="seconds"></param>
public void DisableAfterSeconds(float seconds) {
StartCoroutine(WaitSeconds(seconds));
}
/// <summary>
/// UI objects cant be modified from a thread, so to circumvent that this function
/// sets a bool to signal the loading Screen to disable itself
/// </summary>
/// <param name="seconds"></param>
public void DisableAfterSecondsThread(float seconds) {
disablePending = true;
pendingSeconds = seconds;
}
/// <summary>
/// Enables the loading screen.
/// </summary>
public void EnableLoadingScreen() {
loadingPanel.gameObject.SetActive(true);
}
/// <summary>
/// IEnumerator for coroutine that disables the Loading Screen after X amount of seconds
/// </summary>
/// <param name="seconds"></param>
/// <returns></returns>
private IEnumerator WaitSeconds(float seconds) {
yield return new WaitForSeconds(seconds);
loadingPanel.SetActive(false);
}
}
//}