Skip to content

Commit 2dfa566

Browse files
author
lawwong
committed
Remove redundent script in UnityEngineVRModule
1 parent 763dd4b commit 2dfa566

File tree

5 files changed

+110
-411
lines changed

5 files changed

+110
-411
lines changed

Assets/HTC.UnityPlugin/VRModule/Modules/UnityEngineVRModule.cs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
using UnityEngine;
44

5+
#if UNITY_2017_2_OR_NEWER
6+
using UnityEngine.XR;
7+
#else
8+
using XRSettings = UnityEngine.VR.VRSettings;
9+
using XRDevice = UnityEngine.VR.VRDevice;
10+
#endif
11+
512
namespace HTC.UnityPlugin.VRModuleManagement
613
{
714
public sealed partial class UnityEngineVRModule : VRModule.ModuleBase
@@ -103,38 +110,16 @@ public static class ButtonAxisID
103110
public static int Index(int i) { return s_ids[i]; }
104111
}
105112
#endif
106-
107-
#if UNITY_2017_2_OR_NEWER
108-
public override bool ShouldActiveModule() { return UnityEngine.XR.XRSettings.enabled; }
109-
#else
110-
public override bool ShouldActiveModule() { return UnityEngine.VR.VRSettings.enabled; }
111-
#endif
112-
113-
#if UNITY_5_6_OR_NEWER
114-
public override void OnActivated()
115-
{
116-
SaveTrackingSpaceType();
117-
UpdateTrackingSpaceType();
118-
}
119-
#endif
120-
121-
#if UNITY_5_6_OR_NEWER
122-
public override void OnDeactivated()
123-
{
124-
LoadTrackingSpaceType();
125-
}
126-
#endif
113+
public override bool ShouldActiveModule() { return XRSettings.enabled; }
127114

128115
public override void Update()
129116
{
130117
// set physics update rate to vr render rate
131118
if (VRModule.lockPhysicsUpdateRateToRenderFrequency && Time.timeScale > 0.0f)
132119
{
133120
// FIXME: VRDevice.refreshRate returns zero in Unity 5.6.0 or older version
134-
#if UNITY_2017_2_OR_NEWER
135-
Time.fixedDeltaTime = 1f / UnityEngine.XR.XRDevice.refreshRate;
136-
#elif UNITY_5_6_OR_NEWER
137-
Time.fixedDeltaTime = 1f / UnityEngine.VR.VRDevice.refreshRate;
121+
#if UNITY_5_6_OR_NEWER
122+
Time.fixedDeltaTime = 1f / XRDevice.refreshRate;
138123
#else
139124
Time.fixedDeltaTime = 1f / 90f;
140125
#endif

Assets/HTC.UnityPlugin/VRModule/Modules/UnityEngineVRModule_2017_1.cs

Lines changed: 70 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,85 @@
11
//========= Copyright 2016-2017, HTC Corporation. All rights reserved. ===========
22

3-
#if UNITY_2017_1_OR_NEWER && !UNITY_2017_2_OR_NEWER
3+
#if UNITY_2017_1_OR_NEWER
4+
45
using HTC.UnityPlugin.Utility;
56
using System.Collections.Generic;
67
using UnityEngine;
8+
9+
#if UNITY_2017_2_OR_NEWER
10+
using UnityEngine.XR;
11+
#else
712
using UnityEngine.VR;
13+
using XRSettings = UnityEngine.VR.VRSettings;
14+
using XRDevice = UnityEngine.VR.VRDevice;
15+
using XRNodeState = UnityEngine.VR.VRNodeState;
16+
using XRNode = UnityEngine.VR.VRNode;
17+
#endif
18+
819
#endif
920

1021
namespace HTC.UnityPlugin.VRModuleManagement
1122
{
1223
public sealed partial class UnityEngineVRModule : VRModule.ModuleBase
1324
{
14-
#if UNITY_2017_1_OR_NEWER && !UNITY_2017_2_OR_NEWER
25+
#if UNITY_2017_1_OR_NEWER
1526
private uint m_leftIndex = INVALID_DEVICE_INDEX;
1627
private uint m_rightIndex = INVALID_DEVICE_INDEX;
1728

1829
private Dictionary<ulong, uint> m_node2Index = new Dictionary<ulong, uint>();
1930
private bool[] m_nodeStatesValid = new bool[MAX_DEVICE_COUNT];
20-
private List<VRNodeState> m_nodeStateList = new List<VRNodeState>();
31+
private List<XRNodeState> m_nodeStateList = new List<XRNodeState>();
2132

2233
private IndexedSet<ulong> m_prevExistNodeUids = new IndexedSet<ulong>();
2334
private IndexedSet<ulong> m_currExistNodeUids = new IndexedSet<ulong>();
2435

36+
private TrackingSpaceType m_prevTrackingSpace;
37+
38+
public override void OnActivated()
39+
{
40+
m_prevTrackingSpace = XRDevice.GetTrackingSpaceType();
41+
UpdateTrackingSpaceType();
42+
}
43+
44+
public override void OnDeactivated()
45+
{
46+
XRDevice.SetTrackingSpaceType(m_prevTrackingSpace);
47+
}
48+
2549
public override uint GetLeftControllerDeviceIndex() { return m_leftIndex; }
2650

2751
public override uint GetRightControllerDeviceIndex() { return m_rightIndex; }
2852

29-
private bool IsTrackingDeviceNode(VRNodeState nodeState)
53+
public override void UpdateTrackingSpaceType()
54+
{
55+
switch (VRModule.trackingSpaceType)
56+
{
57+
case VRModuleTrackingSpaceType.Stationary:
58+
XRDevice.SetTrackingSpaceType(TrackingSpaceType.Stationary);
59+
break;
60+
case VRModuleTrackingSpaceType.RoomScale:
61+
XRDevice.SetTrackingSpaceType(TrackingSpaceType.RoomScale);
62+
break;
63+
}
64+
}
65+
66+
private bool IsTrackingDeviceNode(XRNodeState nodeState)
3067
{
3168
switch (nodeState.nodeType)
3269
{
33-
case VRNode.Head:
34-
case VRNode.RightHand:
35-
case VRNode.LeftHand:
36-
case VRNode.GameController:
37-
case VRNode.HardwareTracker:
38-
case VRNode.TrackingReference:
70+
case XRNode.Head:
71+
case XRNode.RightHand:
72+
case XRNode.LeftHand:
73+
case XRNode.GameController:
74+
case XRNode.HardwareTracker:
75+
case XRNode.TrackingReference:
3976
return true;
4077
default:
4178
return false;
4279
}
4380
}
4481

45-
private bool TryGetNodeDeviceIndex(VRNodeState nodeState, out uint deviceIndex)
82+
private bool TryGetNodeDeviceIndex(XRNodeState nodeState, out uint deviceIndex)
4683
{
4784
// only tracking certain type of node (some nodes share same uniqueID)
4885
if (!IsTrackingDeviceNode(nodeState)) { deviceIndex = 0; return false; }
@@ -54,7 +91,7 @@ private bool TryGetNodeDeviceIndex(VRNodeState nodeState, out uint deviceIndex)
5491

5592
var validIndexFound = false;
5693

57-
if (nodeState.nodeType == VRNode.Head)
94+
if (nodeState.nodeType == XRNode.Head)
5895
{
5996
if (m_nodeStatesValid[0])
6097
{
@@ -79,8 +116,8 @@ private bool TryGetNodeDeviceIndex(VRNodeState nodeState, out uint deviceIndex)
79116

80117
switch (nodeState.nodeType)
81118
{
82-
case VRNode.RightHand: m_rightIndex = i; break;
83-
case VRNode.LeftHand: m_leftIndex = i; break;
119+
case XRNode.RightHand: m_rightIndex = i; break;
120+
case XRNode.LeftHand: m_leftIndex = i; break;
84121
}
85122

86123
deviceIndex = i;
@@ -92,7 +129,7 @@ private bool TryGetNodeDeviceIndex(VRNodeState nodeState, out uint deviceIndex)
92129

93130
if (!validIndexFound)
94131
{
95-
Debug.LogWarning("[" + Time.frameCount + "] VRNode added, but device index out of range, drop the node id:" + nodeState.uniqueID.ToString("X8") + " type:" + nodeState.nodeType + " name:" + InputTracking.GetNodeName(nodeState.uniqueID) + " tracked=" + nodeState.tracked);
132+
Debug.LogWarning("[" + Time.frameCount + "] XRNode added, but device index out of range, drop the node id:" + nodeState.uniqueID.ToString("X8") + " type:" + nodeState.nodeType + " name:" + InputTracking.GetNodeName(nodeState.uniqueID) + " tracked=" + nodeState.tracked);
96133
return false;
97134
}
98135

@@ -102,7 +139,7 @@ private bool TryGetNodeDeviceIndex(VRNodeState nodeState, out uint deviceIndex)
102139
return true;
103140
}
104141

105-
private void RemoveNodeDeviceIndex(ulong uniqueID)
142+
private uint RemoveNodeDeviceIndex(ulong uniqueID)
106143
{
107144
var deviceIndex = INVALID_DEVICE_INDEX;
108145
if (m_node2Index.TryGetValue(uniqueID, out deviceIndex))
@@ -113,11 +150,13 @@ private void RemoveNodeDeviceIndex(ulong uniqueID)
113150
if (deviceIndex == m_rightIndex) { m_rightIndex = INVALID_DEVICE_INDEX; }
114151
if (deviceIndex == m_leftIndex) { m_leftIndex = INVALID_DEVICE_INDEX; }
115152
}
153+
154+
return deviceIndex;
116155
}
117156

118157
public override void UpdateDeviceState(IVRModuleDeviceState[] prevState, IVRModuleDeviceStateRW[] currState)
119158
{
120-
if (VRSettings.isDeviceActive && VRDevice.isPresent)
159+
if (XRSettings.isDeviceActive && XRDevice.isPresent)
121160
{
122161
InputTracking.GetNodeStates(m_nodeStateList);
123162
}
@@ -143,24 +182,24 @@ public override void UpdateDeviceState(IVRModuleDeviceState[] prevState, IVRModu
143182

144183
switch (m_nodeStateList[i].nodeType)
145184
{
146-
case VRNode.Head:
185+
case XRNode.Head:
147186
currDeviceState.deviceClass = VRModuleDeviceClass.HMD;
148187
break;
149-
case VRNode.RightHand:
188+
case XRNode.RightHand:
150189
currDeviceState.deviceClass = VRModuleDeviceClass.Controller;
151190
rightIndex = deviceIndex;
152191
break;
153-
case VRNode.LeftHand:
192+
case XRNode.LeftHand:
154193
currDeviceState.deviceClass = VRModuleDeviceClass.Controller;
155194
leftIndex = deviceIndex;
156195
break;
157-
case VRNode.GameController:
196+
case XRNode.GameController:
158197
currDeviceState.deviceClass = VRModuleDeviceClass.Controller;
159198
break;
160-
case VRNode.HardwareTracker:
199+
case XRNode.HardwareTracker:
161200
currDeviceState.deviceClass = VRModuleDeviceClass.GenericTracker;
162201
break;
163-
case VRNode.TrackingReference:
202+
case XRNode.TrackingReference:
164203
currDeviceState.deviceClass = VRModuleDeviceClass.TrackingReference;
165204
break;
166205
default:
@@ -172,9 +211,9 @@ public override void UpdateDeviceState(IVRModuleDeviceState[] prevState, IVRModu
172211
{
173212
// FIXME: getting wrong name in Unity 2017.1f1
174213
//currDeviceState.serialNumber = InputTracking.GetNodeName(m_nodeStateList[i].uniqueID) ?? string.Empty;
175-
currDeviceState.serialNumber = VRDevice.model + " " + m_nodeStateList[i].uniqueID.ToString("X8");
176-
currDeviceState.modelNumber = VRDevice.model + " " + m_nodeStateList[i].nodeType;
177-
currDeviceState.renderModelName = VRDevice.model + " " + m_nodeStateList[i].nodeType;
214+
currDeviceState.serialNumber = XRDevice.model + " " + m_nodeStateList[i].uniqueID.ToString("X8");
215+
currDeviceState.modelNumber = XRDevice.model + " " + m_nodeStateList[i].nodeType;
216+
currDeviceState.renderModelName = XRDevice.model + " " + m_nodeStateList[i].nodeType;
178217

179218
SetupKnownDeviceModel(currDeviceState);
180219
}
@@ -273,8 +312,11 @@ public override void UpdateDeviceState(IVRModuleDeviceState[] prevState, IVRModu
273312
// remove disconnected nodes
274313
for (int i = m_prevExistNodeUids.Count - 1; i >= 0; --i)
275314
{
276-
if (currState[i].isConnected) { currState[i].Reset(); }
277-
RemoveNodeDeviceIndex(m_prevExistNodeUids[i]);
315+
var removedIndex = RemoveNodeDeviceIndex(m_prevExistNodeUids[i]);
316+
if (VRModule.IsValidDeviceIndex(removedIndex))
317+
{
318+
currState[removedIndex].Reset();
319+
}
278320
}
279321

280322
var temp = m_prevExistNodeUids;

0 commit comments

Comments
 (0)