Skip to content

Commit 176ce67

Browse files
committed
test cases written and bugs fixed to loadmanager
1 parent 724b5a7 commit 176ce67

File tree

11 files changed

+281
-5
lines changed

11 files changed

+281
-5
lines changed

Assets/Test.meta

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

Assets/Test/Test.unity

41.3 KB
Binary file not shown.

Assets/Test/Test.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/Test/TestFocus.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
using UnityEngine.Assertions.Comparers;
4+
using UnityLoader;
5+
6+
public class TestFocus : MonoBehaviour, IAssetLoader
7+
{
8+
public float loadDuration = 10;
9+
10+
private void Start()
11+
{
12+
LoadManager.CreateManager(1f/30);
13+
LoadManager.instance.RegisterObject(gameObject);
14+
LoadManager.instance.StartLoading(() => Debug.Log("Loading Complete!"));
15+
}
16+
17+
private void OnApplicationFocus(bool focus)
18+
{
19+
Debug.Log("Focus Change: " + focus);
20+
}
21+
22+
public IEnumerator LoadAssets()
23+
{
24+
float loadTime = Time.realtimeSinceStartup + loadDuration;
25+
26+
while (Time.realtimeSinceStartup < loadTime)
27+
{
28+
Debug.Log("Frame: " + Time.renderedFrameCount);
29+
30+
float delayTime = Time.realtimeSinceStartup + 1f / 30;
31+
32+
while (Time.realtimeSinceStartup < delayTime)
33+
{
34+
35+
}
36+
37+
yield return null;
38+
}
39+
}
40+
41+
public void AssetsLoaded()
42+
{
43+
44+
}
45+
}

Assets/Test/TestFocus.cs.meta

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

Assets/Test/TestFocus.unity

13.5 KB
Binary file not shown.

Assets/Test/TestFocus.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/Test/TestFunctionality.cs

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
using System.Reflection;
4+
using UnityEngine.Assertions;
5+
using UnityLoader;
6+
7+
namespace UnityLoaderTests
8+
{
9+
public class TestFunctionality : MonoBehaviour
10+
{
11+
public int targetFPS = 30;
12+
13+
private void Awake()
14+
{
15+
LoadManager.CreateManager(targetFPS);
16+
}
17+
18+
//
19+
// Test Correct Steps
20+
//
21+
22+
private const int STEPS_USED = 10;
23+
24+
public class TestCorrectStepsClass : IAssetLoader
25+
{
26+
public IEnumerator LoadAssets()
27+
{
28+
for (int i = 0; i < STEPS_USED; i++)
29+
{
30+
LoadManager.instance.IncrementLoadStep();
31+
yield return null;
32+
}
33+
34+
yield return null;
35+
}
36+
37+
public void AssetsLoaded()
38+
{
39+
40+
}
41+
}
42+
43+
public void TestCorrectSteps()
44+
{
45+
TestCorrectStepsClass test = new TestCorrectStepsClass();
46+
LoadManager.instance.RegisterObject(test, STEPS_USED);
47+
LoadManager.instance.StartLoading(() => Assert.IsTrue(LoadManager.instance.progress == 1f));
48+
}
49+
50+
//
51+
// Test Too Few Steps
52+
//
53+
54+
public class TestTooFewStepsClass : IAssetLoader
55+
{
56+
public IEnumerator LoadAssets()
57+
{
58+
for (int i = 0; i < STEPS_USED; i++)
59+
{
60+
if (i % 2 == 0)
61+
{
62+
LoadManager.instance.IncrementLoadStep();
63+
}
64+
65+
yield return null;
66+
}
67+
68+
yield return null;
69+
}
70+
71+
public void AssetsLoaded()
72+
{
73+
74+
}
75+
}
76+
77+
public void TestTooFewSteps()
78+
{
79+
TestTooFewStepsClass test = new TestTooFewStepsClass();
80+
LoadManager.instance.RegisterObject(test, STEPS_USED);
81+
LoadManager.instance.StartLoading(() => Assert.IsTrue(LoadManager.instance.progress == 1f));
82+
}
83+
84+
//
85+
// Test Too Few Steps
86+
//
87+
88+
public class TestTooManyStepsClass : IAssetLoader
89+
{
90+
public IEnumerator LoadAssets()
91+
{
92+
for (int i = 0; i < STEPS_USED; i++)
93+
{
94+
LoadManager.instance.IncrementLoadStep();
95+
yield return null;
96+
}
97+
98+
LoadManager.instance.IncrementLoadStep();
99+
yield return null;
100+
}
101+
102+
public void AssetsLoaded()
103+
{
104+
105+
}
106+
}
107+
108+
public void TestTooManySteps()
109+
{
110+
TestTooManyStepsClass test = new TestTooManyStepsClass();
111+
LoadManager.instance.RegisterObject(test, STEPS_USED);
112+
LoadManager.instance.StartLoading(() => Assert.IsTrue(LoadManager.instance.progress == 1f));
113+
}
114+
115+
//
116+
// Test Force Yield
117+
//
118+
119+
public class TestForceYieldClass : IAssetLoader
120+
{
121+
public IEnumerator LoadAssets()
122+
{
123+
float time = Time.realtimeSinceStartup + 10;
124+
125+
while (Time.realtimeSinceStartup < time)
126+
{
127+
Debug.Log("Frame: " + Time.renderedFrameCount);
128+
yield return new ForceYield();
129+
}
130+
}
131+
132+
public void AssetsLoaded()
133+
{
134+
135+
}
136+
}
137+
138+
public void TestForceYield()
139+
{
140+
TestForceYieldClass test = new TestForceYieldClass();
141+
LoadManager.instance.secondsAllowedPerFrame = 100;
142+
LoadManager.instance.RegisterObject(test);
143+
LoadManager.instance.StartLoading(() => Assert.IsTrue(LoadManager.instance.progress == 1f));
144+
}
145+
}
146+
}

Assets/Test/TestFunctionality.cs.meta

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

Assets/UnityLoader/Scripts/LoadManager.cs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ public class LoadManager : MonoBehaviour
1212
{
1313
#region public
1414
/// <summary>
15-
/// The amount of time the loader will excute before yield to a new frame.
15+
/// The amount of time the loader will execute before yield to a new frame.
1616
/// </summary>
1717
public float secondsAllowedPerFrame = 1f / 30;
1818

1919
/// <summary>
2020
/// Amount of memory System.GC.GetTotalMemory() can get to before yield to a new frame.
2121
/// </summary>
2222
[Header("WebGL")]
23-
public int memoryThresholdForYield = 128000000;
23+
public int memoryThresholdForYield = DEFAULT_WEBGL_MEMORY_THRESHOLD;
2424

2525
/// <summary>
2626
/// Verbose print out the timing of each loaded object.
@@ -50,15 +50,43 @@ public static LoadManager instance
5050
if (_instance == null)
5151
{
5252
LogErrorFormat(
53-
"No instance of {0} can be found! Add the {0} component to a GameObject.",
54-
typeof(LoadManager));
53+
"No instance of {0} can be found! Add the {0} component to a GameObject or invoke {0}.{1}().",
54+
typeof(LoadManager),
55+
((System.Action<float, int, bool>)CreateManager).Method.Name);
5556
}
5657
}
5758

5859
return _instance;
5960
}
6061
}
6162

63+
/// <summary>
64+
/// Creates an instance of a LoadManager. This can be done instead of having one precreated in the scene.
65+
/// </summary>
66+
/// <param name="secondsAllowedPerFrame">The amount of time the loader will execute before yield to a new frame.</param>
67+
/// <param name="webglMemoryThresholdForYield">Amount of memory System.GC.GetTotalMemory() can get to before yield to a new frame.</param>
68+
/// <param name="verboseLogging">Verbose print out the timing of each loaded object.</param>
69+
public static void CreateManager(
70+
float secondsAllowedPerFrame,
71+
int webglMemoryThresholdForYield = DEFAULT_WEBGL_MEMORY_THRESHOLD,
72+
bool verboseLogging = false)
73+
{
74+
if (_instance != null)
75+
{
76+
LogWarningFormat(
77+
"{0} already exists, called to {1} will be ignored!",
78+
typeof(LoadManager),
79+
System.Reflection.MethodBase.GetCurrentMethod().Name);
80+
81+
return;
82+
}
83+
84+
LoadManager newLoad = (new GameObject(CREATED_NAME)).AddComponent<LoadManager>();
85+
newLoad.secondsAllowedPerFrame = secondsAllowedPerFrame;
86+
newLoad.memoryThresholdForYield = webglMemoryThresholdForYield;
87+
newLoad.verboseLogging = verboseLogging;
88+
}
89+
6290
/// <summary>
6391
/// Register an object to be loaded later.
6492
/// </summary>
@@ -220,7 +248,7 @@ public void IncrementLoadStep()
220248
private int _currentStep;
221249

222250
private bool _loadingPaused;
223-
private bool _hasFocus;
251+
private bool _hasFocus = true;
224252
private bool _isLoading;
225253
private bool _originalRunInBackground;
226254

@@ -229,8 +257,11 @@ public void IncrementLoadStep()
229257
private static LoadManager _instance;
230258
private static bool _webglInitialized;
231259

260+
private const int DEFAULT_WEBGL_MEMORY_THRESHOLD = 128000000;
261+
private const float NO_FOCUS_SECONDS_PER_FRAME = 1;
232262
private const int ADDITIONAL_STEPS = 2;
233263
private const string LOG_HEADER = "[Unity Loader]";
264+
private const string CREATED_NAME = "[Unity Loader]";
234265
private const string METHOD_INVOKE_DURING_LOADING_WARNING = "{0}.{1} invoked in the middle of a load! This isn't allowed. Invoke after the load finishes.";
235266
private const string METHOD_INVOKE_NOT_LOADING_WARNING = "{0}.{1} invoked when not loading! This will be ignored.";
236267

@@ -432,6 +463,11 @@ private bool ShouldYield(float frameStartTime)
432463
{
433464
if (!Application.isEditor && !_hasFocus)
434465
{
466+
if (Time.realtimeSinceStartup - frameStartTime >= NO_FOCUS_SECONDS_PER_FRAME)
467+
{
468+
return true;
469+
}
470+
435471
// We don't have focus, go nuts.
436472
return false;
437473
}
36 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)