HeadsetDemoManager.cs
6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//-----------------------------------------------------------------------
// <copyright file="HeadsetDemoManager.cs" company="Google Inc.">
// Copyright 2017 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//-----------------------------------------------------------------------
namespace GoogleVR.HelloVR
{
using System.Collections;
using UnityEngine;
/// <summary>Demonstrates the use of GvrHeadset events and APIs.</summary>
public class HeadsetDemoManager : MonoBehaviour
{
/// <summary>A visual representation of the Gvr Api's Safety Cylinder.</summary>
public GameObject safetyRing;
/// <summary>If `true`, this class logs status messages. If `false`, it does not.</summary>
public bool enableDebugLog = false;
private WaitForSeconds waitFourSeconds = new WaitForSeconds(4);
#region STANDALONE_DELEGATES
/// <summary>An event which triggers when the safety region is entered or exited.</summary>
/// <param name="enter">
/// Value `true` if the safety region is being entered, or `false` if the safety region is
/// being exited.
/// </param>
public void OnSafetyRegionEvent(bool enter)
{
if (enableDebugLog)
{
Debug.Log("SafetyRegionEvent: " + (enter ? "enter" : "exit"));
}
}
/// <summary>An event which triggers when a Recenter occurs.</summary>
/// <param name="recenterType">The type of the last recenter.</param>
/// <param name="recenterFlags">The flag context of the last recenter.</param>
/// <param name="recenteredPosition">The positional reference of the last recenter.</param>
/// <param name="recenteredOrientation">The rotation reference of the last recenter.</param>
public void OnRecenterEvent(GvrRecenterEventType recenterType,
GvrRecenterFlags recenterFlags,
Vector3 recenteredPosition,
Quaternion recenteredOrientation)
{
if (enableDebugLog)
{
Debug.Log(string.Format(
"RecenterEvent: Type {0}, flags {1}\nPosition: {2}, Rotation: {3}",
recenterType, recenterFlags, recenteredPosition, recenteredOrientation));
}
}
#endregion // STANDALONE_DELEGATES
/// <summary>Prints the floor height to console.</summary>
public void FindFloorHeight()
{
float floorHeight = 0.0f;
bool success = GvrHeadset.TryGetFloorHeight(ref floorHeight);
if (enableDebugLog)
{
Debug.Log("Floor height success " + success + "; value " + floorHeight);
}
}
/// <summary>
/// Prints the reference transformation as of the last recenter to console.
/// </summary>
public void FindRecenterTransform()
{
Vector3 position = Vector3.zero;
Quaternion rotation = Quaternion.identity;
bool success = GvrHeadset.TryGetRecenterTransform(ref position, ref rotation);
if (enableDebugLog)
{
Debug.Log("Recenter transform success " + success + "; value " + position + "; "
+ rotation);
}
}
/// <summary>Prints the safety region's type to console.</summary>
public void FindSafetyRegionType()
{
GvrSafetyRegionType safetyType = GvrSafetyRegionType.None;
bool success = GvrHeadset.TryGetSafetyRegionType(ref safetyType);
if (enableDebugLog)
{
Debug.Log("Safety region type success " + success + "; value " + safetyType);
}
}
/// <summary>Prints the safety region's inner radius to console.</summary>
public void FindSafetyInnerRadius()
{
float innerRadius = -1.0f;
bool success = GvrHeadset.TryGetSafetyCylinderInnerRadius(ref innerRadius);
if (enableDebugLog)
{
Debug.Log("Safety region inner radius success " + success + "; value "
+ innerRadius);
}
// Don't activate the safety cylinder visual until the radius is a reasonable value.
if (innerRadius > 0.1f && safetyRing != null)
{
safetyRing.SetActive(true);
safetyRing.transform.localScale = new Vector3(innerRadius, 1, innerRadius);
}
}
/// <summary>Prints the safety region's outer radius to console.</summary>
public void FindSafetyOuterRadius()
{
float outerRadius = -1.0f;
bool success = GvrHeadset.TryGetSafetyCylinderOuterRadius(ref outerRadius);
if (enableDebugLog)
{
Debug.Log("Safety region outer radius success " + success + "; value " +
outerRadius);
}
}
private void OnEnable()
{
if (safetyRing != null)
{
safetyRing.SetActive(false);
}
if (!GvrHeadset.SupportsPositionalTracking)
{
return;
}
GvrHeadset.OnSafetyRegionChange += OnSafetyRegionEvent;
GvrHeadset.OnRecenter += OnRecenterEvent;
if (enableDebugLog)
{
StartCoroutine(StatusUpdateLoop());
}
}
private void OnDisable()
{
if (!GvrHeadset.SupportsPositionalTracking)
{
return;
}
GvrHeadset.OnSafetyRegionChange -= OnSafetyRegionEvent;
GvrHeadset.OnRecenter -= OnRecenterEvent;
}
private void Start()
{
if (enableDebugLog)
{
Debug.Log("Device supports positional tracking: "
+ GvrHeadset.SupportsPositionalTracking);
}
}
private IEnumerator StatusUpdateLoop()
{
while (true)
{
yield return waitFourSeconds;
FindFloorHeight();
FindRecenterTransform();
FindSafetyOuterRadius();
FindSafetyInnerRadius();
FindSafetyRegionType();
}
}
}
}