Unity version: 5.6.0p4
AndroidStudio version: 2.2.2
1. Create a project
2. Select SDK
3. Create an empty project file
4. Name the project
5. Switch project mode
6. Import the Unity library file class.jar
Win路径
:
C:\ProgramFiles\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\classes.jar
Mac path
:
/Applications/Unity/PlaybackEngines/AndroidPlayer/Variations/mono/Release/Classes\classes.jar
7. Reference the class.jar file
8. Modify the build.gradle file
(1) Change
apply plugin: 'com.android.application'
to
apply plugin: 'com.android.library'
.
Only in this format can
AndroidStudio export the aar files that Unity needs to use.
(2)
Delete
the applicationId under
defaultConfig
(3) Delete
the introduction of compile 'com.android.support:appcompat-v7:25.1.0' under
dependencies
After modification:
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile files('libs/classes.jar')
}
9. Delete the styles.xml file
10. Modify the AndroidManifest.xml file
After modification:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wangby.Android" >
<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true" >
<activity android:name="com.wangby.Android.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
</application>
</manifest>
11. Modify the MainActivity file
After modification:
package com.wangby.Android;
//import android.support.v7.app.AppCompatActivity;//这里不需要引用这个android库文件
import android.os.Bundle;
import com.unity3d.player.UnityPlayerActivity;
import android.app.AlertDialog;
import android.widget.Toast;
import android.os.Vibrator;
import com.unity3d.player.UnityPlayer;
public class MainActivity extends UnityPlayerActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);//注释掉这句话,这句话的意思是:启动界面是Android中的这个activity_main
}
// 调用弹出Android一个窗口
public String ShowDialog(final String _title, final String _content){
runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(_title).setMessage(_content).setPositiveButton("Down", null);
builder.show();
}
});
return "Java return";
}
// 定义一个显示Toast的方法,在Unity中调用此方法
public void ShowToast(final String mStr2Show) {
// 同样需要在UI线程下执行
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),mStr2Show, Toast.LENGTH_LONG).show();
}
});
}
// 定义一个手机振动的方法,在Unity中调用此方法
public void SetVibrator() {
Vibrator mVibrator=(Vibrator)getSystemService(VIBRATOR_SERVICE);
mVibrator.vibrate(new long[]{200, 2000, 2000, 200, 200, 200}, -1); //-1:表示不重复 0:循环的震动
}
// arg1:unity中的对象名字,记住是对象名字,不是脚本类名
// arg2: 函数名
// arg3: 传给函数的参数,目前只看到一个参 数,并且是string的,自己传进去转吧
public void callUnityFunc(String _objName , String _funcStr, String _content) {
UnityPlayer.UnitySendMessage(_objName, _funcStr, _content);
}
}
12. Compile
13. Open the compiled arr file
Path: project/app/build/outputs/aar/app-debug.arr
These are the files needed
14. Import into Unity
15. Create script AndroidTest
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AndroidTest : MonoBehaviour {
[SerializeField] private Text _label = null;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void myShowDialog () {
print ("myShowDialog_1");
AndroidJavaClass androidJavaClass = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
AndroidJavaObject androidJavaObject = androidJavaClass.GetStatic<AndroidJavaObject> ("currentActivity");
string [] mObject = new string [2];
mObject [0] = "小菜菜";
mObject [1] = "大春绿";
string ret = androidJavaObject.Call<string> ("ShowDialog", mObject);
setMessage (ref ret);
print ("myShowDialog_2");
}
public void myShowToast () {
print ("myShowToast_1");
AndroidJavaClass androidJavaClass = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
AndroidJavaObject androidJavaObject = androidJavaClass.GetStatic<AndroidJavaObject> ("currentActivity");
androidJavaObject.Call ("ShowToast", "你是不是傻");
print ("myShowToast_2");
}
/// <summary>
/// arg0: 调用android中的方法
/// arg1: 对象名
/// arg2: 函数名
/// arg3: 参数1
/// </summary>
public void myInteraction () {
print ("myInteraction_1");
AndroidJavaClass androidJavaClass = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
AndroidJavaObject androidJavaObject = androidJavaClass.GetStatic<AndroidJavaObject> ("currentActivity");
androidJavaObject.Call ("callUnityFunc", "ReceiveObject", "receiveMessage", "此系统表示不喜欢你,并表示你长得很丑...");
print ("myInteraction_2");
}
public void receiveMessage (string message) {
print ("receiveMessage_:" + message);
setMessage (ref message);
}
public void setMessage (ref string content) {
print ("setMessage_:" + content);
_label.text = content;
}
}
16, Department UI
17. Configure SDK, JDK, NDK
18. Publishing options
19. APK file after release
20. Effect after release