1+ //-----------------------------------------------------------------------
2+ // <copyright file="DemoInputManager.cs" company="Google Inc.">
13// Copyright 2017 Google Inc. All rights reserved.
24//
35// Licensed under the Apache License, Version 2.0 (the "License");
1113// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1214// See the License for the specific language governing permissions and
1315// limitations under the License.
16+ // </copyright>
17+ //-----------------------------------------------------------------------
1418
1519#if UNITY_ANDROID && ! UNITY_EDITOR
1620#define RUNNING_ON_ANDROID_DEVICE
@@ -24,7 +28,6 @@ namespace GoogleVR.Demos
2428
2529#if UNITY_2017_2_OR_NEWER
2630 using UnityEngine . XR ;
27-
2831#else
2932 using XRSettings = UnityEngine . VR . VRSettings ;
3033#endif // UNITY_2017_2_OR_NEWER
@@ -34,17 +37,17 @@ public class DemoInputManager : MonoBehaviour
3437 private const string MESSAGE_CANVAS_NAME = "MessageCanvas" ;
3538 private const string MESSAGE_TEXT_NAME = "MessageText" ;
3639 private const string LASER_GAMEOBJECT_NAME = "Laser" ;
37- private const string CONTROLLER_CONNECTING_MESSAGE = "Controller connecting..." ;
38- private const string CONTROLLER_DISCONNECTED_MESSAGE = "Controller disconnected " ;
39- private const string CONTROLLER_SCANNING_MESSAGE = "Controller scanning ..." ;
40+ private const string CONTROLLER_CONNECTING_MESSAGE = "Daydream controller connecting..." ;
41+ private const string CONTROLLER_DISCONNECTED_MESSAGE = "Daydream controller not connected " ;
42+ private const string CONTROLLER_SCANNING_MESSAGE = "Scanning for Daydream controller ..." ;
4043 private const string NON_GVR_PLATFORM =
41- "Please select a supported Google VR platform via 'Build Settings > Android | iOS > Switch Platform'\n " ;
44+ "Please select a supported Google VR platform via 'Build Settings > Android | iOS > Switch Platform'\n " ;
4245
4346 private const string VR_SUPPORT_NOT_CHECKED =
44- "Please make sure 'Player Settings > Virtual Reality Supported' is checked\n " ;
47+ "Please make sure 'Player Settings > Virtual Reality Supported' is checked\n " ;
4548
4649 private const string EMPTY_VR_SDK_WARNING_MESSAGE =
47- "Please add 'Daydream' or 'Cardboard' under 'Player Settings > Virtual Reality SDKs'\n " ;
50+ "Please add 'Daydream' or 'Cardboard' under 'Player Settings > Virtual Reality SDKs'\n " ;
4851
4952 // Java class, method, and field constants.
5053 private const int ANDROID_MIN_DAYDREAM_API = 24 ;
@@ -63,10 +66,10 @@ public class DemoInputManager : MonoBehaviour
6366
6467 // Buttons that can trigger pointer switching.
6568 private const GvrControllerButton pointerButtonMask =
66- GvrControllerButton . App |
67- GvrControllerButton . TouchPadButton |
68- GvrControllerButton . Trigger |
69- GvrControllerButton . Grip ;
69+ GvrControllerButton . App |
70+ GvrControllerButton . TouchPadButton |
71+ GvrControllerButton . Trigger |
72+ GvrControllerButton . Grip ;
7073
7174 [ Tooltip ( "Reference to GvrControllerMain" ) ]
7275 public GameObject controllerMain ;
@@ -120,8 +123,8 @@ void Start()
120123 // The list is populated with valid VR SDK(s), pick the first one.
121124 gvrEmulatedPlatformType =
122125 ( XRSettings . supportedDevices [ 0 ] == GvrSettings . VR_SDK_DAYDREAM ) ?
123- EmulatedPlatformType . Daydream :
124- EmulatedPlatformType . Cardboard ;
126+ EmulatedPlatformType . Daydream :
127+ EmulatedPlatformType . Cardboard ;
125128 }
126129 isDaydream = ( gvrEmulatedPlatformType == EmulatedPlatformType . Daydream ) ;
127130#else
@@ -134,17 +137,17 @@ void Start()
134137 if ( vrDeviceName != GvrSettings . VR_SDK_CARDBOARD &&
135138 vrDeviceName != GvrSettings . VR_SDK_DAYDREAM )
136139 {
137- Debug . LogErrorFormat ( "Loaded device was '{0}', must be one of '{1}' or '{2}'" ,
140+ Debug . LogErrorFormat ( "Loaded device was '{0}', must be one of '{1}' or '{2}'" ,
138141 vrDeviceName , GvrSettings . VR_SDK_DAYDREAM , GvrSettings . VR_SDK_CARDBOARD ) ;
139- return ;
142+ return ;
140143 }
141144
142145 // On a non-Daydream ready phone, fall back to Cardboard if it's present in the list of
143146 // enabled VR SDKs.
144147 // On a Daydream-ready phone, go into Cardboard mode if it's the currently-paired viewer.
145148 if ( ( ! IsDeviceDaydreamReady ( ) && playerSettingsHasCardboard ( ) ) ||
146- ( IsDeviceDaydreamReady ( ) && playerSettingsHasCardboard ( ) &&
147- GvrSettings . ViewerPlatform == GvrSettings . ViewerPlatformType . Cardboard ) )
149+ ( IsDeviceDaydreamReady ( ) && playerSettingsHasCardboard ( ) &&
150+ GvrSettings . ViewerPlatform == GvrSettings . ViewerPlatformType . Cardboard ) )
148151 {
149152 vrDeviceName = GvrSettings . VR_SDK_CARDBOARD ;
150153 }
@@ -195,7 +198,7 @@ void Update()
195198#if ! RUNNING_ON_ANDROID_DEVICE
196199 UpdateEmulatedPlatformIfPlayerSettingsChanged ( ) ;
197200 if ( ( isDaydream && gvrEmulatedPlatformType == EmulatedPlatformType . Daydream ) ||
198- ( ! isDaydream && gvrEmulatedPlatformType == EmulatedPlatformType . Cardboard ) )
201+ ( ! isDaydream && gvrEmulatedPlatformType == EmulatedPlatformType . Cardboard ) )
199202 {
200203 return ;
201204 }
@@ -235,7 +238,7 @@ public static bool playerSettingsHasCardboard()
235238 element => element . Equals ( GvrSettings . VR_SDK_CARDBOARD ) ) ;
236239 }
237240
238- #if ! RUNNING_ON_ANDROID_DEVICE
241+ #if ! RUNNING_ON_ANDROID_DEVICE
239242 private void UpdateEmulatedPlatformIfPlayerSettingsChanged ( )
240243 {
241244 if ( ! playerSettingsHasDaydream ( ) && ! playerSettingsHasCardboard ( ) )
@@ -246,51 +249,51 @@ private void UpdateEmulatedPlatformIfPlayerSettingsChanged()
246249 // Player Settings > VR SDK list may have changed at runtime. The emulated platform
247250 // may not have been manually updated if that's the case.
248251 if ( gvrEmulatedPlatformType == EmulatedPlatformType . Daydream &&
249- ! playerSettingsHasDaydream ( ) )
252+ ! playerSettingsHasDaydream ( ) )
250253 {
251254 gvrEmulatedPlatformType = EmulatedPlatformType . Cardboard ;
252255 }
253256 else if ( gvrEmulatedPlatformType == EmulatedPlatformType . Cardboard &&
254- ! playerSettingsHasCardboard ( ) )
257+ ! playerSettingsHasCardboard ( ) )
255258 {
256259 gvrEmulatedPlatformType = EmulatedPlatformType . Daydream ;
257260 }
258261 }
259- #endif // !RUNNING_ON_ANDROID_DEVICE
262+ #endif // !RUNNING_ON_ANDROID_DEVICE
260263
261- #if RUNNING_ON_ANDROID_DEVICE
262- // Running on an Android device.
263- private static bool IsDeviceDaydreamReady ( )
264- {
265- // Check API level.
266- using ( var version = new AndroidJavaClass ( PACKAGE_BUILD_VERSION ) )
264+ #if RUNNING_ON_ANDROID_DEVICE
265+ // Running on an Android device.
266+ private static bool IsDeviceDaydreamReady ( )
267267 {
268- if ( version . GetStatic < int > ( FIELD_SDK_INT ) < ANDROID_MIN_DAYDREAM_API )
269- {
268+ // Check API level.
269+ using ( var version = new AndroidJavaClass ( PACKAGE_BUILD_VERSION ) )
270+ {
271+ if ( version . GetStatic < int > ( FIELD_SDK_INT ) < ANDROID_MIN_DAYDREAM_API )
272+ {
273+ return false ;
274+ }
275+ }
276+
277+ // API level > 24, check whether the device is Daydream-ready..
278+ AndroidJavaObject androidActivity = null ;
279+ try
280+ {
281+ androidActivity = GvrActivityHelper . GetActivity ( ) ;
282+ }
283+ catch ( AndroidJavaException e )
284+ {
285+ Debug . LogError ( "Exception while connecting to the Activity: " + e ) ;
270286 return false ;
271- }
272- }
273-
274- // API level > 24, check whether the device is Daydream-ready..
275- AndroidJavaObject androidActivity = null ;
276- try
277- {
278- androidActivity = GvrActivityHelper . GetActivity ( ) ;
279- }
280- catch ( AndroidJavaException e )
281- {
282- Debug . LogError ( "Exception while connecting to the Activity: " + e ) ;
283- return false ;
284- }
285-
286- AndroidJavaClass daydreamApiClass = new AndroidJavaClass ( PACKAGE_DAYDREAM_API_CLASS ) ;
287- if ( daydreamApiClass == null || androidActivity == null )
288- {
289- return false ;
290- }
291-
292- return daydreamApiClass . CallStatic < bool > ( METHOD_IS_DAYDREAM_READY , androidActivity ) ;
293- }
287+ }
288+
289+ AndroidJavaClass daydreamApiClass = new AndroidJavaClass ( PACKAGE_DAYDREAM_API_CLASS ) ;
290+ if ( daydreamApiClass == null || androidActivity == null )
291+ {
292+ return false ;
293+ }
294+
295+ return daydreamApiClass . CallStatic < bool > ( METHOD_IS_DAYDREAM_READY , androidActivity ) ;
296+ }
294297#endif // RUNNING_ON_ANDROID_DEVICE
295298
296299 private void UpdateStatusMessage ( )
@@ -329,7 +332,7 @@ private void UpdateStatusMessage()
329332 string vrSdkWarningMessage = isVrSdkListEmpty ? EMPTY_VR_SDK_WARNING_MESSAGE : "" ;
330333 string controllerMessage = "" ;
331334 GvrPointerGraphicRaycaster graphicRaycaster =
332- messageCanvas . GetComponent < GvrPointerGraphicRaycaster > ( ) ;
335+ messageCanvas . GetComponent < GvrPointerGraphicRaycaster > ( ) ;
333336 GvrControllerInputDevice dominantDevice = GvrControllerInput . GetDevice ( GvrControllerHand . Dominant ) ;
334337 GvrConnectionState connectionState = dominantDevice . State ;
335338
@@ -370,7 +373,7 @@ private void UpdateStatusMessage()
370373 messageCanvas . SetActive ( isVrSdkListEmpty ||
371374 ( connectionState != GvrConnectionState . Connected ) ) ;
372375#endif // !UNITY_ANDROID && !UNITY_IOS
373- }
376+ }
374377
375378 private void SetVRInputMechanism ( )
376379 {
0 commit comments