3

-> Summary:

I want to send messages from AndroidStudio - Code to Unity - Normal way doesn't seem to be working.


-> What I tried:

I have two projects in Unity3D. They are exactly the same, except that one of them is set up for the Oculus Gear Vr... Then I export (Android) both of these Projects and open them as AndroidStudio Projects. When i run these now both work exactly as expected !

But when i try to use the UnityPlayer.UnitySendMessage - method, the method seems not to be executed in the app with the setup vor VR... The app doesnt crash, it doesnt show any errors in LogCat and i tried to catch any Throwable but that didnt work either...

(On the App with no VR setup the exact same lines of codes works ...)


-> Android - Code:

public class UnityPlayerActivity extends Activity {
   [...]

   onCreate(Bundle savedInstanceState){
      [...] //code generated by Unity

      try{
         Log.d("mDEBUG", "Test Android1");
         UnityPlayer.UnitySendMessage("AndroidCommunication", "AndroidStudioInit", "false");
         Log.d("mDEBUG", "Test Android2");
      }catch(Throwable e){
         e.printStackTrace();
      }
   }

   //Function used by Unity - Code
   public void unityLog(String msg){
      Log.d("mDEBUG", "Unity: " + msg);
   }

   [...]
}

-> Unity - Code:

public void AndroidStudioInit(string flag)
{
   bool sendCurCameraRotation = bool.Parse(flag);
   
   AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
   AndroidJavaObject unityPlayerActivity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");

   unityPlayerActivity.Call("unityLog", "AndroidStudio - Unity - Init");
}


-> Expected Outputs:

For both apps it should print in Logcat:

  1. "[...] D/mDEBUG: Test Android1"
  2. "[...] D/mDEBUG: Unity: AndroidStudio - Unity - Init"
  3. "[...] D/mDEBUG: Test Android2"

-> Actual Results:

For app without setup for VR:

  1. "[...] D/mDEBUG: Test Android1"
  2. "[...] D/mDEBUG: Unity: AndroidStudio - Unity - Init"
  3. "[...] D/mDEBUG: Test Android2"

For app with VR:

  1. "[...] D/mDEBUG: Test Android1"
  2. "[...] D/mDEBUG: Test Android2"

There are also no errors and the Unity App doesnt crash... it just "seems to ignore" the function call...

Community
  • 1
  • 1
Programm
  • 43
  • 1
  • 7

1 Answers1

1

Ok it seems I know what the problem is and I think it can't really be helped...

I was sending a message to Unity like this:

mUnityPlayer = new UnityPlayer(this);
setContentView(mUnityPlayer);
mUnityPlayer.requestFocus();

UnityPlayer.UnitySendMessage("AndroidCommunication", "AndroidStudioInit", "false");

And normally Unity will store the message in a queue to execute it after Unity has started properly.

That works for the App without Vr enabled just fine.

But with Vr mode enabled a screen will first show up on your device (something like "Put your device into the Gear Vr ..."). And I think it somehow messes up with the message sending...

So Unity will only execute messages after proper starting (After window "Made by Unity" went by).

To remember: Don't send Messages until unity has completely started if you use Vr... If you still want to execute code on start you can try to build a method in Unity that will notify the android code that Unity has finished !

Cheers !

Programm
  • 43
  • 1
  • 7