Skip to content

Instantly share code, notes, and snippets.

@McBrideMusings
Last active June 15, 2022 17:53
Show Gist options
  • Save McBrideMusings/463e647a313372b6af78c0ebdeaf952a to your computer and use it in GitHub Desktop.
Save McBrideMusings/463e647a313372b6af78c0ebdeaf952a to your computer and use it in GitHub Desktop.
Sample script for getting Android Intents in Unity
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// https://developer.android.com/studio/command-line/adb#IntentSpec
public class UnityAndroidIntents : MonoBehaviour
{
private void Awake()
{
getIntentData();
}
private bool getIntentData()
{
#if (!UNITY_EDITOR && UNITY_ANDROID)
return CreatePushClass (new AndroidJavaClass ("com.unity3d.player.UnityPlayer"));
#endif
return false;
}
public bool CreatePushClass(AndroidJavaClass UnityPlayer)
{
#if UNITY_ANDROID
AndroidJavaObject currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject intent = currentActivity.Call<AndroidJavaObject>("getIntent");
AndroidJavaObject extras = GetExtras(intent);
if (extras != null)
{
string ex = GetProperty(extras, "my_text");
return true;
}
else
{
return false;
}
#else
return false;
#endif
}
private AndroidJavaObject GetExtras(AndroidJavaObject intent)
{
AndroidJavaObject extras = null;
try
{
extras = intent.Call<AndroidJavaObject>("getExtras");
}
catch (Exception e)
{
Debug.Log(e.Message);
}
return extras;
}
private string GetProperty(AndroidJavaObject extras, string name)
{
string s = string.Empty;
try
{
s = extras.Call<string>("getString", name);
}
catch (Exception e)
{
Debug.Log(e.Message);
}
return s;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment