Skip to content

Commit

Permalink
Cardboard XR Plugin v1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
chaosemer committed Oct 14, 2020
1 parent 0d3b490 commit f9d253a
Show file tree
Hide file tree
Showing 20 changed files with 125 additions and 42 deletions.
4 changes: 2 additions & 2 deletions Editor/BuildPostProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//-----------------------------------------------------------------------
// <copyright file="BuildPostProcessor.cs" company="Google LLC">
// Copyright 2020 Google LLC. All Rights Reserved.
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,7 @@
// </copyright>
//-----------------------------------------------------------------------

#if UNITY_EDITOR
#if UNITY_EDITOR && UNITY_IOS

namespace Google.XR.Cardboard.Editor
{
Expand Down
2 changes: 1 addition & 1 deletion Editor/CardboardMetadata.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//-----------------------------------------------------------------------
// <copyright file="CardboardMetadata.cs" company="Google LLC">
// Copyright 2020 Google LLC. All Rights Reserved.
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Google Cardboard XR Plugin for Unity
====================================
Copyright 2020 Google LLC. All rights reserved.
Copyright 2020 Google LLC

This SDK provides everything you need to create your own Virtual Reality (VR)
experiences for Google Cardboard in Unity 2019.3.12f1 or later. It supports
Expand Down
6 changes: 6 additions & 0 deletions Runtime/Android/generate_sdk_aar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# How to generate a new `sdk.aar`

Follow [Download and build the demo app](https://developers.google.com/cardboard/develop/c/quickstart#download_and_build_the_demo_app),
up to step 3. After assembling the `:sdk` module, copy the generated `.aar` file
(choose the debug or release version according to your needs) and replace
the `sdk.aar` file in this folder with it.
7 changes: 7 additions & 0 deletions Runtime/Android/generate_sdk_aar.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Runtime/Android/sdk.aar
Binary file not shown.
25 changes: 20 additions & 5 deletions Runtime/Api.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//-----------------------------------------------------------------------
// <copyright file="Api.cs" company="Google LLC">
// Copyright 2020 Google LLC. All Rights Reserved.
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -38,7 +38,7 @@ public static bool IsCloseButtonPressed
{
get
{
if (Input.touchCount == 0)
if (!XRLoader._isStarted || Input.touchCount == 0)
{
return false;
}
Expand All @@ -57,7 +57,7 @@ public static bool IsGearButtonPressed
{
get
{
if (Input.touchCount == 0)
if (!XRLoader._isStarted || Input.touchCount == 0)
{
return false;
}
Expand All @@ -76,7 +76,7 @@ public static bool IsTriggerPressed
{
get
{
if (Input.touchCount == 0)
if (!XRLoader._isStarted || Input.touchCount == 0)
{
return false;
}
Expand All @@ -96,6 +96,11 @@ public static bool IsTriggerPressed
/// <returns>Whether or not device parameters are found.</returns>
public static bool HasDeviceParams()
{
if (!XRLoader._isInitialized)
{
return false;
}

IntPtr encodedDeviceParams;
int size;
CardboardQrCode_getSavedDeviceParams(out encodedDeviceParams, out size);
Expand All @@ -116,6 +121,11 @@ public static bool HasDeviceParams()
/// </summary>
public static void ScanDeviceParams()
{
if (!XRLoader._isInitialized)
{
return;
}

_deviceParamsCount = CardboardQrCode_getQrCodeScanCount();
Debug.Log("[CardboardApi] QR Code scanning activity launched.");
CardboardQrCode_scanQrCodeAndSaveDeviceParams();
Expand All @@ -129,7 +139,7 @@ public static void ScanDeviceParams()
public static bool HasNewDeviceParams()
{
// TODO(b/156501367): Move this logic to the XR display provider.
if (_deviceParamsCount == -1)
if (!XRLoader._isInitialized || _deviceParamsCount == -1)
{
return false;
}
Expand All @@ -142,6 +152,11 @@ public static bool HasNewDeviceParams()
/// </summary>
public static void ReloadDeviceParams()
{
if (!XRLoader._isInitialized)
{
return;
}

// TODO(b/156501367): Move this logic to the XR display provider.
Debug.Log("[CardboardApi] Reload device parameters.");
_deviceParamsCount = CardboardQrCode_getQrCodeScanCount();
Expand Down
4 changes: 2 additions & 2 deletions Runtime/ApiConstants.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//-----------------------------------------------------------------------
// <copyright file="ApiConstants.cs" company="Google LLC">
// Copyright 2020 Google LLC. All Rights Reserved.
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -24,7 +24,7 @@ internal static class ApiConstants
public const string CardboardApi = "cardboard_api";
#elif UNITY_IOS
public const string CardboardApi = "__Internal";
#elif UNITY_EDITOR
#else
public const string CardboardApi = "NOT_AVAILABLE";
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion Runtime/UnitySubsystemsManifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Cardboard",
"version": "1.0.0",
"version": "1.3.0",
"libraryName": "cardboard_api",
"displays": [
{
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Widget.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//-----------------------------------------------------------------------
// <copyright file="Widget.cs" company="Google LLC">
// Copyright 2020 Google LLC. All Rights Reserved.
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
85 changes: 62 additions & 23 deletions Runtime/XRLoader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//-----------------------------------------------------------------------
// <copyright file="XRLoader.cs" company="Google LLC">
// Copyright 2020 Google LLC. All Rights Reserved.
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,6 +37,29 @@ public class XRLoader : XRLoaderHelper
private static List<XRInputSubsystemDescriptor> _inputSubsystemDescriptors =
new List<XRInputSubsystemDescriptor>();

private Texture2D _closeTexture;

private Texture2D _gearTexture;

/// <summary>
/// Gets a value indicating whether the subsystems are initialized or not.
/// </summary>
///
/// <returns>
/// True after a successful call to Initialize() without a posterior call to
/// Deinitialize().
/// </returns>
internal static bool _isInitialized { get; private set; }

/// <summary>
/// Gets a value indicating whether the subsystems are started or not.
/// </summary>
///
/// <returns>
/// True after a successful call to Start() without a posterior call to Stop().
/// </returns>
internal static bool _isStarted { get; private set; }

/// <summary>
/// Initialize the loader. This should initialize all subsystems to support the desired
/// runtime setup this loader represents.
Expand All @@ -50,6 +73,7 @@ public override bool Initialize()
_displaySubsystemDescriptors, "Display");
CreateSubsystem<XRInputSubsystemDescriptor, XRInputSubsystem>(
_inputSubsystemDescriptors, "Input");
_isInitialized = true;
return true;
}

Expand All @@ -62,6 +86,7 @@ public override bool Start()
{
StartSubsystem<XRDisplaySubsystem>();
StartSubsystem<XRInputSubsystem>();
_isStarted = true;
return true;
}

Expand All @@ -74,6 +99,7 @@ public override bool Stop()
{
StopSubsystem<XRDisplaySubsystem>();
StopSubsystem<XRInputSubsystem>();
_isStarted = false;
return true;
}

Expand All @@ -86,16 +112,35 @@ public override bool Deinitialize()
{
DestroySubsystem<XRDisplaySubsystem>();
DestroySubsystem<XRInputSubsystem>();
CardboardSDKDeinitialize();
_isInitialized = false;
return true;
}

[DllImport(ApiConstants.CardboardApi)]
private static extern void CardboardUnity_setScreenParams(
int x, int y, int width, int height);

[DllImport(ApiConstants.CardboardApi)]
private static extern void CardboardUnity_setWidgetCount(int count);

[DllImport(ApiConstants.CardboardApi)]
private static extern void CardboardUnity_setWidgetParams(
int i, IntPtr texture, int x, int y, int width, int height);

#if UNITY_ANDROID
[DllImport(ApiConstants.CardboardApi)]
private static extern void CardboardUnity_initializeAndroid(IntPtr context);
#endif

/// <summary>
/// For Android, initializes JavaVM and Android activity context.
/// Then, for both Android and iOS, it sets the screen size in pixels.
/// </summary>
private static void CardboardSDKInitialize()
private void CardboardSDKInitialize()
{
#if UNITY_ANDROID
// TODO(b/169797155): Move this to UnityPluginLoad().
// Gets Unity context (Main Activity).
var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
Expand All @@ -112,39 +157,33 @@ private static void CardboardSDKInitialize()
(int)Screen.safeArea.x, (int)Screen.safeArea.y, (int)Screen.safeArea.width,
(int)Screen.safeArea.height);

Texture2D closeTexture = Resources.Load<Texture2D>(
"Cardboard/quantum_ic_close_white_24");
Texture2D gearTexture = Resources.Load<Texture2D>(
"Cardboard/quantum_ic_settings_white_24");
_closeTexture = Resources.Load<Texture2D>("Cardboard/quantum_ic_close_white_24");
DontDestroyOnLoad(_closeTexture);
_gearTexture = Resources.Load<Texture2D>("Cardboard/quantum_ic_settings_white_24");
DontDestroyOnLoad(_gearTexture);

RectInt closeRect = Widget.CloseButtonRenderRect;
RectInt gearRect = Widget.GearButtonRenderRect;
RectInt alignmentRect = Widget.AlignmentRect;
CardboardUnity_setWidgetCount(3);
CardboardUnity_setWidgetParams(
0, closeTexture.GetNativeTexturePtr(), closeRect.x, closeRect.y,
0, _closeTexture.GetNativeTexturePtr(), closeRect.x, closeRect.y,
closeRect.width, closeRect.height);
CardboardUnity_setWidgetParams(
1, gearTexture.GetNativeTexturePtr(), gearRect.x, gearRect.y, gearRect.width,
1, _gearTexture.GetNativeTexturePtr(), gearRect.x, gearRect.y, gearRect.width,
gearRect.height);
CardboardUnity_setWidgetParams(
2, Texture2D.whiteTexture.GetNativeTexturePtr(), alignmentRect.x,
alignmentRect.y, alignmentRect.width, alignmentRect.height);
}

[DllImport(ApiConstants.CardboardApi)]
private static extern void CardboardUnity_setScreenParams(
int x, int y, int width, int height);

[DllImport(ApiConstants.CardboardApi)]
private static extern void CardboardUnity_setWidgetCount(int count);

[DllImport(ApiConstants.CardboardApi)]
private static extern void CardboardUnity_setWidgetParams(
int i, IntPtr texture, int x, int y, int width, int height);

#if UNITY_ANDROID
[DllImport(ApiConstants.CardboardApi)]
private static extern void CardboardUnity_initializeAndroid(IntPtr context);
#endif
/// <summary>
/// Close and gear button textures are preserved until the XR provider is deinitialized.
/// </summary>
private void CardboardSDKDeinitialize()
{
Resources.UnloadAsset(_closeTexture);
Resources.UnloadAsset(_gearTexture);
}
}
}
2 changes: 1 addition & 1 deletion Runtime/XRSettings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//-----------------------------------------------------------------------
// <copyright file="XRSettings.cs" company="Google LLC">
// Copyright 2020 Google LLC. All Rights Reserved.
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Binary file modified Runtime/iOS/cardboard_api.a
Binary file not shown.
2 changes: 1 addition & 1 deletion Runtime/iOS/cardboard_api.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Google LLC. All Rights Reserved.
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
9 changes: 9 additions & 0 deletions Runtime/iOS/generate_cardboard_api_a.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# How to generate a new `cardboard_api.a`

Follow the [Download and build the demo app](https://developers.google.com/cardboard/develop/ios/quickstart#download_and_build_the_demo_app)
up to step 2 to open `Cardboard.xcworkspace` workspace in XCode. Select the
`sdk` module and then `Generic iOS Device` as the target device (or choose to
build for a specific phone if that suits your needs better). Build the `sdk`
module, copy the generated `.a` file and replace the `cardboard_api.a` file in
this folder. Similarly, do the same with the `sdk.bundle` folder and its
contents if necessary.
7 changes: 7 additions & 0 deletions Runtime/iOS/generate_cardboard_api_a.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Samples~/hellocardboard-unity/Scripts/CameraPointer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//-----------------------------------------------------------------------
// <copyright file="CameraPointer.cs" company="Google LLC">
// Copyright 2020 Google LLC. All Rights Reserved.
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Samples~/hellocardboard-unity/Scripts/CardboardStartup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//-----------------------------------------------------------------------
// <copyright file="CardboardStartup.cs" company="Google LLC">
// Copyright 2020 Google LLC. All Rights Reserved.
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Samples~/hellocardboard-unity/Scripts/ObjectController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//-----------------------------------------------------------------------
// <copyright file="ObjectController.cs" company="Google LLC">
// Copyright 2020 Google LLC. All Rights Reserved.
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.google.xr.cardboard",
"displayName": "Google Cardboard XR Plugin for Unity",
"version": "1.2.0",
"version": "1.3.0",
"unity": "2019.3",
"description": "Provides rendering and input support for Google Cardboard.",
"keywords": [
Expand Down

0 comments on commit f9d253a

Please sign in to comment.