Skip to content

Commit 2f80d01

Browse files
committed
Change how the ClientKit object works: use properties.
1 parent 4e47ee7 commit 2f80d01

4 files changed

Lines changed: 37 additions & 47 deletions

File tree

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

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,56 +18,75 @@ public class ClientKit : MonoBehaviour
1818
[Tooltip("A string uniquely identifying your application, in reverse domain-name format.")]
1919
public string AppID;
2020

21-
private OSVR.ClientKit.ClientContext context;
21+
private OSVR.ClientKit.ClientContext contextObject;
2222

2323
/// <summary>
24-
/// Use to find the single instance of this object/script in your game.
24+
/// Use to access the single instance of this object/script in your game.
2525
/// </summary>
2626
/// <returns>The instance, or null in case of error</returns>
27-
public static ClientKit Get()
27+
public static ClientKit instance
2828
{
29-
ClientKit candidate = GameObject.FindObjectOfType<ClientKit>();
30-
if (null == candidate)
29+
get
3130
{
32-
Debug.LogError("You need the ClientKit prefab!");
31+
ClientKit candidate = GameObject.FindObjectOfType<ClientKit>();
32+
if (null == candidate)
33+
{
34+
Debug.LogError("OSVR Error: You need the ClientKit prefab in your game!!");
35+
}
36+
return candidate;
37+
38+
}
39+
}
40+
41+
/// <summary>
42+
/// Access the underlying Managed-OSVR client context object.
43+
/// </summary>
44+
public OSVR.ClientKit.ClientContext context
45+
{
46+
get
47+
{
48+
EnsureStarted();
49+
return contextObject;
3350
}
34-
return candidate;
3551
}
3652

37-
public OSVR.ClientKit.ClientContext GetContext()
53+
private void EnsureStarted()
3854
{
39-
if (context == null)
55+
if (contextObject == null)
4056
{
4157
if (0 == AppID.Length)
4258
{
4359
Debug.LogError("OSVR ClientKit instance needs AppID set to a reverse-order DNS name! Using dummy name...");
4460
AppID = "org.opengoggles.osvr-unity.dummy";
4561
}
4662
Debug.Log("Starting OSVR with app ID: " + AppID);
47-
context = new OSVR.ClientKit.ClientContext(AppID, 0);
63+
contextObject = new OSVR.ClientKit.ClientContext(AppID, 0);
4864
}
49-
return context;
5065
}
5166

67+
/// <summary>
68+
/// Static constructor that enhances the DLL search path to ensure dependent native dlls are found.
69+
/// </summary>
5270
static ClientKit()
5371
{
5472
DLLSearchPathFixer.fix();
5573
}
5674

5775
void Start()
5876
{
59-
GetContext();
77+
EnsureStarted();
6078
}
6179

6280
void FixedUpdate()
6381
{
64-
context.update();
82+
contextObject.update();
6583
}
6684

6785
void OnDestroy()
6886
{
69-
Debug.Log("Disconnecting from OSVR server.");
70-
context = null;
87+
Debug.Log("Shutting down OSVR.");
88+
contextObject.Dispose ();
89+
contextObject = null;
7190
}
7291
}
7392
}

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ public class OrientationInterface : MonoBehaviour
2626
/// </summary>
2727
public string path;
2828

29-
/// <summary>
30-
/// This should be a reference to the single ClientKit instance in your project.
31-
/// </summary>
32-
private ClientKit clientKit;
33-
3429
private OSVR.ClientKit.Interface iface;
3530
private OSVR.ClientKit.OrientationCallback cb;
3631

@@ -43,12 +38,7 @@ void Start()
4338
return;
4439
}
4540

46-
if (null == clientKit)
47-
{
48-
clientKit = OSVR.Unity.ClientKit.Get();
49-
}
50-
51-
iface = clientKit.GetContext().getInterface(path);
41+
iface = OSVR.Unity.ClientKit.instance.context.getInterface(path);
5242
cb = new OSVR.ClientKit.OrientationCallback(callback);
5343
iface.registerCallback(cb, IntPtr.Zero);
5444
}

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ public class PoseInterface : MonoBehaviour
2626
/// </summary>
2727
public string path;
2828

29-
/// <summary>
30-
/// This should be a reference to the single ClientKit instance in your project.
31-
/// </summary>
32-
private ClientKit clientKit;
33-
3429
private OSVR.ClientKit.Interface iface;
3530
private OSVR.ClientKit.PoseCallback cb;
3631

@@ -42,12 +37,7 @@ void Start()
4237
return;
4338
}
4439

45-
if (null == clientKit)
46-
{
47-
clientKit = OSVR.Unity.ClientKit.Get();
48-
}
49-
50-
iface = clientKit.GetContext().getInterface(path);
40+
iface = OSVR.Unity.ClientKit.instance.context.getInterface(path);
5141
cb = new OSVR.ClientKit.PoseCallback(callback);
5242
iface.registerCallback(cb, IntPtr.Zero);
5343
}

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ public class PositionInterface : MonoBehaviour
2626
/// </summary>
2727
public string path;
2828

29-
/// <summary>
30-
/// This should be a reference to the single ClientKit instance in your project.
31-
/// </summary>
32-
private ClientKit clientKit;
33-
3429
private OSVR.ClientKit.Interface iface;
3530
private OSVR.ClientKit.PositionCallback cb;
3631

@@ -43,11 +38,7 @@ void Start()
4338
return;
4439
}
4540

46-
if (null == clientKit)
47-
{
48-
clientKit = OSVR.Unity.ClientKit.Get();
49-
}
50-
iface = clientKit.GetContext().getInterface(path);
41+
iface = OSVR.Unity.ClientKit.instance.context.getInterface(path);
5142
cb = new OSVR.ClientKit.PositionCallback(callback);
5243
iface.registerCallback(cb, IntPtr.Zero);
5344
}

0 commit comments

Comments
 (0)