Skip to content

Instantly share code, notes, and snippets.

@inertiave
Created October 31, 2019 07:03
Show Gist options
  • Save inertiave/1b0d6113c3b7413484e25b9f25abe52e to your computer and use it in GitHub Desktop.
Save inertiave/1b0d6113c3b7413484e25b9f25abe52e to your computer and use it in GitHub Desktop.
using UnityEngine;
public class Toast : MonoBehaviour {
// Singleton
static Toast instance;
public static Toast GetInstance()
{
if (instance == null) {
instance = FindObjectOfType<Toast>();
if (instance == null) {
GameObject GoToast = new GameObject("Toast");
instance = GoToast.AddComponent<Toast>();
}
}
return instance;
}
AndroidJavaObject curActivity = null;
// Awake
void Awake()
{
if (Application.platform == RuntimePlatform.Android) {
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
curActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
}
}
// Main Function
public void DrawToast(string str)
{
if (str.Length == 0) {
Debug.Log("null input");
}
else {
#if UNITY_EDITOR
Debug.Log("Draw Toast : " + str);
#endif
// Toast on Android
if (Application.platform == RuntimePlatform.Android) {
curActivity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
{
AndroidJavaObject toast = new AndroidJavaObject("android.widget.Toast", curActivity);
toast.CallStatic<AndroidJavaObject>("makeText", curActivity, str, 0).Call("show");
}));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment