Jay
Jay

Reputation: 75

How to detect UI Objects from Main Camera object when using Google Cardboard VR SDK for Unity

When using Google Cardboard VR SDK for Unity, how can I detect when the Main Camera object looks at UI objects inside Canvas? OnPointerEnter() and OnPointerExit() happens when I look at 3D Objects in the samples project that Google offers, but there is no way to do it for UI Objects.

Upvotes: 0

Views: 526

Answers (1)

Abolfazl
Abolfazl

Reputation: 100

Based on your explanation, I guess you have set the Render Mode of your Canvas to Screen Space-Overlay or Screen Space-Camera. When you use one of these render modes, the position of your Canvas on the screen will never change, so you can never catch any element of the UI unless its bound includes the middle point of the screen. why? because you are using google VR and you have no joystick or something similar to control the cursor's position, therefore the cursor position is always (0, 0).

Let's assume the cursor and Canvas, both, are children of your camera. You can move your camera by shaking your head, this way the canvas and the cursor move in comparison with other objects in your virtual world based on camera movement, but they never move in comparison with each other.

So what's the solution? I think you can set the Render Mode of your canvas to World Space. This way your canvas will be an object in the world that the cursor can navigate through it. I know you think it's weird, cause likely you want to always see the canvas just in front of your eyes. So I think you just have one solution:

Do not change the Render Mode of the canvas, locate All UI elements on the canvas, it's better to locate them near the edges of the screen and also with adequate distances, then write a script to do this: When the cursor is moving, calculate the direction of movement and find out which UI elements could be the target of this movement, move it towards the middle point of the screen, when its bound includes the middle point, its OnClick will be called. After that, you must return all UI elements to their early positions. Also if the cursor stopped moving before the UI element reaches the middle point, you must return all UI elements to their early positions.

I know, it's so hard to handle this, but it's the only way I could propose.

Upvotes: 0

Related Questions