Created
April 15, 2016 12:52
-
-
Save TarasOsiris/dec42cf3ecca6c854182f385e38dbf57 to your computer and use it in GitHub Desktop.
Shows toast on Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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