1.首先打开https://developer.android.com/studio/projects/android-library学习如何创建一个Android Library
2.打开Android Studio 创建项目
- 输入项目名称
- 输入Company domain或者用默认的
- 点击next
3.点击next
4.因为是创建Android plugin 所以选择Add No Activity点击Finish
5.创建的项目目录如下
6.
+点击File—》New—》New Module 弹出下面界面
+ 选择Android Library
+ 点击Next
7.
+ 输入Application/Library name
+ 点击Finish
+ 创建
8.创建后的目录
9.创建JavaClass
10.
+ 输入名称
+ 点击OK
11. 脚本内容
package com.wawagame.app.game.mediaplayerplugin; import android.util.Log; public class TutorialClass { public void LogNativeAndroidLogcatMessage(){ Log.d("Unity", "Native LogCcat Message!!!"); } }
12.Make Moudule Library(jar 包)
+ BuildàMake Module ’mediaplayerplugin’
E:workandroidStudiomediaplayerpluginuildintermediatespackaged-classesdebug
13.在Android Sudio
双击打开:build.gradle(Module:mediaplayerplugin)
14.将下面代码复制到build.gradle建中
task exprotJar(type: Copy){ from('build/intermediates/packaged-classes/debug/classes.jar') into('../../../../../../unity/PluginTurial/Assets/Plugins/Android') rename('classes.jar','MediaPlayerPlugin.jar') } task deleteOldJar(type:Delete){ delete('../../../../../../unity/PluginTurial/Assets/Plugins/Android/MediaPlayerPlugin.jar') } exprotJar.dependsOn(deleteOldJar)
15.
afterEvaluate { assembleDebug.finalizedBy(exprotJar) //build.finalizedBy(exprotJar) }
16.如下图操作这样就讲classes.jar复制并重命名到Unity项目中了
17.在unity中调用javaClass
有两种方式:
1). AndroidJavaClass对象(用法见Unity Api)
2).AndroidJavaObject 对象(用法见Unity Api)
+在unity创建C#
private AndroidJavaObject javaClass; public Text myText; // Use this for initialization void Start () { javaClass = new AndroidJavaObject("com.wawagame.app.game.mediaplayerplugin.TutorialClass"); javaClass.Call("LogNativeAndroidLogcatMessage"); string a = javaClass.Call<string>("LogNativeAndroidLogcatMessageReturn"); Debug.Log("a=============" + a); myText.text = javaClass.Call<string>("LogNativeAndroidLogcatMessageReturn"); } // Update is called once per frame void Update () { }
二.Java 调用Unity Function
1. android Project 需要一个Unity classes.jar文件
2.复制classes.jar文件
+ 如果你的打包方式为Mono
复制文件是:C:ProgramFilesUnityEditorDataPlaybackEnginesAndroidPlayerVariationsmonoReleaseClasses classes.jar
+ 如果你的打包方式为L2CPP
复制文件是:C:ProgramFilesUnityEditorDataPlaybackEnginesAndroidPlayerVariationsil2cppReleaseClasses classes.jar
3.将classes.jar文件复制到如下图所示的文件中
4.在build.gradle(Module:mediaplayerplugin)中加入下面代码
include('clasess.jar')
5.调用unity使用的对象com.unity3d.player.UnityPlayer (在Unity Api 说明Building Plugins for Android可以看到)
使用方法:com.unity3d.player.UnityPlayer.UnitySendMessage
6.首次使用没有自动导入怎们办
+ File—》Settings—》Anto Import
+ 勾上Add unambiguous imports on ths fly和 Auto import in completion
+ 点击Apply然后点击OK
7.在Android Sudio中的代码:
public void CallAorB(String value){ if("A".equals((value))){ DoSomethingA(); }else if("B".equals(value)){ DoSomethingB(); } } public void DoSomethingA() { UnityPlayer.UnitySendMessage("MyGameObject","ChangeTextA","1111111"); } public void DoSomethingB() { UnityPlayer.UnitySendMessage("MyGameObject","ChangeTextB","2222222"); }
8.在Unity中的代码:
public void CallJavaFunction(string value) { javaClass.Call("CallAorB", value); } public void OnABtnClick() { CallJavaFunction("A"); } public void OnBBtnClick() { CallJavaFunction("B"); }