I am launching an Unity application from another Android application using a custom implicit intent. This is working fine, but I cannot figure out how to read the intent extra data in Unity?
ANDROID INTENT TO LAUNCH UNITY APP
i=new Intent();
i.setAction("com.company.unityapp.MyMethod");
i.putExtra("KEY","This is the message string");
startActivity(i);
UNITY APP AndroidManifest.xml
<intent-filter>
<action android:name="com.company.unityapp.MyMethod" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
I have a GameObject in my scene with a script attached. Inside the start method I have this code to try and read the extra data that was passed along with the intent
AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject intent = currentActivity.Call<AndroidJavaObject>("getIntent");
bool hasExtra = intent.Call<bool> ("hasExtra", "arguments");
if (hasExtra) {
AndroidJavaObject extras = intent.Call<AndroidJavaObject> ("getExtras");
arguments = extras.Call<string> ("getString", "arguments");
}
This is not working and arguments is always empty. Any help would be appreciated.
It took me quite some time to figure this out. All solutions found online were only partly complete. Below is the full solution for launching a Unity application from another android application using a custom implicit intent and also how to access the extra data sent with the intent inside Unity.
To accomplish this you need to create a Android plugin that will be used by Unity to access the intent extra data.
ANDROID PLUGIN:
You need to copy the classes.jar from Unity installation folder to the android plugin folder /lib/classes.jar
AndroidManifest.xml
Important here is the package name used: com.company.plugin
Gradle build file:
Add the following to the app gradle build file to be able to create a .jar to be used with Unity
Copy the created AndroidPlugin.jar to Unity Assets/Plugins/Android
UNITY APP:
Set the bundle identifier in PlayerSettings to be the same as set in the Android Plugin - com.company.plugin
Create custom AndroidManifest.xml file in Assets/Plugins/Android
Important here is to use the same package name as used in the plugin. Also note the intent name: com.company.plugin.do
AndroidManifest.XML
Create a unity script named AccessManager and attach the script to a game object in the scene. OnAccessToken is the method that will receive the message sent from the android plugin and will contain the extra data sent from the intent.
ANDROID APP:
Create a standard Android application that will launch the Unity Application and send the intent extra data