如何进行Unity3D与Android消息传递
1个回答
2017-02-11 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
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();
}
});
}
}
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();
}
});
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询