googlevr / gvr-unity-sdk

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

Intended usage when setting up your own Activity on Android. #136

Closed andr3wmac closed 8 years ago

andr3wmac commented 8 years ago

I'm integrating Unity + Google Cardboard as an activity launched as part of an existing android application. Everything appears to be working fine however I noted some errors in the logs:

I/Unity   ( 2928): Creating Cardboard object
I/Unity   ( 2928):
I/Unity   ( 2928): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 64)
I/Unity   ( 2928):
I/Unity   ( 2928): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 64)
I/Unity   ( 2928):
E/Unity   ( 2928): Exception creating object com.google.vr.platform.unity.UnityVrActivityListener: UnityEngine.AndroidJavaException: java.lang.ClassCastException: co.river.integratedplayer.RiverVRUnityPlayerActivity cannot be cast to com.google.unity.GoogleUnityActivity
E/Unity   ( 2928):   at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in <filename unknown>:0
E/Unity   ( 2928):   at UnityEngine.AndroidJNISafe.NewObject (IntPtr clazz, IntPtr methodID, UnityEngine.jvalue[] args) [0x00000] in <filename unknown>:0
E/Unity   ( 2928):   at UnityEngine.AndroidJavaObject._AndroidJavaObject (System.String className, System.Object[] args) [0x00000] in <filename unknown>:0
E/Unity   ( 2928):   at UnityEngine.AndroidJavaObject..ctor (System.String className, System.Object[] args) [0x00000] in <filename unknown>:0
E/Unity   ( 2928):   at BaseAndroidDevice.Create (System.String className, System.Object[] args) [0x00000] in <filename unknown>:0
E/Unity   ( 2928):
E/Unity   ( 2928): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 64)
E/Unity   ( 2928):
I/Unity   ( 2928):
I/Unity   ( 2928): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 64)
I/Unity   ( 2928):
E/Unity   ( 2928): Object is null when calling method setAlignmentMarkerEnabled
E/Unity   ( 2928):
E/Unity   ( 2928): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 64)
E/Unity   ( 2928):
E/Unity   ( 2928): Object is null when calling method setSettingsButtonEnabled
E/Unity   ( 2928):
E/Unity   ( 2928): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 64)
E/Unity   ( 2928):
E/Unity   ( 2928): Object is null when calling method setVRBackButtonEnabled
E/Unity   ( 2928):
E/Unity   ( 2928): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 64)
E/Unity   ( 2928):
E/Unity   ( 2928): Object is null when calling method setShowVrBackButtonOnlyInVR
E/Unity   ( 2928):
E/Unity   ( 2928): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 64)
E/Unity   ( 2928):
E/Unity   ( 2928): Object is null when calling method setTapIsTrigger
E/Unity   ( 2928):
E/Unity   ( 2928): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 64)
E/Unity   ( 2928):
E/Unity   ( 2928): Object is null when calling method setVRModeEnabled
E/Unity   ( 2928):
E/Unity   ( 2928): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 64)
E/Unity   ( 2928):
E/Unity   ( 2928): Object is null when calling method onPause
E/Unity   ( 2928):
E/Unity   ( 2928): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 64)
E/Unity   ( 2928):
E/Unity   ( 2928): Object is null when calling method setVRModeEnabled
E/Unity   ( 2928):
E/Unity   ( 2928): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 64)
E/Unity   ( 2928):

I've traced this to here: https://github.com/googlesamples/cardboard-unity/blob/master/Cardboard/Scripts/VRDevices/CardboardAndroidDevice.cs#L30-L35

Right now my activity is currently extending com.unity3d.player.UnityPlayerActivity which is working great. I tried com.google.unity.GoogleUnityActivity but when I launch using that the google cardboard overlay comes up over everything and no input events make it to Unity.

What is the intention behind com.google.unity.GoogleUnityActivity and com.google.vr.platform.unity.UnityVrActivityListener ? What is considered the proper way to create your own activity for Unity that utilizes Google Cardboard?

To clarify, the only real issue is the errors in the logs, Cardboard continues to work fine when I use com.unity3d.player.UnityPlayerActivity, I would just like to know if this could be causing any issues and if there is a way to fix it.

smdol commented 8 years ago

You should extend GoogleUnityActivity, at least for now. It is basically an altered version of UnityPlayerActivity that has hooks for the UnityVrActivityListener class. This class holds the Cardboard UI layer, and it should only intercept events like pressing on the VR Back Button, the Gear Button, etc. It does also catch screen touches and report them to Unity differently from usual, but you can disable that by clearing "TapIsTrigger" on the Cardboard.cs object.

andr3wmac commented 8 years ago

Okay, I've switched to utilizing GoogleUnityActivity. After disabling tap trigger mostly everything works fine. Theres a minor issue I'm having.

When I first launch the activity it appears VR mode is enabled. There's a ~1 second period where I can see the cardboard overlay (line down the middle, gear button in the corner) and then my Unity code kicks in and sets VRModeEnabled = false and it disappears. I've tried modifying cardboard SDK here:

https://github.com/googlesamples/cardboard-unity/blob/e94bb104a2ef5924103a4d887a0a451ed56bca41/Cardboard/Scripts/Cardboard.cs#L79

However it still happens. Is there a way to toggle this from the Activity level before it launches to ensure it's never drawn? It's disrupting my loading screen.

smdol commented 8 years ago

That bug is from the native layer, but should be fixed in the next release (coming soon).

andr3wmac commented 8 years ago

You're more familiar with the internal workings than I am; can you think of any negative consequences to not utilizing GoogleUnityActivity other than the console spam on start up and missing TapIsTrigger support? This app is in the final stages and it's very likely we'll be shipping sooner than that release and the startup flash is unsightly where as the log spam is not seen by the end user so I'm considering switching back to UnityPlayerActivity.

dill0wn commented 8 years ago

Any advice on what could be done in the event that a (non-cardboard) 3rd party plugin needs to be the main activity?

smdol commented 8 years ago

At the moment, there's no friendly way to do this, but it can be (and has been) done. Decompile unityvractivity.jar and get the code for GoogleUnityActivity. Incorporate it with the other plugin's activity, either as a base, or as an extension, or even just copy in the implementation.

cxy200927099 commented 8 years ago

Hi: Is there complete document for GoogleUnityActivity? And teach us how to extends the GoogleUnityActivity? i export a android project from cardboard unity project,i find that the launcher activity is GoogleUnityActivity, But i want to add some ui on this project, so i need extend GoogleUnityActivity; is there any reference code or document?