Skip to content

Instantly share code, notes, and snippets.

@TarasOsiris
Created April 15, 2016 12:52
Show Gist options
  • Save TarasOsiris/dec42cf3ecca6c854182f385e38dbf57 to your computer and use it in GitHub Desktop.
Save TarasOsiris/dec42cf3ecca6c854182f385e38dbf57 to your computer and use it in GitHub Desktop.
Shows toast on Android
public static void ShowToast(string text)
{
if (Application.platform == RuntimePlatform.Android)
{
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
activity.Call("runOnUiThread", new AndroidJavaRunnable(
()=>
{
AndroidJavaClass Toast = new AndroidJavaClass("android.widget.Toast");
AndroidJavaObject javaString = new AndroidJavaObject("java.lang.String", text);
AndroidJavaObject context = activity.Call<AndroidJavaObject>("getApplicationContext");
AndroidJavaObject toast = Toast.CallStatic<AndroidJavaObject>("makeText", context, javaString, Toast.GetStatic<int>("LENGTH_SHORT"));
toast.Call("show"); }
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment