1)新建空的AndroidStudio工程,但是新建过程时最小SDK版本要与unity一致,如下图所示,本次操作均为api16
2)创建Library,如下图所示,新建module,然后选择Android Library。
新建模块为UnityPlugin,如下图所示,注意此时的PackageName后续用得到
4)引入unity的classes.jar
按下图所示,找到Unity安装目录下的classes.jar文件,并放入上述工程中\unityplugin\libs下(即新建Module下的libs下)
选择File-ProjectStructure,调出Project Structure界面,依次选择unityplugin(上述建立的module)-Dependencies,然后点击右侧加号-Jar dependences,并选择libs下引入的classes.jar,如下图所示,确定引入。
5)添加插件代码
在com.idea.unityplugin下新建类UnityPlugin
在类UnityPlugin(继承UnityPlayerActivity )中添加如下代码
package com.idea.unityplugin;import android.os.Bundle; import android.widget.Toast;import com.unity3d.player.UnityPlayerActivity;public class UnityPlugin extends UnityPlayerActivity {@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);}// 传入数据,并通过toast显示public void ShowToast(final String message){runOnUiThread(new Runnable() {@Overridepublic void run() {Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();}});}// 返回一个字符串(静态方法)public static String GetInformation(){return "Android Static Msg";}// 返回一个字符串(非静态方法)public String GetInfo(){return "Android Msg";} }
6)生成jar或者aar包
点击build.gradle(此插件的gradle),添加如下代码,操作如下图所示。
task makeJar(type: Copy) {delete 'build/libs/unityplugin.jar'from('build/intermediates/bundles/release/')into('build/libs/')include('classes.jar')rename('classes.jar', 'unityplugin.jar')
}makeJar.dependsOn(build)
点击Terminal,输入gradlew makeJar回车,等待执行结束即可,即可在\unityplugin\build\outputs\aar下目录下找到.aar文件。
7)将6)中aar文件放入unity中Assets文件下Plugins\Android目录下,同时新建AndroidManifest.xml文件,并添加如下内容
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.xxxx.androidplugin"android:versionCode="1"android:versionName="1.0"><applicationandroid:allowBackup="true"android:label="@string/app_name"android:supportsRtl="true"><activity android:name="com.XXXX.unityplugin.UnityPlugin"android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>
注意事项如下图所示
8)调用代码
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;public class CallAndroidMethod : MonoBehaviour {public Button[] buttons;public Text text;private void ShowAndroidToast(){//text.text = "Toast ";using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")){using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity")){text.text = "Toast:";jo.Call("ShowToast", "Android Toast");}}//using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))//{// text.text += " Toast1 ";// using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("crrentActivity"))// {// text.text += " Toast2 ";// //jo.Call("ShowToast", "Android Toast");// jo.CallStatic<string>("GetInformation");// text.text += " Toast3 ";// }//} }private void GetInfomation(){string info = "";using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")){using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity")){text.text = "Static:";info = jo.CallStatic<string>("GetInformation");}}text.text += info;}private void GetInfo(){string info = "";using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")){using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity")){text.text = "Nonstatic:";info = jo.Call<string>("GetInfo");}}text.text += info;}// Use this for initializationvoid Start (){buttons[0].onClick.AddListener(ShowAndroidToast);buttons[1].onClick.AddListener(GetInfomation);buttons[2].onClick.AddListener(GetInfo);} }
然后发布apk即可用了
出现问题:
1)CommandInvokationFailure: Gradle build failed.
解决:修改Build System为Internal
2)IOException: Failed to Move File / Directory from 'Temp/StagingArea\android-libraries\app-debug\classes.jar' to 'Temp/StagingArea\android-libraries\app-debug\libs\classes.jar'.
解决:将插件aar文件用解压软件打开(直接双击),并不解压,然后删除libs下的classed.jar即可。
参考文献:
https://blog.csdn.net/qq_16763249/article/details/81876038(非MainActivity调用方法,纯java类调用)
https://blog.csdn.net/qq_28775437/article/details/78605691?locationNum=3&fps=1(打包报错问题汇总)
https://blog.csdn.net/qiu_zhongya/article/details/51419456(主要参考文献_原文)