Unity调用安卓Native(Android Studio)
2017-06-29 本文已影响0人
三十五岁是码农
最后有项目下载地址
引入Unity的class.jar
Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes.jar
导出.jar包
在AS中进行操作
PopupManager.java
package com.xxx.xxx;//Unity中的Bundle Idendtifier
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnKeyListener;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import com.unity3d.player.UnityPlayer;
public class PopupManager
{
public static void ShowMessagePopup(String title, String message, String okButtonText) {
AlertDialog.Builder messagePopup = new AlertDialog.Builder(new ContextThemeWrapper(UnityPlayer.currentActivity, GetTheme()));
messagePopup.setTitle(title);
messagePopup.setMessage(message);
messagePopup.setPositiveButton(okButtonText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
UnityPlayer.UnitySendMessage("AndroidMessagePopup", "OnMessagePopUpCallBack", "0");
}
});
messagePopup.setOnKeyListener(KeyListener);
messagePopup.setCancelable(false);
messagePopup.show();
}
public static void ShowDialogPopup(String title, String message, String yesButtonText, String noButtonText) {
AlertDialog.Builder dialogPopupBuilder = new AlertDialog.Builder(new ContextThemeWrapper(UnityPlayer.currentActivity, GetTheme()));
dialogPopupBuilder.setTitle(title);
dialogPopupBuilder.setMessage(message);
dialogPopupBuilder.setPositiveButton(yesButtonText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
UnityPlayer.UnitySendMessage("AndroidDialogPopup", "OnDialogPopUpCallBack", "0");
}
});
dialogPopupBuilder.setNegativeButton(noButtonText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
UnityPlayer.UnitySendMessage("AndroidDialogPopup", "OnDialogPopUpCallBack", "1");
}
});
dialogPopupBuilder.setOnKeyListener(KeyListener);
dialogPopupBuilder.setCancelable(false);
dialogPopupBuilder.show();
}
public static void ShowRatePopup(String title, String message, String yesButtonText, String laterButtonText, String noButtonText) {
AlertDialog.Builder ratePopupBuilder = new AlertDialog.Builder(new ContextThemeWrapper(UnityPlayer.currentActivity, GetTheme()));
ratePopupBuilder.setTitle(title);
ratePopupBuilder.setMessage(message);
ratePopupBuilder.setPositiveButton(yesButtonText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
UnityPlayer.UnitySendMessage("AndroidRateUsPopUp", "OnRatePopUpCallBack", "0");
}
});
ratePopupBuilder.setNegativeButton(noButtonText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
UnityPlayer.UnitySendMessage("AndroidRateUsPopUp", "OnRatePopUpCallBack", "2");
}
});
ratePopupBuilder.setNeutralButton(laterButtonText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
UnityPlayer.UnitySendMessage("AndroidRateUsPopUp", "OnRatePopUpCallBack", "1");
}
});
ratePopupBuilder.setOnKeyListener(KeyListener);
ratePopupBuilder.setCancelable(false);
ratePopupBuilder.show();
}
@SuppressLint("InlinedApi")
private static int GetTheme(){
int theme = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
theme = android.R.style.Theme_Material_Light_Dialog;
} else {
theme = android.R.style.Theme_Holo_Dialog;
}
return theme;
}
public static void OpenAppRatingPage(String url) {
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
UnityPlayer.currentActivity.startActivity(intent);
}
public static void OpenWebPage(String webUrl){
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(webUrl));
UnityPlayer.currentActivity.startActivity(browserIntent);
}
private static DialogInterface.OnKeyListener KeyListener = new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.d("AndroidNative", "AndroidPopUp");
UnityPlayer.UnitySendMessage("AndroidMessagePopup", "OnMessagePopUpCallBack", "0");
UnityPlayer.UnitySendMessage("AndroidDialogPopup", "OnDialogPopUpCallBack", "1");
UnityPlayer.UnitySendMessage("AndroidRateUsPopUp", "OnRatePopUpCallBack", "2");
dialog.dismiss();
}
return false;
}
};
}
//简化版的
package cn.gfplay.LingFeiBaoDingMahjong;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnKeyListener;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import com.unity3d.player.UnityPlayer;
public class PopupManager
{
public static void ShowDialogPopup(String title, String message, String yesButtonText, String noButtonText) {
AlertDialog.Builder dialogPopupBuilder = new AlertDialog.Builder(new ContextThemeWrapper(UnityPlayer.currentActivity, GetTheme()));
dialogPopupBuilder.setTitle(title);
dialogPopupBuilder.setMessage(message);
dialogPopupBuilder.setPositiveButton(yesButtonText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
UnityPlayer.UnitySendMessage("AndroidDialogPopup", "OnDialogPopUpCallBack", "0");
}
});
dialogPopupBuilder.setNegativeButton(noButtonText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
UnityPlayer.UnitySendMessage("AndroidDialogPopup", "OnDialogPopUpCallBack", "1");
}
});
dialogPopupBuilder.setOnKeyListener(KeyListener);
dialogPopupBuilder.setCancelable(false);
dialogPopupBuilder.show();
}
@SuppressLint("InlinedApi")
private static int GetTheme(){
int theme = 0;
/*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
theme = android.R.style.Theme_Material_Light_Dialog;
} else {
theme = android.R.style.Theme_Holo_Dialog;
}*/
theme=android.R.style.Theme_Black;
return theme;
}
public static void OpenWebPage(String webUrl){
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(webUrl));
UnityPlayer.currentActivity.startActivity(browserIntent);
}
private static DialogInterface.OnKeyListener KeyListener = new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.d("AndroidNative", "AndroidPopUp");
//UnityPlayer.UnitySendMessage("AndroidMessagePopup", "OnMessagePopUpCallBack", "0");
UnityPlayer.UnitySendMessage("AndroidDialogPopup", "OnDialogPopUpCallBack", "1");
//UnityPlayer.UnitySendMessage("AndroidRateUsPopUp", "OnRatePopUpCallBack", "2");
dialog.dismiss();
}
return false;
}
};
}
build.gradle修改
apply plugin: 'com.android.library'
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
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(include: ['*.jar'], dir: 'libs')
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.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:26.+'
testCompile 'junit:junit:4.12'
compile files('libs/classes.jar')
}
//task to delete the old jar
task deleteOldJar(type: Delete) { delete 'build/outputs/NativeExit.jar'}
//task to export contents as jar
task exportJar(type: Copy) {
from('build/intermediates/bundles/debug/')
into('build/outputs/')
include('classes.jar')
///Rename the jar
rename('classes.jar', 'NativeExit.jar')
}
exportJar.dependsOn(deleteOldJar, build)
02.将AS中的.jar引入unity中
string CLASS_NAME = "com.xxx.xxx.PopupManager";
AndroidJavaObject bridge = new AndroidJavaObject(CLASS_NAME);
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject act = jc.GetStatic<AndroidJavaObject>("currentActivity");
act.Call("runOnUiThread", new AndroidJavaRunnable(() =>
{
bridge.CallStatic(methodName, args);
}));
这是unity与Android交互的部分,把Android中的方法注册过来,在Unity中调用