码迷,mamicode.com
首页 > 移动开发 > 详细

Android 与Unity交互之Toast消息

时间:2015-12-21 14:03:29      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

Toast.makeText(MainActivity.this,message.obj.toString(),Toast.LENGTH_SHORT).show();
这一句代码不能直接放在
public void ShowMessage(String msg){
Log.e("-------", msg);
// And this is how you call it from the worker thread:
  Toast.makeText(MainActivity.this,message.obj.toString(),Toast.LENGTH_SHORT).show();
}
来接收Unity发送过来的消息。
具体是这样的,在继承UnityPlayerActivity或UnityNativePlayerActivity的Activity中:
在onCreate中加入下列代码:

(需要import android.os.Message;)
//处理事件
mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message message) {
// This is where you do your work in the UI thread.
// Your worker tells you in the message what to do.
if(message.what == 1111){
Toast.makeText(MainActivity.this,message.obj.toString(),Toast.LENGTH_SHORT).show();
}
}
};

然后在类体中加入
public void ShowMessage(String msg){
Log.e("-------", msg);
// And this is how you call it from the worker thread:
Message message = mHandler.obtainMessage( 1111,msg);
message.sendToTarget();
}

Android 与Unity交互之Toast消息

标签:

原文地址:http://www.cnblogs.com/fuliufuliu/p/5063021.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!