googlevr / gvr-unity-sdk

Google VR SDK for Unity
http://developers.google.com/vr/unity/
Other
2.71k stars 1.09k forks source link

OnGUI rendering #47

Closed miketucker closed 8 years ago

miketucker commented 9 years ago

The new CardboardGUI is great for VR, but I'd like to add a traditional button to toggle Cardboard mode on/off. However, all OnGUI rendering seems to be overwritten.

Is there any way to still render a GUI.Button or GUI.Texture?

thanks!

smdol commented 9 years ago

If you mean to render OnGUI in VR, see the Legacy/DemoScene which has an example of doing that.

miketucker commented 9 years ago

Thanks for your advice. I'd like to do an old-fashioned OnGUI directly onto the screen frame. The legacy example appears to render the GUI to a render texture which is then imposed in the 3d environment (see screengrab):

screen

smdol commented 9 years ago

Is this GUI meant to be in VR mode or out of VR mode?

miketucker commented 9 years ago

the gui will simply be a static button locked to the corner of the screen that toggles between regular and VR mode. Traditionally this could be done with an OnGUI(){ GUI.Button()... but this is being overwritten by the SDK's rendering.

smdol commented 9 years ago

If you are willing to modify the Cardboard.cs script slightly, you can get something to draw on the screen after the stereo view is done. That would be in the EndOfFrame() function. You can use CardboardOnGUIWindow script as well. Which version of the Cardboard SDK are you using? (The exact way to do this depends on the version.)

miketucker commented 9 years ago

That did it, thanks! It was a little fiddly to capture touch inputs because OnGUI can occur more than once per frame, but that hook allows GUI textures to be rendered correctly. (Using latest version btw)

Maybe for the next version there could be a standardized way of toggling Cardboard mode? I'd imagine there will be many apps that want to support it, but don't necessarily want to maintain two separate apps on the store.

ro4tub commented 9 years ago

hello @miketucker Could the buttons in legacy DemoScene receive any input? In my built demo, the buttons can't be touchable on a mobile phone.

miketucker commented 9 years ago

@ro4tub

In Cardboard.cs add this to the header:

public delegate void CardboardOnGUIEvent();
public event CardboardOnGUIEvent OnGUIRender;

and in the EndOfFrame function:

 if(OnGUIRender != null) OnGUIRender();

This will provide a hook for doing regular OnGUI stuff. Then in your script with the GUI:

void Start(){
     cardboard.OnGUIRender += HandleOnGUIRender;
}

void OnDestroy(){
     cardboard.OnGUIRender -= HandleOnGUIRender;
}

void OnGUI(){
    HandleOnGUIRender();
}

void HandleOnGUIRender(){
    if(GUI.Button ...
}

The key thing here is that Unity will continue to check for mouse/touch button events with the OnGUI function, and the new event hook will allow it to be rendered properly by Cardboard

ro4tub commented 9 years ago

@miketucker It works well on Editor, but failed on Phone. Is it ok on your phone?

smdol commented 9 years ago

@ro4tub The new 0.5.1 release should fix the missing java function bug.

smxthereisonlyone commented 9 years ago

@smdol It works on iPhone but not on Android, why? I amusing 0.5.1

smdol commented 9 years ago

@smxthereisonlyone Try turning off TapIsTrigger on the Cardboard script. That setting makes screen touches look like magnet trigger events (which only happens on Android but not iPhone).

smxthereisonlyone commented 9 years ago

@smdol But then there is no build in solution to register button events for the new cardboard v2?

smdol commented 9 years ago

You can see screen taps either with Input.touches or Input.GetMouseButtonDown(0).