Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a Toast message in Unity similar to one in android?

Tags:

c#

unity3d

Is there something like Toast message in Unity one that is similar to android, other than GUI.In android it was easy with one line code.

public void buttonclick()
{
// Message to show
}
like image 811
zyonneo Avatar asked Oct 01 '18 11:10

zyonneo


1 Answers

/// <param name="message">Message string to show in the toast.</param>
    private void _ShowAndroidToastMessage(string message)
    {
        AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject unityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

        if (unityActivity != null)
        {
            AndroidJavaClass toastClass = new AndroidJavaClass("android.widget.Toast");
            unityActivity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
            {
                AndroidJavaObject toastObject = toastClass.CallStatic<AndroidJavaObject>("makeText", unityActivity, message, 0);
                toastObject.Call("show");
            }));
        }
    }
like image 81
Awais Naseer Avatar answered Oct 13 '22 00:10

Awais Naseer