在 Unity 專案中建立 *.cs 檔,插入以下程式碼:
AndroidJavaClass UnityPlayer;
AndroidJavaObject currentActivity;
AndroidJavaObject context;
string toastString;
void Start ()
{
if (Application.platform == RuntimePlatform.Android)
{
UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
context = currentActivity.Call<AndroidJavaObject>("getApplicationContext");
}
this.toastString = "Hello Unity!";
currentActivity.Call("runOnUiThread", new AndroidJavaRunnable(showToast));
}
void showToast()
{
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");
}
沒有留言:
張貼留言