-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoadingController.cs
78 lines (78 loc) · 1.97 KB
/
LoadingController.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoadingController : MonoBehaviour {
public static LoadingController instance;
public Transform barContent;
float progress = 0f;
int calls = 0;
int totalCallCount = 17;
// Use this for initializa
float nextPosition = -700;
float speed;
bool updating = false;
bool once = true;
bool set = false;
float smoothLerp = 0;
void Start () {
instance = this;
calls = 0;
barContent.localPosition = new Vector3 (-700, 0, 0);
StartCoroutine (StartUpdating ());
UserData.instance.updateScene = false;
}
// Update is called once per frame
void Update () {
if (updating) {
if (once) {
if (Mathf.Abs (barContent.localPosition.x - nextPosition) >= 20f) {
if (nextPosition > barContent.localPosition.x) {
if (smoothLerp < progress) {
smoothLerp += (Time.deltaTime * 0.5f);
}
barContent.localPosition = new Vector3 (Mathf.Lerp (-700, 0, smoothLerp), 0, 0);
}
} else {
if (set) {
StartCoroutine (StartUpdatingScene ());
once = false;
}
}
}
}
}
IEnumerator StartUpdating () {
yield return new WaitForSeconds (0.2f);
updating = true;
}
IEnumerator StartUpdatingScene () {
UserData.instance.updateScene = true;
yield return null;
}
public void SetTo (float percent) {
set = true;
if (percent != progress) {
if (percent > progress) {
progress = percent;
nextPosition = -700 + (progress * 700);
}
}
}
public void AddProgress () {
calls++;
CalculatePercentage ();
}
void CalculatePercentage () {
progress = (float) calls / (float) totalCallCount;
progress = Mathf.Clamp (progress, 0f, 1f);
nextPosition = -700 + (progress * 700);
}
IEnumerator MoveBar () {
yield return null;
for (float i = 0; i <= 1; i += Time.deltaTime / 1) {
transform.localPosition = new Vector3 (Mathf.Lerp (0, -700, i), 0, 0);
yield return null;
}
transform.localPosition = new Vector3 (-700, 0, 0);
}
}