I am developing a project which includes Android app and Unity app. The main app is Android which at some point opens the Unity app. I exported the Unity app as Android plugin and integrated it in the Android studio as AAR module. When I open Unity app from Android I have to send a variable (integer) and retrieve it in Unity. Depending on this integer then I activate different objects. My question is how to send this integer in Android and then retrieve it in Unity? I read and tried a lot, used "UnityPlayer.UnitySendMessage" in Android and "AndroidJavaObject" in Unity but I didn't manage to achieve what I want.
Package name of Android app is "android.unityembed" and of Unity is "com.borut.multiple".
Java code:
package android.unityembed;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.unity3d.player.UnityPlayer;
import com.borut.multiple.UnityPlayerActivity;
public class Start extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), UnityPlayerActivity.class);
startActivity(intent);
}
});
}
public static int OnSceneLoadComplete(){
return 1;
}
}
Unity code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.VR;
public class NormalStart : MonoBehaviour {
public GameObject space1;
public GameObject space2;
// Use this for initialization
void Start () {
StartCoroutine (LoadDeviceInit ("", false));
// x is the variable you get from Java
AndroidJavaClass jc = new AndroidJavaClass ("android.unityembed.Start");
int x = jc.CallStatic<int> ("OnSceneLoadComplete");
}
// Update is called once per frame
void Update () {
}
public void active1() {
space1.SetActive (true);
}
public void active2() {
space2.SetActive (true);
}
IEnumerator LoadDeviceInit(string newDevice, bool enable)
{
Debug.Log ("LoadDevice: " + newDevice + " enabled: " + enable);
VRSettings.LoadDeviceByName(newDevice);
yield return null;
VRSettings.enabled = enable;
yield return null;
Camera.main.ResetAspect();
}
//Called from Java to receive message
public void JavaMessageReceiver(string message)
{
Debug.Log("Received Message form Java: "+message);
}
}
EDIT: I solved the problem, updated code above does what I wanted.
I have another issue. My Unity app uses Google VR SDK and it's implemented also for Cardboard. When I build Unity app normally (I get .apk file) the Cardboard loads like it should (split screen). But when I export this app as a module in Android Studio, when I run UnityPlayerActivity the Cardboard view doesn't load, the scene is loaded like a normal Unity 3D scene.
Here are two screenshots of Android Studio Logcat: Logcat 1 Logcat 2
Here is also a screenshot of my Player Settings in Unity. Sometimes I want to load the Cardboard, sometimes not (depending on user's choice). Unity Player Settings
How can I solve this problem?