- Home /
Opening files/URLs with an Unity App in Android
I want to be able to open a resource (e.g. an AssetBundle) from a browser or file explorer in Android. I have associated a file extension as described HERE, and capture the current activity and intent as outlined HERE. I get the current activity and intent on focus change, but if I click on different files without killing my App in between, I always only get the URI of the initial click/intent. I access the activity in OnFocusChange() and it looks as if only the first time I get real Intent data and access only a copy afterwards. Does anyone have an idea how to fix this, or a better way to implement this functionality?
EDIT: OK, so it seems someone had the same problem HERE. Does this mean I have to build a Android plugin? Where do I add code to override the Main Activity behavior?
Here's the relevant code:
void OnApplicationFocus(bool focusStatus)
{
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject context = jc.GetStatic<AndroidJavaObject>("currentActivity");
count++;
AndroidJavaObject intent = context.Call<AndroidJavaObject>("getIntent");
string action = intent.Call<string>("getAction");
if (action == "android.intent.action.VIEW")
{
AndroidJavaObject URI = intent.Call<AndroidJavaObject>("getData");
string path = URI.Call<string>("getPath");
tm.text = action + "\n" + path + "\n" + count;
}
else
tm.text = action + "\n" + count;
}
and the relevant part from my AndroidManifest:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />
<data android:mimeType="*/*" />
<data android:host="*" />
<data android:pathPattern=".*\\.btp" />
<data android:pathPattern=".*\\..*\\.btp" />
<data android:pathPattern=".*\\..*\\..*\\.btp" />
<data android:pathPattern=".*\\..*\\..*\\..*\\.btp" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />