AndroidStudio Export Jar For Unity
环境:AndroidStudio 2.1.1 + Unity5.4.0f1
大体步骤如下:
- 创建AndroidStudio工程
- 生成jar
- 将jar导入Unity工程
废话不多说,一步一步完成就好……
创建AndroidStudio工程
1.1 设置好名称、路径、包名。
1.2 选择SDK
1.3 选择 Empty Activity
1.4 不改名,直接finish
到这里,工程就创建完毕了,接下来创建一个Module来封装几个简单的方法,然后生成Jar文件。
生成jar
2.1 创建Module 点击File->New->New Module
2.2 选择Anroid Library
2.3 输入Library的名字和包名,爱输啥输啥,看喜好,这里我的包名书写的和新建的项目包名不同,这里为org.lzy.test.myjar
2.4 找到Unity中的classes.jar导入到AndroidStudio工程,那个文件路径大约在Unity安装路径的这里(5.x版本的Unity):
\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes\classes.jar
找到后复制这个文件到刚才新建的Library中的libs文件中。
如果视图是Android,不是Project,可以切换到Project标签。点击标签的小三角就能看到
2.5 把classes.jar加入依赖。点击File->Project Structure
2.6 Modules选择自己新建的Android Library,中间栏点Dependencies标签,然后点击右侧加号+,选择File Dependency,然后找到自己拷贝进来的在libs目录下的classes.jar点击OK
2.7 新建Class。
名字为:MyExportActivity
MyExportActivity.java
package org.lzy.test.myjar;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Vibrator;
import com.unity3d.player.UnityPlayerActivity;
public class MyExportActivity extends UnityPlayerActivity {
public void showDialog(final String mTitle, final String mContent) {
runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MyExportActivity.this);
mBuilder.setTitle(mTitle)
.setMessage(mContent)
.setPositiveButton("确定", null);
mBuilder.show();
}
});
}
public void shareText(String subject, String body) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
public void setVibrator() {
Vibrator mVibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
mVibrator.vibrate(new long[]{200, 2000, 2000, 200, 200, 200}, -1);
}
}
2.8 双击build.gradle,修改代码,注意修改的是自己新建的Library的build.gradle,不是整个project的build.gradle
build.gradle
//indicates that this is a library
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
sourceSets {
main {
//Path to your source code
java {
srcDir 'src/main/java'
}
}
}
defaultConfig {
minSdkVersion 21
targetSdkVersion 23
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
lintOptions {
abortOnError false
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile files('libs/classes.jar')
}
//task to delete the old jar
task deleteOldJar(type: Delete) {
delete 'release/AndroidPlugin.jar'
}
//task to export contents as jar
task exportJar(type: Copy) {
from('build/intermediates/bundles/release/')
into('release/')
include('classes.jar')
///Rename the jar
rename('classes.jar', 'AndroidPlugin.jar')
}
exportJar.dependsOn(deleteOldJar, build)
2.9 刷新gradle,一般情况下修改了build.gradle后,系统上方会让你SyncNow,还可以手动刷新一下,右侧点击Gradle标签,选中自己的Library,右击选择Refresh external project
2.10 生成jar,找到other下的exportJar,并双击,等待完成
完成后出现如下提示
我们可以看到在release目录已经有生成好的jar包了,右击Show in Explorer可以拿到jar包AndroidPlugin.jar
将jar导入Unity工程
创建Unity工程,制作三个按钮来测试封装的android层的三个方法
3.1 创建Unity工程,新建UI的Canvas
3.2 在Canvas下创建UI的Panel,添加一个Grid Layout Group
3.3 创建3个按钮,名为:BtnShowDialog、BtnShareText、BtnSetVibrator
3.4 编写代码,触发按钮事件调用Android方法
UIController.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UIController : MonoBehaviour
{
private AndroidJavaClass unity;
private string subject = "WORD-O-MAZE";
private string body = "PLAY THIS AWESOME. GET IT ON THE PLAYSTORE";
private Button[] btns;
void Awake()
{
if (unity == null)
{
unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
}
}
void Start()
{
btns = GetComponentsInChildren<Button>();
for (int i = 0; i < btns.Length; i++)
{
Button btn = btns[i];
btn.onClick.AddListener(delegate
{
OnBtnClick(btn.gameObject);
});
}
}
void OnBtnClick(GameObject go)
{
switch (go.name)
{
case "BtnShowDialog":
CallShowDialog();
break;
case "BtnShareText":
CallShareText();
break;
case "BtnSetVibrator":
CallSetVibrator();
break;
}
}
private void CallShowDialog()
{
AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
currentActivity.Call("showDialog", "this is my title", "this is my content");
}
private void CallShareText()
{
AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
currentActivity.Call("shareText", subject, body);
}
private void CallSetVibrator()
{
AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
currentActivity.Call("setVibrator");
}
}
3.5 将UIController.cs脚本挂到Canvas上
3.6 将刚才打好的AndroidPlugin.jar拷贝到Unity工程的Assets\Plugins\Android中,并在该文件中新建AndroidManifest.xml文件。
注意:在activity的这一句包名和类名要书写正确
activity android:name=”org.lzy.test.myjar.MyExportActivity”
此处的包名是我们新建的Library的包名:org.lzy.test.myjar,类名是书写的MyExportActivity.java的类名
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
<application android:icon="@drawable/app_icon" android:label="@string/app_name">
<activity android:name="org.lzy.test.myjar.MyExportActivity"
android:label="@string/app_name"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
3.7 设置Unity的Player Settings,注意先切换到Android,包名随便修改,一定要修改,默认的包名无法打包,然后,Build And Run吧。
3.8运行效果
showDialog
shareText
setVibrator
震动没有办法截图。。。。
参考感谢
很多人,不过都没记住