如何进行Unity3D与Android消息传递

2024-11-23 01:29:02
推荐回答(1个)
回答1:

UnityTestActivity.java 主Activity ,提供一个接口方法StartActivity(),它是由Unity中调用
package com.xys;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.unity3d.player.UnityPlayerActivity;

public class UnityTestActivity extends UnityPlayerActivity {
/** Called when the activity is first created. */

Context mContext = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;

}

public void StartActivity0(String name)
{
Intent intent = new Intent(mContext,TestActivity0.class);
intent.putExtra("name", name);
this.startActivity(intent);
}

}
接着是TestActivity.java 打开这个界面后,程序将在这里向Unity发送消息。
package com.xys;

import com.unity3d.player.UnityPlayer;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TestActivity0 extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TextView text = (TextView)this.findViewById(R.id.textView1);
text.setText(this.getIntent().getStringExtra("name"));

final EditText edit = (EditText)this.findViewById(R.id.edit);

Button close = (Button)this.findViewById(R.id.button0);
close.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
//注释1
UnityPlayer.UnitySendMessage("Main Camera","messgae",edit.getText().toString());
TestActivity0.this.finish();
}
});
}

}