Skip to content

Instantly share code, notes, and snippets.

@randhirraj3130
Created June 6, 2017 06:11
Show Gist options
  • Save randhirraj3130/b73a50d99c61043ac410e8ee83da4f3b to your computer and use it in GitHub Desktop.
Save randhirraj3130/b73a50d99c61043ac410e8ee83da4f3b to your computer and use it in GitHub Desktop.
how to show the toast the in unity for android devices
public static void showToast(string text)
{
if (Application.platform == RuntimePlatform.Android) {
AndroidJavaClass unityPlayer = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject> ("currentActivity");
AndroidJavaClass Toast = new AndroidJavaClass ("android.widget.Toast");
AndroidJavaObject javaString = new AndroidJavaObject ("java.lang.String", text);
AndroidJavaObject context = currentActivity.Call<AndroidJavaObject> ("getApplicationContext");
AndroidJavaObject toast = Toast.CallStatic<AndroidJavaObject> ("makeText", context, javaString, Toast.GetStatic<int> ("LENGTH_SHORT"));
toast.Call ("show");
} else {
Debug.Log(text);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment