Unity直接调用Android Toast

Unity直接调用Android Toast

这两天在搭一套UI框架,想把Android的Toast直接集成上去,有不想直接打jar包,所有写了个C#直接调用,废话不多说,直接干货:

 using UnityEngine;

 public class AndroidExtend : Singleton<AndroidExtend>
{ #region 属性 /// <summary>
/// 安卓入口
/// </summary>
public AndroidJavaClass UnityPlayer { get; private set; } /// <summary>
/// 安卓主Activity
/// </summary>
public AndroidJavaObject CurrentActivity { get; private set; } #endregion #region 初始化 protected override void OnInit()
{
UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
CurrentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
} #endregion #region 外部接口 /// <summary>
/// 安卓端弹Toast
/// </summary>
/// <param name="message">内容</param>
/// <param name="duration">时长 毫秒</param>
public void Toast(string message, int duration)
{
AndroidJavaClass jc = new AndroidJavaClass("android.widget.Toast");
AndroidJavaObject toast = jc.CallStatic<AndroidJavaObject>("makeText", CurrentActivity, message, duration);
toast.Call("show");
} #endregion }

代码比较简单,直接新建一个类把代码粘进去就OK了

继续惯例:欢迎交流,QQ:1107628852(加备注)

上一篇:编译安装php时提示Cannot find MySQL header files的解决方法


下一篇:HDU1789(Doing Homework again)题解