Browse Source

AndyHan1001 Update FungusManager.cs

Use "double checked locking" algorithm to implement the singleton for "FungusManager" class, which can improve performance.
master
Steve Halliwell 5 years ago
parent
commit
ebc7d2bed9
  1. 25
      Assets/Fungus/Scripts/Components/FungusManager.cs

25
Assets/Fungus/Scripts/Components/FungusManager.cs

@ -19,9 +19,9 @@ namespace Fungus
#endif #endif
public sealed class FungusManager : MonoBehaviour public sealed class FungusManager : MonoBehaviour
{ {
static FungusManager instance; volatile static FungusManager instance; // The keyword "volatile" is friendly to the multi-thread.
static bool applicationIsQuitting = false; static bool applicationIsQuitting = false;
static object _lock = new object(); readonly static object _lock = new object(); // The keyword "readonly" is friendly to the multi-thread.
void Awake() void Awake()
{ {
@ -96,18 +96,23 @@ namespace Fungus
return null; return null;
} }
lock (_lock) // Use "double checked locking" algorithm to implement the singleton for this "FungusManager" class, which can improve performance.
if (instance == null)
{ {
if (instance == null) lock (_lock)
{ {
var go = new GameObject(); if (instance == null)
go.name = "FungusManager"; {
DontDestroyOnLoad(go); var go = new GameObject();
instance = go.AddComponent<FungusManager>(); go.name = "FungusManager";
} DontDestroyOnLoad(go);
instance = go.AddComponent<FungusManager>();
}
return instance; }
} }
return instance;
} }
} }

Loading…
Cancel
Save