Unity Android SDK 详解
一 名词解释
(1)SDK :软件开发工具包(Software Development Kit)应用软件的开发工具的集合
(2)JDK :Java 开发工具,主要提供基础类库,Java编译器,运行时JVM(Java虚拟机),调试器等
https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
(3)JRE :Java运行时环境(Java Runtime Environment)Java运行时环境,时JDK的一部分
(4)Android SDK :安卓软件开发工具包,在JDK基础上添加了附加类库android.jar,提供了Android虚拟机DVM和ART
(5)JNI :Java Native Interface JNI是Java调用Native 语言的一种规范,通过JNI可以使得Java与C/C++机型交互
(6)NDK :Native Develp Kit 官方的解释如下:Android NDK 是一套允许您使用原生代码语言(例如C和C++) 实现部分功能的工具集,简单来说就是Android除了可以用Java来实现,部分功能也可以用C++,NDK不依赖Dalvik Java虚拟机
NDK主要是把Native语言编译为动态链接库(.dll文件,.so文件,.jnilib)
(7).NET :Microsoft .NET Framework,.NET 技术框架组成在Windows系统下的具体的实现,简而言之相当于Java的jdk,即最基础的开发包,最基础的框架
(8)Mono :是.net framework的一个开源的、跨平台的实现,并非微软的产品,它也是有mono编译器和mono运行时组成的
(9)IL :将.NET代码转化为机器语言的一个中间语言,也就是说.NET代码经过编译之后不会直接生成机器语言,而是会先生成IL,类似汇编代码
二 Android程序主要组成
(1)AndroidManifest.xml:对应Unity的路径Assets/Plugins/AndroidManifest.xml Android 程序清单文件,包含应用程序名称、图标、包名称、四大组件模块、权限等
(6)libs:对应Unity的路径Assets/Plugins/Android 里面存放了Java写的类库jar和aar
三 Unity调用Android
(1)AndroidJavaClass是java.lang.Class通用实例的Unity表示形式
(2)AndroidJavaObject是java.lang.Object的通用实例的Unity表示形式
例子:
(3)Activity和UnityPlayerActivity
https://www.jianshu.com/p/476087b4c087
UnityPlayerActivity是unity Android 包的默认的Activity
(4)application
application可以被继承,下面是个例子
然后在androidmanifest中引用
(5)android 代码调用Unity函数
UnityPlayer.UnitySendMessage(string gameObject, string runtimeScript, string arg);
四 移动MM实例
application
package com.xx.Racing; import android.app.Application; import android.content.Context; import com.secneo.sdkp.DexHelper; public class MMApplication extends Application { protected void attachBaseContext(Context ctx) { super.attachBaseContext(ctx); DexHelper.install(this, "mmbilling.3.1.8.jar.protected.jar"); } }
AndroidforUnity 给unity调用的库
package com.mm.payforunity; import android.content.Context; import mm.purchasesdk.Purchase; public class PayForUnity { private static final String APPID = "300009265921"; private static final String APPKEY = "065DF5E770AE73C7ECDE5EE7FA174999"; private static IAPListener listener; public static void InitSDK(Context context, String gameObject, String runtimeScript) { try { Purchase.getInstance().setAppInfo("300009265921", "065DF5E770AE73C7ECDE5EE7FA174999"); } catch (Exception e1) { e1.printStackTrace(); } listener = new IAPListener(context, gameObject, runtimeScript); try { Purchase.getInstance().init(context, listener); } catch (Exception e) { e.printStackTrace(); } } public static void doPay(Context context, String paycode) { try { Purchase.getInstance().order(context, paycode, 1, listener); } catch (Exception e1) { e1.printStackTrace(); return; } } }
Androidmanifest
<manifest android:versionName="1.0" android:versionCode="1" package="com.gzzhengyou.Racing" xmlns:android="http://schemas.android.com/apk/res/android"> <supports-screens android:anyDensity="true" android:xlargeScreens="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true"/> <application android:label="@string/app_name" android:icon="@drawable/app_icon" android:name="com.xx.Racing.MMApplication"> <activity android:label="@string/app_name" android:name="com.xx.Racing.UnityPlayerNativeActivity" android:configChanges="orientation|keyboardHidden|screenSize" android:screenOrientation="landscape"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.SEND_SMS"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> </manifest>
unity call Android
using UnityEngine; using System; using System.Collections.Generic; public class MMBillingAndroid { #if UNITY_ANDROID private AndroidJavaClass klass = new AndroidJavaClass("com.mm.payforunity.PayForUnity"); private static MMBillingAndroid _instance; public static MMBillingAndroid Instance { get { if(_instance==null){ _instance = new MMBillingAndroid(); } return _instance; } } public void InitSDK(String gameObject, String runtimeScript) { AndroidJavaClass unityPlayer = new AndroidJavaClass ("com.unity3d.player.UnityPlayer"); AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"); curActivity.Call("runOnUiThread", new AndroidJavaRunnable(() => { klass.CallStatic("InitSDK", curActivity, gameObject, runtimeScript); })); } public void doPay(string paycode) { AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"); curActivity.Call("runOnUiThread", new AndroidJavaRunnable(() => { klass.CallStatic("doPay", curActivity, paycode); })); } #endif }