標籤:
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訊息