Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create Toast on Android Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyPluginIsBananas : MonoBehaviour
{
const string ToastClassName = "android.widget.Toast";
public void OnMyButtonClick()
{
print("The cake is a lie!!");
var toastJavaClass = new AndroidJavaClass(ToastClassName);
const int duration = 1; // LENGTH_LONG in Android API
const string text = "Hello world!"; // C# string is automatically converted to java.lang.String
var context = GetUnityActivity();
var javaToastObject =
toastJavaClass.CallStatic<AndroidJavaObject>("makeText", context, text, duration);
javaToastObject.Call("show");
toastJavaClass.Dispose();
}
AndroidJavaObject GetUnityActivity()
{
using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
return unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment