如何进行Unity3D与Android消息传递
2016-12-10 · 知道合伙人软件行家
关注
展开全部
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();
}
});
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询