Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
a script to show Toast Messages on Android Unity3D Apps
using UnityEngine;
public class ToastMessage : MonoBehaviour
{
string toastString;
string input;
AndroidJavaObject currentActivity;
AndroidJavaClass UnityPlayer;
AndroidJavaObject context;
void Start()
{
if (Application.platform == RuntimePlatform.Android)
{
UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
context = currentActivity.Call<AndroidJavaObject>("getApplicationContext");
}
}
public void showToastOnUiThread(string toastString)
{
this.toastString = toastString;
currentActivity.Call("runOnUiThread", new AndroidJavaRunnable(showToast));
}
void showToast()
{
Debug.Log(this + ": Running on UI thread");
AndroidJavaClass Toast = new AndroidJavaClass("android.widget.Toast");
AndroidJavaObject javaString = new AndroidJavaObject("java.lang.String", toastString);
AndroidJavaObject toast = Toast.CallStatic<AndroidJavaObject>("makeText", context, javaString, Toast.GetStatic<int>("LENGTH_SHORT"));
toast.Call("show");
}
}
@lasithalakmal

This comment has been minimized.

Copy link

@lasithalakmal lasithalakmal commented May 19, 2018

What is showToast at 2nd line in showToastOnUiThread method?

@sasherafat

This comment has been minimized.

Copy link

@sasherafat sasherafat commented Jul 18, 2018

hi
why currentActivity.Call("runOnUiThread", new AndroidJavaRunnable(showToast));
return null?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment