我们已经知道unity与iOS之间的简单交互了,那如何与Android之间进行交互呢。
使用的是Android Studio
首先创建一个Android工程,记住Package name和Application name
选择SDK API 的最小版本,记住
创建一个新工程,然后在unity的Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes路径下找到Classes.jar文件
拷贝Classes文件到libs文件夹下
add as library(添加包)
application —>library
applicationId 这一行删掉
修改后的build.gradle文件
修改 AndroidManifest.xml
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true">
在valus文件里只留一个string.xml文件即可
修改main_activity.xml文件,将没用的删掉
书写MainActivity
import android.os.Vibrator;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.unity3d.player.UnityPlayerActivity;
import com.unity3d.player.UnityPlayer;
public class MainActivity extends UnityPlayerActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
}
public String ShowDialog(final String _title,final String _content)
{
runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(_title).setMessage(_content).setPositiveButton("Down",null);
builder.show();
}
});
return "Java return";
}
//定义一个现实Toast方法,在unity中调用
public void ShowToast(final String mStr2Show){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),mStr2Show,Toast.LENGTH_LONG).show();
Log.e("msg","success+++++++++++++++++++");
}
});
}
//定义一个手机振动的方法,Unity中调用此方法
public void SetVibrator(){
Vibrator mVibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
mVibrator.vibrate(new long[]{200,2000,2000,200,200,200},-1);
}
//给unity传递数据
public void callUnityFunc(String _objName,String _funcStr,String _content){
UnityPlayer.UnitySendMessage(_objName,_funcStr,"Come from:" + _content);
Log.e("_ojbName",_objName);
Log.e("_funcStr",_funcStr);
Log.e("_content",_content);
Log.e("msg","success+++++++++++++++++++");
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour {
private Text mText;
// Use this for initialization
void Start () {
mText = GameObject.Find("MsgText").GetComponent();
}
// Update is called once per frame
void Update () {
}
public void getActivity(){
AndroidJavaClass jc = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic ("currentActivity");
//jo.Call ("StartActivity0","123");
string[] mObject = new string[2];
mObject[0] = "Jar4Android";
mObject[1] = "WOW,It worked";
//unity调用Android方法
//string ret = jo.Call ("ShowDialog",mObject);
jo.Call("ShowToast","123");
Debug.Log ("1111111111111111111111111");
}
public void MyShowToast(){
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic("currentActivity");
//unity调用Android方法
jo.Call("callUnityFunc", "Main Camera", "BeCallFunc", "yangx");
Debug.Log ("222222222222222222222222222");
}
public void BeCallFunc(string _content)
{
AndroidJavaClass jc = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic ("currentActivity");
jo.Call ("ShowToast","123");
Debug.Log ("333333333333333333333333333333");
setMsg(ref _content);
}
private void setMsg(ref string _str)
{
mText.text = _str;
}
}
根据自己Android里面的设置填好就可以了(我太懒,在网上随便截了一张图)