1
1
using System . Collections ;
2
2
using UnityEngine ;
3
3
using System . Collections . Generic ;
4
+
5
+ #if UNITY_WEBGL
4
6
using System . Runtime . InteropServices ;
7
+ #endif
5
8
6
9
namespace UnityGameLoader
7
10
{
@@ -11,6 +14,7 @@ namespace UnityGameLoader
11
14
public class LoadManager : MonoBehaviour
12
15
{
13
16
#region public
17
+
14
18
/// <summary>
15
19
/// The amount of time the loader will execute before yield to a new frame.
16
20
/// </summary>
@@ -33,7 +37,7 @@ public class LoadManager : MonoBehaviour
33
37
/// </summary>
34
38
public float progress
35
39
{
36
- get { return ( float ) _currentStep / _totalSteps ; }
40
+ get { return ( float ) _currentStep / _totalSteps ; }
37
41
}
38
42
39
43
/// <summary>
@@ -52,7 +56,7 @@ public static LoadManager instance
52
56
LogErrorFormat (
53
57
"No instance of {0} can be found! Add the {0} component to a GameObject or invoke {0}.{1}()." ,
54
58
typeof ( LoadManager ) ,
55
- ( ( System . Action < float , int , bool > ) CreateManager ) . Method . Name ) ;
59
+ ( ( System . Action < float , int , bool > ) CreateManager ) . Method . Name ) ;
56
60
}
57
61
}
58
62
@@ -202,7 +206,7 @@ public void LoadRegistered(System.Action onLoadComplete)
202
206
onLoadComplete ( ) ;
203
207
}
204
208
}
205
- ,
209
+ ,
206
210
stepsFromLoads ) ) ;
207
211
}
208
212
@@ -311,6 +315,7 @@ public void IncrementLoadStep()
311
315
}
312
316
313
317
#endregion
318
+
314
319
#region private
315
320
316
321
private int _totalSteps ;
@@ -331,8 +336,12 @@ public void IncrementLoadStep()
331
336
private const int DEFAULT_LOADING_STEPS = 1 ;
332
337
private const string LOG_HEADER = "[Unity Loader]" ;
333
338
private const string CREATED_NAME = "[Unity Loader]" ;
334
- 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." ;
335
- private const string METHOD_INVOKE_NOT_LOADING_WARNING = "{0}.{1} invoked when not loading! This will be ignored." ;
339
+
340
+ private const string METHOD_INVOKE_DURING_LOADING_WARNING =
341
+ "{0}.{1} invoked in the middle of a load! This isn't allowed. Invoke after the load finishes." ;
342
+
343
+ private const string METHOD_INVOKE_NOT_LOADING_WARNING =
344
+ "{0}.{1} invoked when not loading! This will be ignored." ;
336
345
337
346
private struct LoaderStep
338
347
{
@@ -346,12 +355,13 @@ public LoaderStep(IEnumerator enumerator, int additionalSteps)
346
355
}
347
356
}
348
357
358
+ #if UNITY_WEBGL
349
359
[ DllImport ( "__Internal" , EntryPoint = "ulInitialize" ) ]
350
360
private static extern void JSInitialize ( ) ;
351
361
352
362
[ DllImport ( "__Internal" , EntryPoint = "ulIsTabActive" ) ]
353
363
private static extern bool JSIsTabActive ( ) ;
354
-
364
+ #endif
355
365
private void Awake ( )
356
366
{
357
367
if ( _instance != null )
@@ -365,11 +375,13 @@ private void Awake()
365
375
366
376
_instance = this ;
367
377
368
- if ( Application . platform == RuntimePlatform . WebGLPlayer && ! Application . isEditor && ! _webglInitialized )
378
+ #if UNITY_WEBGL
379
+ if ( ! Application . isEditor && ! _webglInitialized )
369
380
{
370
381
JSInitialize ( ) ;
371
382
_webglInitialized = true ;
372
383
}
384
+ #endif
373
385
}
374
386
375
387
private void OnApplicationFocus ( bool focus )
@@ -457,7 +469,7 @@ private IEnumerator LoadRegisteredEnumerators()
457
469
{
458
470
LogWarningFormat (
459
471
"Progress step count mismatch! Expecting {0} to be invoked the same number supplied during registration! Fixing." ,
460
- ( ( System . Action ) IncrementLoadStep ) . Method . Name ) ;
472
+ ( ( System . Action ) IncrementLoadStep ) . Method . Name ) ;
461
473
462
474
_currentStep = preEnumSteps + _loaders [ i ] . additionalSteps ;
463
475
}
@@ -466,15 +478,15 @@ private IEnumerator LoadRegisteredEnumerators()
466
478
467
479
if ( verboseLogging )
468
480
{
469
- LogFormat ( "Loading {0} - Time: {1}" , _loaders [ i ] . enumerator , Time . realtimeSinceStartup - assetLoadTimeStart ) ;
481
+ LogFormat ( "Loading {0} - Time: {1}" , _loaders [ i ] . enumerator ,
482
+ Time . realtimeSinceStartup - assetLoadTimeStart ) ;
470
483
}
471
484
}
472
485
}
473
486
474
487
private bool ShouldYield ( float frameStartTime )
475
488
{
476
- if ( Application . platform == RuntimePlatform . WebGLPlayer )
477
- {
489
+ #if UNITY_WEBGL
478
490
if ( ! Application . isEditor )
479
491
{
480
492
if ( System . GC . GetTotalMemory ( false ) > memoryThresholdForYield )
@@ -492,20 +504,18 @@ private bool ShouldYield(float frameStartTime)
492
504
return false ;
493
505
}
494
506
}
495
- }
496
- else
507
+ #else
508
+ if ( ! Application . isEditor && ! _hasFocus )
497
509
{
498
- if ( ! Application . isEditor && ! _hasFocus )
510
+ if ( Time . realtimeSinceStartup - frameStartTime >= NO_FOCUS_SECONDS_PER_FRAME )
499
511
{
500
- if ( Time . realtimeSinceStartup - frameStartTime >= NO_FOCUS_SECONDS_PER_FRAME )
501
- {
502
- return true ;
503
- }
504
-
505
- // We don't have focus, go nuts.
506
- return false ;
512
+ return true ;
507
513
}
514
+
515
+ // We don't have focus, go nuts.
516
+ return false ;
508
517
}
518
+ #endif
509
519
510
520
if ( ( Time . realtimeSinceStartup - frameStartTime ) >= secondsAllowedPerFrame )
511
521
{
@@ -532,4 +542,4 @@ private static void LogErrorFormat(string msg, params object[] args)
532
542
533
543
#endregion
534
544
}
535
- }
545
+ }
0 commit comments