Unity2020.1 Android插件制作
之前写插件的时候搜索了大量资料,有些因为版本问题过时,有些根本就是错的,所以记录下亲测的步骤
-
-
文件→生成配置→Android→导出项目
-
基于 1 的页面,点击左下角「玩家设置」→其他设置→配置→脚本后端→IL2CPP,目标架构→ARMv7, ARM64
-
unity 偏好设置→外部工具,确保配置好 sdk 和 ndk
-
-
插件开发(可参考部分)
-
新建Android studio 项目
- Empty Activity
- 包名随意
- minimum SDK 跟 unity 项目保持一致
-
拷贝
unity
的classes.jar (/Applications/Unity/PlaybackEngines/AndroidPlayer/Variations/mono/Release/Classes\classes.jar)
到 项目app/libs
目录下,右键classes.jar→Add As Library
-
MainActivity
删除setContentView
行代码, 删除layout
下activity_main.xml
-
修改
build.gradle
apply plugin: 'com.android.application
改成apply plugin: 'com.android.library
, 删除 defaultConfig 下的 applicaionId 行 -
在
/Applications/Unity/Hub/Editor/2020.1.13f1/PlaybackEngines/AndroidPlayer/Source/com/unity3d/player/UnityPlayerActivity.java
找到UnityPlayerActivity.java
, 直接拷贝source 目录下的 com 包到 java文件夹 -
修改 AndroidManifest.xml
-
在 build.gradle 里加入依赖库,比如:
-
新建一个 class,在里面写原生代码:
package 路径; public class MyClass { private Class> unityPlayerClass; private Field unityCurrentActivity; private Method unitySendMessage; public void init () { //Get the UnityPlayer class using Reflection unityPlayerClass = Class.forName("com.unity3d.player.UnityPlayer"); //Get the currentActivity field unityCurrentActivity= unityPlayerClass.getField("currentActivity"); //Get the UnitySendMessage method unitySendMessage = unityPlayerClass.getMethod("UnitySendMessage", new Class [] { String.class, String.class, String.class } ); } //Use this method to get UnityPlayer.currentActivity public Activity currentActivity () { Activity activity = (Activity) unityCurrentActivity.get(unityPlayerClass); return activity; } public void unitySendMessageInvoke (String gameObject, String methodName, String param) { //Invoke the UnitySendMessage Method unitySendMessage.invoke(null, new Object[] { gameObject, methodName, param} ); } public void makeToast (final String toastText, final int duration) { currentActivity().runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getActivity(), toastText, duration).show(); } }); } // 别忘了调用 private void listenSDKData() { currentActivity().runOnUiThread(new Runnable() { @Override public void run() { MotrixiSDK.INSTANCE.setOnLogListener(new OnLogListener() { @Override public void onLogListener(String s) { unitySendMessageInvoke("UnityAndroidConnect", "AndroidCallUnityMsg", s); } }); } }); } }
-
Build->Make Project,一般只要
build/intermediates/arr_main_jar/debug/classes.jar
就行
用压缩包打开jar 包,删除 com 目录下的 unity3D和 项目名最底层文件夹里的 BuildConfig.class
-
把上述项目的 AndroidManifest.xml 和 导出的 classes.jar(可以改名)放到unity 中,路径如下
jar 放到
Assets/Plugins/Android/libs
下 -
在
/Applications/Unity/Hub/Editor/2020.1.13f1/PlaybackEngines/AndroidPlayer/Tools/GradleTemplates
路径下有 gradle 模板,把需要用到的放到 unity 项目 Assets/Plugins/Android 下:- BaseProjectTemplate.gradle: project 路径下的gradle
- gradleTemplate: gradle 配置
- mainTemplate: module 下的 gradle
-
Unity 创建一个GameObject,命名与自定义java class 文件中的 gameObject 名字一致, Assets 下创建一个cs文件,关联到GameObject.
cs 文件如下:
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class UnityAndroidConnect : MonoBehaviour { private string appKey = ""; // 两个按钮 private GameObject resetButton; private GameObject uploadButton; // 文本框 private GameObject informationLabel; // 文本框的文字 public Text informtaionText; // AndroidJavaClass jc; AndroidJavaObject androidInstance; // Start is called before the first frame update void Start() { try { //参数就是jar 包的项目package name androidInstance = new AndroidJavaObject("com.motrixi.unity.plugin.Motrixi"); if (null != androidInstance) { // 参数为自定义 java 文件里的方法 androidInstance.Call("initPlugin"); androidInstance.Call("init", appKey) } } catch (Exception e) { Debug.Log( "init error:" + e.ToString()); } // 获取 resetButton resetButton = GameObject.Find("ResetButton"); resetButton.GetComponent
-
导出Android项目就可以了
-