Skip to content

Commit 3233535

Browse files
committed
updates to the loadmanager from company project, can register enumerators as well
1 parent b8646e9 commit 3233535

19 files changed

+374
-199
lines changed

Assets/Test/TestFocus.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ private void Start()
1212
{
1313
LoadManager.CreateManager(1f / 30);
1414
LoadManager.instance.RegisterObject(gameObject);
15-
LoadManager.instance.StartLoading(() => Debug.Log("Loading Complete!"));
15+
LoadManager.instance.LoadRegistered(() => Debug.Log("Loading Complete!"));
1616
}
1717

1818
private void OnApplicationFocus(bool focus)
@@ -38,10 +38,5 @@ public IEnumerator LoadAssets()
3838
yield return null;
3939
}
4040
}
41-
42-
public void AssetsLoaded()
43-
{
44-
45-
}
4641
}
4742
}

Assets/Test/TestFunctionality.cs

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,13 @@ public IEnumerator LoadAssets()
3232

3333
yield return null;
3434
}
35-
36-
public void AssetsLoaded()
37-
{
38-
39-
}
4035
}
4136

4237
public void TestCorrectSteps()
4338
{
4439
TestCorrectStepsClass test = new TestCorrectStepsClass();
4540
LoadManager.instance.RegisterObject(test, STEPS_USED);
46-
LoadManager.instance.StartLoading(() => Assert.IsTrue(LoadManager.instance.progress == 1f));
41+
LoadManager.instance.LoadRegistered(() => Assert.IsTrue(LoadManager.instance.progress == 1f));
4742
}
4843

4944
//
@@ -60,24 +55,19 @@ public IEnumerator LoadAssets()
6055
{
6156
LoadManager.instance.IncrementLoadStep();
6257
}
63-
58+
6459
yield return null;
6560
}
6661

6762
yield return null;
6863
}
69-
70-
public void AssetsLoaded()
71-
{
72-
73-
}
7464
}
7565

7666
public void TestTooFewSteps()
7767
{
7868
TestTooFewStepsClass test = new TestTooFewStepsClass();
7969
LoadManager.instance.RegisterObject(test, STEPS_USED);
80-
LoadManager.instance.StartLoading(() => Assert.IsTrue(LoadManager.instance.progress == 1f));
70+
LoadManager.instance.LoadRegistered(() => Assert.IsTrue(LoadManager.instance.progress == 1f));
8171
}
8272

8373
//
@@ -97,18 +87,13 @@ public IEnumerator LoadAssets()
9787
LoadManager.instance.IncrementLoadStep();
9888
yield return null;
9989
}
100-
101-
public void AssetsLoaded()
102-
{
103-
104-
}
10590
}
10691

10792
public void TestTooManySteps()
10893
{
10994
TestTooManyStepsClass test = new TestTooManyStepsClass();
11095
LoadManager.instance.RegisterObject(test, STEPS_USED);
111-
LoadManager.instance.StartLoading(() => Assert.IsTrue(LoadManager.instance.progress == 1f));
96+
LoadManager.instance.LoadRegistered(() => Assert.IsTrue(LoadManager.instance.progress == 1f));
11297
}
11398

11499
//
@@ -127,19 +112,14 @@ public IEnumerator LoadAssets()
127112
yield return new ForceYield();
128113
}
129114
}
130-
131-
public void AssetsLoaded()
132-
{
133-
134-
}
135115
}
136116

137117
public void TestForceYield()
138118
{
139119
TestForceYieldClass test = new TestForceYieldClass();
140120
LoadManager.instance.secondsAllowedPerFrame = 100;
141121
LoadManager.instance.RegisterObject(test);
142-
LoadManager.instance.StartLoading(() => Assert.IsTrue(LoadManager.instance.progress == 1f));
122+
LoadManager.instance.LoadRegistered(() => Assert.IsTrue(LoadManager.instance.progress == 1f));
143123
}
144124
}
145125
}

Assets/UnityLoader/Example/AssetLoaderImplementer.cs

Lines changed: 53 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,78 +3,75 @@
33
using UnityEngine.UI;
44
using UnityGameLoader;
55

6-
public class AssetLoaderImplementer : MonoBehaviour, IAssetLoader
6+
namespace UnityGameLoaderExamples
77
{
8-
public Image image;
9-
public Text text;
10-
11-
private int _steps;
12-
13-
private void Start()
8+
public class AssetLoaderImplementer : MonoBehaviour, IAssetLoader
149
{
15-
image.color = Color.red;
16-
text.text = "0";
17-
}
10+
public Image image;
11+
public Text text;
1812

19-
public IEnumerator LoadAssets()
20-
{
21-
// We load our assets here and yield at appropriate frame breaks!
22-
image.color = Color.red;
23-
_steps = 0;
13+
private int _steps;
2414

25-
text.text = _steps.ToString();
15+
private void Start()
16+
{
17+
image.color = Color.red;
18+
text.text = "0";
19+
}
2620

27-
yield return null;
21+
public IEnumerator LoadAssets()
22+
{
23+
// We load our assets here and yield at appropriate frame breaks!
24+
image.color = Color.red;
25+
_steps = 0;
2826

29-
// We might take quite a bit of time to load stuff, who knows!
30-
float timeToLoad = Time.realtimeSinceStartup + Random.value * 2;
27+
text.text = _steps.ToString();
3128

32-
while (Time.realtimeSinceStartup < timeToLoad)
33-
{
34-
// Doing super important load stuff!
3529
yield return null;
36-
}
3730

38-
_steps++;
39-
text.text = _steps.ToString();
31+
// We might take quite a bit of time to load stuff, who knows!
32+
float timeToLoad = Time.realtimeSinceStartup + Random.value * 2;
4033

41-
// We can also call other enumerators to let them do their job.
42-
yield return LoadOtherAssetsFunction();
34+
while (Time.realtimeSinceStartup < timeToLoad)
35+
{
36+
// Doing super important load stuff!
37+
yield return null;
38+
}
4339

44-
_steps++;
45-
text.text = _steps.ToString();
40+
_steps++;
41+
text.text = _steps.ToString();
4642

47-
// There's no guarantee that yield will force a frame to pass. Maybe we want to? We can!
48-
yield return new ForceYield();
43+
// We can also call other enumerators to let them do their job.
44+
yield return LoadOtherAssetsFunction();
4945

50-
_steps++;
51-
text.text = _steps.ToString();
52-
}
46+
_steps++;
47+
text.text = _steps.ToString();
5348

54-
private IEnumerator LoadOtherAssetsFunction()
55-
{
56-
_steps++;
57-
text.text = _steps.ToString();
49+
// There's no guarantee that yield will force a frame to pass. Maybe we want to? We can!
50+
yield return new ForceYield();
5851

59-
// This can go as deep as we want
60-
yield return LoadYetAnotherFunction();
52+
_steps++;
53+
text.text = _steps.ToString();
54+
}
6155

62-
// And we'll resume when that enumerator completely finishes!
63-
_steps++;
64-
text.text = _steps.ToString();
65-
}
56+
private IEnumerator LoadOtherAssetsFunction()
57+
{
58+
_steps++;
59+
text.text = _steps.ToString();
6660

67-
private IEnumerator LoadYetAnotherFunction()
68-
{
69-
// Good times.
70-
yield return null;
71-
_steps++;
72-
text.text = _steps.ToString();
73-
}
61+
// This can go as deep as we want
62+
yield return LoadYetAnotherFunction();
7463

75-
public void AssetsLoaded()
76-
{
77-
// Any actions that need to happen after everything has been loaded can be placed here!
78-
image.color = Color.green;
64+
// And we'll resume when that enumerator completely finishes!
65+
_steps++;
66+
text.text = _steps.ToString();
67+
}
68+
69+
private IEnumerator LoadYetAnotherFunction()
70+
{
71+
// Good times.
72+
yield return null;
73+
_steps++;
74+
text.text = _steps.ToString();
75+
}
7976
}
80-
}
77+
}
Binary file not shown.

Assets/UnityLoader/Example/ExampleMultiplePhases.unity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/UnityLoader/Example/InteractWithLoadManager.cs renamed to Assets/UnityLoader/Example/InteractWithLoadManagerExample.cs

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
using UnityEngine;
1+
using System.Collections;
2+
using UnityEngine;
23
using UnityEngine.UI;
34
using UnityGameLoader;
45

56
namespace UnityGameLoaderExamples
67
{
7-
public class InteractWithLoadManager : MonoBehaviour
8+
public class InteractWithLoadManagerExample : MonoBehaviour
89
{
910
public Image loadProgress;
1011
public Text loadState;
@@ -14,7 +15,12 @@ public class InteractWithLoadManager : MonoBehaviour
1415
public Button pauseResumeButton;
1516
public Text pauseResumeText;
1617

18+
public Text enumText;
19+
1720
private LoadState _state;
21+
private bool _runRegisteredObjs = true;
22+
23+
private const int STEPS_FOR_ENUMERATOR = 11;
1824

1925
private enum LoadState
2026
{
@@ -56,8 +62,21 @@ public void LoadCancel()
5662
}
5763
else
5864
{
59-
LoadManager.instance.RegisterObjectDeep(gameObject);
60-
LoadManager.instance.StartLoading(OnLoadComplete);
65+
if (_runRegisteredObjs)
66+
{
67+
LoadManager.instance.RegisterEnumerator(EnumeratorToLoad(), STEPS_FOR_ENUMERATOR);
68+
LoadManager.instance.RegisterObjectDeep(gameObject);
69+
70+
// Can load all the registered objects
71+
LoadManager.instance.LoadRegistered(OnLoadComplete);
72+
}
73+
else
74+
{
75+
// Or we can load arbitrary enumerators where we step the progress counter ourselves. In this case
76+
// we know we'll run 11 steps (incremented in the enumerator)
77+
LoadManager.instance.LoadEnumerator(EnumeratorToLoad(), OnLoadComplete, STEPS_FOR_ENUMERATOR);
78+
}
79+
6180
loadState.text = "Loading...";
6281
pauseResumeButton.gameObject.SetActive(true);
6382
loadCancelText.text = "Cancel";
@@ -84,6 +103,11 @@ public void PauseResume()
84103
}
85104
}
86105

106+
public void LoadTypeChanged(bool runRegObjs)
107+
{
108+
_runRegisteredObjs = runRegObjs;
109+
}
110+
87111
private void OnLoadComplete()
88112
{
89113
loadProgress.fillAmount = LoadManager.instance.progress;
@@ -92,5 +116,43 @@ private void OnLoadComplete()
92116
loadState.text = "Load Complete!";
93117
pauseResumeButton.gameObject.SetActive(false);
94118
}
119+
120+
private IEnumerator EnumeratorToLoad()
121+
{
122+
enumText.color = Color.red;
123+
124+
for (int i = 0; i < 10; i++)
125+
{
126+
float runUntil = Time.realtimeSinceStartup + 1;
127+
enumText.text = i.ToString();
128+
129+
while (Time.realtimeSinceStartup < runUntil)
130+
{
131+
yield return null;
132+
}
133+
134+
LoadManager.instance.IncrementLoadStep();
135+
}
136+
137+
enumText.text = "10";
138+
139+
// Can call into other enumerators
140+
yield return SpinForSecond();
141+
142+
enumText.text = "Complete!";
143+
enumText.color = Color.green;
144+
}
145+
146+
private IEnumerator SpinForSecond()
147+
{
148+
float runUntil = Time.realtimeSinceStartup + 1;
149+
150+
while (Time.realtimeSinceStartup < runUntil)
151+
{
152+
yield return null;
153+
}
154+
155+
LoadManager.instance.IncrementLoadStep();
156+
}
95157
}
96158
}

0 commit comments

Comments
 (0)