XR Cardboard Interactor
Helpers for integrating Google Cardboard XR Plugin with the Unity XR Interaction Toolkit
These assets provide a simple way to use the XR Interaction Toolkit with the Cardboard XR Plugin.
- Blog post: https://medium.com/@linojon/an-xr-cardboard-interactor-for-unity-eec6270be2e9
- Repository: https://github.com/linojon/CardboardInteractor
Cardboard plugin documentation:
- Google Cardboard XR Plugin for Unity: https://github.com/googlevr/cardboard-xr-plugin
- Quickstart for Google Cardboard for Unity: https://developers.google.com/cardboard/develop/unity/quickstart
Quick Start
The following instructions are updated to Unity 2021.3.21f1 LTS, but they should be valid for a wide range of versions.
- Install XR Plugin Management (Edit > Project Settings > XR Plugin Management)
- From Windows > Package Manager, install
XR Interaction Toolkit
and make sure that also theXR Plugin Management
got installed - Still in Package Manager, add the Google Cardboard SDK package (
https://github.com/googlevr/cardboard-xr-plugin.git
) via url (+ > Add Package From Git URL); - Follow the instructions to set up the Google Cardboard SDK
https://developers.google.com/cardboard/develop/unity/quickstart
- Either open the CardboardInteractorDemo scene or set up your own.
Use the Demo Scene
This repository can be directly opened from Unity Hub. In the Assets\CardboardHelpers\Demo folder the CardboardInteractableDemo
scene gives a simple example on how to use the CardboardInteractor.
Set up your own Scene
The demo scene can be easily used for your own purposes by simply deleting those GameObjects not required by your projects. However it's also quite simple to set up your own Scene from scratch.
Start a new empty 3D project. The standard Main Camera
component will be added, together with a default lighting.
Make the Main Camera an Interactor
On the Main Camera game object:
- right-click the
Main Camera
object and selectXR > Convert Main Camera To XR Rig
: now theMain Camera
will be wrapped by anXR Rig
GameObject; you may want to adjust its Transform properties to place it in an appropriate position; - on the newly created
XRRig
, through theAdd Component
button, add anXR Origin
component and set its attributes:- drag
Camera Offset
contained inXRRig
toCamera Floor Offset Object
property; - drag
Main Camera
contained inXRRig>Camera Offset
toCamera Game Object
property;
- drag
- select this latter
Main Camera
object and add the following Components:XR Controller (Device-based)
and set theController Node
property toLeft Eye
XR Ray Interactor
and set theSelect Action Trigger
toState Change
XR Cardboard Controller
i.e. the script contained in this repository
- it's recommended to create an Interactable layer and set the Interactables Layer Mask on
XR Cardboard Controller
- add the
Reticle
prefab as a child of Main Camera, set Position Z=2
The camera will now interact with UI buttons on a world canvas
Set the XR Interaction Manager
On the current scene:
- add a new XR > Interaction Manager
- go back to the
XRRig > Camera Offset > Main Camera
object and in theXR Ray Interactor
set the propertyInteraction Manager
to the newly createdXR Interaction Manager
Make an object a Cardboard Interactable
To make a game object interactable, on the object:
- add a
CardboardInteractable
script from this package
The camera will now interact with the game object. The default mechanic is you point-and-click to select, move your head, then point-and-click again to unselect.
Note the On Hover Enter, On Hover Exit, On Select Enter, and On Select Exit actions are comparable to the XRI interactable ones.
Settings
The XRCardboardController scripts holds a flag called Make All Buttons Clickable
that automatically adds a CardboardButtonClickable
Script Component to any GameObject of type Button
. This script will automatically invoke the standard OnClick()
event of the Button.
Troubleshooting
- The Sphere does not get highlighted when the reticle is pointing to the Cube: by copying and pasting the Cube and Sphere GameObjects it is likely that some reference can be corrupted. Typical symptom is when you point to the cube and the sphere is not highlighted. Two main reasons:
- the alpha channel of the
Hover
andSelect
colors rolled back to zero, thus rendering only a transparent color; set it back to one; - the Cardboard Interactable component of Cube might have wrong link to the Sphere Highlighter script. Please set them back manually after copying.
- the alpha channel of the
- I don't see the reticle/crossair: you probably didn't set the Z position of the reticle;