Skip to content

Commit 8a9d7ff

Browse files
committed
Create InterfaceGameObject as the script component for game objects.
1 parent 8ee344e commit 8a9d7ff

7 files changed

Lines changed: 209 additions & 5 deletions

File tree

OSVR-Unity/Assets/OSVRUnity/Prefabs/InterfaceCallback.prefab renamed to OSVR-Unity/Assets/OSVRUnity/Prefabs/InterfaceGameObject.prefab

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ GameObject:
1010
- 4: {fileID: 412524}
1111
- 114: {fileID: 11412524}
1212
m_Layer: 0
13-
m_Name: InterfaceCallback
13+
m_Name: InterfaceGameObject
1414
m_TagString: Untagged
1515
m_Icon: {fileID: 0}
1616
m_NavMeshLayer: 0
@@ -23,8 +23,8 @@ Transform:
2323
m_PrefabInternal: {fileID: 100100000}
2424
m_GameObject: {fileID: 112524}
2525
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
26-
m_LocalPosition: {x: -.100000001, y: 0, z: 0}
27-
m_LocalScale: {x: .100000001, y: .100000001, z: .100000001}
26+
m_LocalPosition: {x: 0, y: 0, z: 0}
27+
m_LocalScale: {x: 1, y: 1, z: 1}
2828
m_Children: []
2929
m_Father: {fileID: 0}
3030
m_RootOrder: 0
@@ -36,7 +36,7 @@ MonoBehaviour:
3636
m_GameObject: {fileID: 112524}
3737
m_Enabled: 1
3838
m_EditorHideFlags: 0
39-
m_Script: {fileID: 11500000, guid: 07e9520e0cc30554c9e6c2985af8c48f, type: 3}
39+
m_Script: {fileID: 11500000, guid: 2dbe481ddb829b04a8f8f4fa85065051, type: 3}
4040
m_Name:
4141
m_EditorClassIdentifier:
4242
path:

OSVR-Unity/Assets/OSVRUnity/Prefabs/InterfaceCallback.prefab.meta renamed to OSVR-Unity/Assets/OSVRUnity/Prefabs/InterfaceGameObject.prefab.meta

File renamed without changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* OSVR-Unity Connection
2+
*
3+
* <http://sensics.com/osvr>
4+
* Copyright 2014 Sensics, Inc.
5+
* All rights reserved.
6+
*
7+
* Final version intended to be licensed under Apache v2.0
8+
*/
9+
10+
using UnityEngine;
11+
12+
namespace OSVR
13+
{
14+
namespace Unity
15+
{
16+
public class GetParent
17+
{
18+
public static GameObject Get(GameObject go)
19+
{
20+
if (null == go || null == go.transform || null == go.transform.parent)
21+
{
22+
return null;
23+
}
24+
Transform parent = go.transform.parent;
25+
return parent.gameObject;
26+
}
27+
}
28+
}
29+
}

OSVR-Unity/Assets/OSVRUnity/src/GetParent.cs.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.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Unity
1616
/// <summary>
1717
/// OSVR Interface, supporting generic callbacks that provide the source path and a Unity-native datatype.
1818
/// </summary>
19-
public class InterfaceCallbacks : MonoBehaviour
19+
public class InterfaceCallbacks : ScriptableObject
2020
{
2121
/// <summary>
2222
/// The interface path you want to connect to.
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/* OSVR-Unity Connection
2+
*
3+
* <http://sensics.com/osvr>
4+
* Copyright 2014 Sensics, Inc.
5+
* All rights reserved.
6+
*
7+
* Final version intended to be licensed under Apache v2.0
8+
*/
9+
10+
using UnityEngine;
11+
using System.Collections;
12+
13+
namespace OSVR
14+
{
15+
namespace Unity
16+
{
17+
18+
/// <summary>
19+
/// A script component to add to a GameObject in order to access an interface, managing lifetime and centralizing the path specification.
20+
/// </summary>
21+
public class InterfaceGameObject : MonoBehaviour
22+
{
23+
/// <summary>
24+
/// The interface path you want to connect to.
25+
/// </summary>
26+
[Tooltip("The interface path you want to access. If left blank, the path of the nearest ancestor with a path will be used.")]
27+
public string path;
28+
29+
30+
public InterfaceCallbacks osvrInterface
31+
{
32+
get
33+
{
34+
Start();
35+
return iface;
36+
}
37+
}
38+
39+
protected InterfaceGameObject interfaceGameObject
40+
{
41+
get
42+
{
43+
return this;
44+
}
45+
}
46+
47+
#region Private implementation
48+
private InterfaceCallbacks iface;
49+
50+
private class PathHolder : MonoBehaviour
51+
{
52+
[HideInInspector]
53+
public string path;
54+
55+
PathHolder()
56+
{
57+
hideFlags = HideFlags.HideAndDontSave;
58+
}
59+
}
60+
#endregion
61+
62+
#region Methods for Derived Classes
63+
/// <summary>
64+
/// Call from your Awake method to advertise the presence or absence of a path specification on this game object.
65+
/// </summary>
66+
protected void AdvertisePath()
67+
{
68+
if (null != iface)
69+
{
70+
/// Already started.
71+
return;
72+
}
73+
74+
PathHolder holder = GetComponent<PathHolder>();
75+
if (path.Length > 0) {
76+
/// If we have a path, be sure we advertise it.
77+
if (null == holder) {
78+
holder = gameObject.AddComponent<PathHolder>();
79+
}
80+
holder.path = path;
81+
} else {
82+
/// Don't advertise a path that is empty
83+
if (null != holder) {
84+
Object.Destroy(holder);
85+
}
86+
}
87+
}
88+
89+
/// <summary>
90+
/// Call from your Start method
91+
/// </summary>
92+
protected void Start()
93+
{
94+
if (null != iface)
95+
{
96+
return;
97+
}
98+
AdvertisePath();
99+
GameObject go = this.gameObject;
100+
PathHolder holder = null;
101+
string usedPath = path;
102+
while (null != go && 0 == usedPath.Length)
103+
{
104+
holder = go.GetComponent<PathHolder>();
105+
if (null != holder)
106+
{
107+
usedPath = holder.path;
108+
//print("[OSVR] " + name + ": Found path " + usedPath + " in ancestor " + go.name);
109+
}
110+
go = GetParent.Get(go);
111+
}
112+
113+
if (0 == usedPath.Length)
114+
{
115+
Debug.LogError("[OSVR] Missing path for " + name + " - no path found in this object's InterfaceGameObject or any ancestor!");
116+
return;
117+
}
118+
119+
iface = ScriptableObject.CreateInstance<InterfaceCallbacks>();
120+
iface.path = usedPath;
121+
iface.Start();
122+
}
123+
124+
protected void Stop()
125+
{
126+
if (null != iface)
127+
{
128+
Object.Destroy(iface);
129+
iface = null;
130+
}
131+
PathHolder holder = GetComponent<PathHolder>();
132+
if (null != holder)
133+
{
134+
Object.Destroy(holder);
135+
}
136+
}
137+
#endregion
138+
139+
#region Event Methods
140+
void Awake()
141+
{
142+
AdvertisePath();
143+
}
144+
145+
void OnDestroy()
146+
{
147+
Stop();
148+
}
149+
150+
void OnApplicationQuit()
151+
{
152+
Stop();
153+
}
154+
155+
#endregion
156+
157+
}
158+
}
159+
}

OSVR-Unity/Assets/OSVRUnity/src/InterfaceGameObject.cs.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.

0 commit comments

Comments
 (0)