CESDK Android环境搭建及相互通讯

欲善其事先利其器,开发Android的Plugin之前要先把基础的环境搭建好.

Intellij IDEA

从Flash开始使用的IDE.当让用他来写Android部分代码了. (PS: Unity开发可以使用旗下的Rider,很好用).

新建一个Android工程,类型选择Add No Activity

QQ20171205-151549

新建成功后选择New->Module

QQ20171205-151801

选择Android Library

QQ20171205-151729

修改新建的Library的build.gradle,再最下方加入

1
2
3
4
5
6
7
8
9
10
11
task deleteOldJar(type: Delete) {
delete 'release/CESDK.jar'
}
// task to export contents as jar
task exportJar(type: Copy) {
from('build/intermediates/bundles/debug/')
into('release/')
include('classes.jar')
rename('classes.jar', 'CESDK.jar')
}
exportJar.dependsOn(deleteOldJar, build)

最终效果大概如下(根据每人的环境会有不同)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
apply plugin: 'com.android.library'

android {
compileSdkVersion 26
buildToolsVersion "26.0.1"

defaultConfig {
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
testCompile 'junit:junit:4.12'
}

task deleteOldJar(type: Delete) {
delete 'release/CESDK.jar'
}
// task to export contents as jar
task exportJar(type: Copy) {
from('build/intermediates/bundles/debug/')
into('release/')
include('classes.jar')
rename('classes.jar', 'CESDK.jar')
}
exportJar.dependsOn(deleteOldJar, build)

随便在src/main/java/你的包名下面写一个Class.然后在该build.gradleexportJartask上右键,选择Run当前的Task

QQ20171205-152459

一切正常的情况下,会在会在当前Module的Release目录下找到CESDK.jar

将此文件复制到Unity工程下的任意目录即可

Unity5.0以后可以使用任意目录,之前版本需要放置在Assets\Plugins\Android目录下

此时就完成了Jar包构建部分的搭建.

相互通讯

Java部分代码

Test.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package eran.cesdk;

public class Test {

public static String GetStringMessage(int _num) {
return "Message from CESDK -> num is : " + _num;
}

private CEPluginCallback mCallBack;

public void Initialize(CEPluginCallback _callback) {
mCallBack = _callback;
mCallBack.ShowMsg("CESDK init complete v1");
}

public String GetStringMessage2(int _num) {
return "Message2 from CESDK -> num is : " + _num;
}
}

CEPluginCallback.class

1
2
3
4
5
package eran.cesdk;

public interface CEPluginCallback {
void ShowMsg(String _msg);
}

Unity部分代码

CEPluginCallback.cs

1
2
3
4
5
6
7
8
9
10
11
public class CEPluginCallback : AndroidJavaProxy
{
public CEPluginCallback() : base("eran.cesdk.CEPluginCallback")
{
}

public void ShowMsg(string _msg)
{
CELog.Log(_msg);
}
}

TestButton.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public void OnClickBtn1()
{
CELog.Log("Android function is to be called -> v5");

//调用Static函数
var ajc = new AndroidJavaClass("eran.cesdk.Test"); //(1)
var msg = ajc.CallStatic<string>("GetStringMessage", 66); //(2)

CELog.Log(msg);

//New Object方式调用
var jo = new AndroidJavaObject("eran.cesdk.Test");

//Unity->Android Function
//Android Function->Android Proxy->Unity
jo.Call("Initialize", new CEPluginCallback());

//普通调用
msg = jo.Call<string>("GetStringMessage2", 77);

CELog.Log(msg);
}

直接打包APK,在模拟器或真机运行. 效果如下:

QQ20171205-153927

坚持原创技术分享,您的支持将鼓励我继续创作!