- Home /
Send data from Android to Unity
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.
Answer by Kudorado · Apr 22, 2018 at 11:47 AM
Java public class SomeJNIClass{ public static int GetInterger(){ return 0;//or something you want. } }
Unity public class SomeClass : MonoBehaviour{ void Start(){ var someJNI = new AndroidJavaObject("com.package.SomeJNIClass"); int value = someJNI.GetStatic("GetInterger"); } }
I tried this already and now again but it didn't work. I will paste the code because probably I am doing something wrong. Package name of Android app is "android.unityembed" and of Unity is "com.borut.multiple".
Java code:
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.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), UnityPlayerActivity.class);
startActivity(intent);
}
});
}
public static int getInteger() {
return 1;
}
}
Unity code:
public class NormalStart : $$anonymous$$onoBehaviour {
public GameObject space1;
public GameObject space2;
// Use this for initialization
void Start () {
StartCoroutine (LoadDeviceInit ("", false));
PlayerPrefs.SetInt ("VR$$anonymous$$ode", 2);
var someJNI = new AndroidJavaObject ("android.unityembed.Start");
int value = someJNI.GetStatic<int> ("getInteger");
if (value == 1) {
space1.SetActive (true);
} else {
space2.SetActive (true);
}
}
// Update is called once per frame
void Update () {
}
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();
}
}
When I open Unity app (on button click) none of the objects "space1" and "space2" become active.
Answer by joshuaGnol · Feb 18, 2020 at 06:17 AM
https://www.youtube.com/watch?v=EElBAGkjPt4
you might want to check this out. it helps me perfectly.
Answer by degee147 · Dec 01, 2020 at 02:18 PM
Please see an answer here, It took me a while as well but I figured it out: https://stackoverflow.com/a/65092095/1828990
Your answer
![](https://koobas.hobune.stream/wayback/20220612161256im_/https://answers.unity.com/themes/thub/images/avi.jpg)