Skip to content

Commit cb1b5d8

Browse files
committed
Merge pull request #52 from OSVR/persistentSingleton
Made ClientKit a persistent singleton
2 parents c3c5dc0 + a056426 commit cb1b5d8

2 files changed

Lines changed: 39 additions & 13 deletions

File tree

OSVR-Unity/Assets/OSVRUnity/src/ClientKit.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public class ClientKit : MonoBehaviour
3131

3232
private OSVR.ClientKit.ClientContext contextObject;
3333

34+
/// Uses the Unity "Persistent Singleton" pattern, see http://unitypatterns.com/singletons/
35+
private static ClientKit _instance;
36+
3437
/// <summary>
3538
/// Use to access the single instance of this object/script in your game.
3639
/// </summary>
@@ -39,13 +42,19 @@ public static ClientKit instance
3942
{
4043
get
4144
{
42-
ClientKit candidate = GameObject.FindObjectOfType<ClientKit>();
43-
if (null == candidate)
45+
if(_instance == null)
4446
{
45-
Debug.LogError("OSVR Error: You need the ClientKit prefab in your game!!");
47+
_instance = GameObject.FindObjectOfType<ClientKit>();
48+
if (_instance == null)
49+
{
50+
Debug.LogError("OSVR Error: You need the ClientKit prefab in your game!!");
51+
}
52+
else
53+
{
54+
DontDestroyOnLoad(_instance.gameObject);
55+
}
4656
}
47-
return candidate;
48-
57+
return _instance;
4958
}
5059
}
5160

@@ -78,7 +87,20 @@ private void EnsureStarted()
7887
void Awake()
7988
{
8089
DLLSearchPathFixer.fix();
81-
DontDestroyOnLoad(gameObject);
90+
//if an instance of this singleton does not exist, set the instance to this object and make it persist
91+
if(_instance == null)
92+
{
93+
_instance = this;
94+
DontDestroyOnLoad(this);
95+
}
96+
else
97+
{
98+
//if an instance of this singleton already exists, destroy this one
99+
if(_instance != this)
100+
{
101+
Destroy(this.gameObject);
102+
}
103+
}
82104
}
83105
void Start()
84106
{

OSVR-Unity/Assets/OSVRUnity/src/VRHead.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class VRHead : MonoBehaviour
5454
private Camera _camera;
5555
private DeviceDescriptor _deviceDescriptor;
5656
private OsvrDistortion _distortionEffect;
57+
private DisplayInterface _displayInterface;
5758
private bool _initDisplayInterface = false;
5859
#endregion
5960

@@ -68,27 +69,30 @@ void Start()
6869
_distortionEffect.enabled = (viewMode == ViewMode.mono);
6970
}
7071

72+
_displayInterface = GetComponent<DisplayInterface>();
73+
7174
//update VRHead with info from the display interface if it has been initialized
7275
//it might not be initialized if it is still parsing a display json file
7376
//in that case, we will try to initialize asap in the update function
74-
if (GetComponent<DisplayInterface>().Initialized)
77+
if (_displayInterface != null && _displayInterface.Initialized)
7578
{
7679
UpdateDisplayInterface();
77-
}
80+
}
81+
//else initialize in Update()
7882
}
7983
#endregion
8084

8185
#region Loop
8286
void Update()
8387
{
84-
85-
if(!_initDisplayInterface && !GetComponent<DisplayInterface>().Initialized)
88+
89+
if (!_initDisplayInterface && !_displayInterface.Initialized)
8690
{
8791
//if the display configuration hasn't initialized, ping the DisplayInterface to retrieve it from the ClientKit
8892
//this would mean DisplayInterface was unable to retrieve that data in its Start() function.
89-
GetComponent<DisplayInterface>().ReadDisplayPath();
93+
_displayInterface.ReadDisplayPath();
9094
}
91-
else if (!_initDisplayInterface && GetComponent<DisplayInterface>().Initialized)
95+
else if (!_initDisplayInterface && _displayInterface.Initialized)
9296
{
9397
//once the DisplayInterface has initialized, meaning it has read data from the /display path which contains
9498
//display configuration, update the Camera and other settings with properties from the display configuration.
@@ -207,7 +211,7 @@ void Init()
207211
/// </summary>
208212
private void GetDeviceDescription()
209213
{
210-
_deviceDescriptor = GetComponent<DisplayInterface>().GetDeviceDescription();
214+
_deviceDescriptor = _displayInterface.GetDeviceDescription();
211215
if (_deviceDescriptor != null)
212216
{
213217
switch (_deviceDescriptor.DisplayMode)

0 commit comments

Comments
 (0)