运行环境
unity2019.2.0
Androidstudio3.5.2
基础的不说了,直接上代码
MainActivity
package com.gzpi.mylibrary;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.unity3d.player.UnityPlayerActivity;
public class MainActivity extends Activity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取一个按钮点击事件为跳转unity界面
textView = findViewById(R.id.button_to_unity);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, mActivity.class);
startActivity(intent);
}
});
}
}
mActivity
package com.gzpi.mylibrary;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;
public class mActivity extends UnityPlayerActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
testSendMessage();
}
// 向unity发送信息
public void testSendMessage()
{
UnityPlayer.UnitySendMessage("unityObject", "fromAndroid", "test");
}
// 接收unity信息
public void ShowToast()
{
// unity点击按钮跳转回安卓界面
Intent intent = new Intent(this, MainActivity.class);
this.startActivity(intent);
}
}
AndroidMainifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gzpi.mylibrary">
<application
android:allowBackup="true"
android:label="调用程序测试"
android:supportsRtl="true">
<activity
android:name="com.gzpi.mylibrary.MainActivity"
android:label="调用程序测试">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
<activity android:name="com.gzpi.mylibrary.mActivity"></activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_to_unity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
就这几处代码,因为是很简单的demo其他地方不需要修改,像导入unity的jar包什么的我就不写进去了
打aar包,导入unity
这里有一步需要注意 刚打出来的aar包需要把libs文件夹下的jar包删掉,如果之后在unity打包有报错的话需要打开aar包根目录的class.jar,删掉BuildConfig文件
C#脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class test : MonoBehaviour
{
public Button btn;
AndroidJavaObject jo;
private void Awake()
{
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
btn.onClick.AddListener(CallAndroid);
}
public void CallAndroid()
{
jo.Call("ShowToast");
Debug.Log("点击按钮");
System.Console.WriteLine("点击按钮");
}
void fromAndroid(string str)
{
Debug.Log("安卓传来信息 : " + str);
System.Console.WriteLine("安卓传来信息 : " + str);
}
}
注意把BuildSetting包名换成和安卓一致的,打包搞定
有一点要注意,如果是新建模块,在新建模块里执行的话安卓包名和unity不能一致,否则会报错(个人发现,不知道是否正确,但是经过实践确实是这样)