Building and using plug-ins for Android

犀渠玉剑良家子,白马金羁侠少年。这篇文章主要讲述Building and using plug-ins for Android相关的知识,希望能为你提供帮助。
【Building and using plug-ins for Android】
1、AAR plug-ins and Android Libraries
android Archive (AAR) plug-ins are bundles that include compiled java and native (C/C++) code, resources, and an Android Manifest. The .aar file itself is a zip archive which contains all of the Assets. 
To add an AAR plug-in to your project, copy the .aar file into any of your project folders, then select it in Unity to open the Import Settings in the Inspector window. Tick the  Android  checkbox to mark this .aar file as compatible with Unity:

Building and using plug-ins for Android

文章图片

AAR is the recommended plug-in format for Unity Android applications.
2)Android Library
Android Library projects are similar to AAR plug-ins: they contain native and Java code, resources, and an Android Manifest. However, an Android Library is not a single archive file, but a directory with a special structure which contains all of the Assets. 
Unity treats any subfolder of Assets/Plugins/Android as a potential Android Library, and disables Asset importing from within these subfolders. The subfolder is recognized as an Android Library if it contains the AndroidManifest.xml file, and the project.properties file contains the string  android.library=true.
3)Providing additional Android Assets and resources
【Building and using plug-ins for Android】If you need to add Assets to your Unity application that should be copied unchanged into the output package, import them into the Assets/Plugins/Android/assets directory. They appear in the assets/ directory of your APK, and are accessed by using the  getAssets()  Android API from your Java code.
2、JAR plug-ins
They can only contain Java code (for example, they can’t contain Android resources), which makes their use very limited.
To add a JAR plug-in to your project, copy the .jar file into any of your project folders, then select it in Unity to open the Import Settings in the Inspector window. Tick the Android checkbox to mark this .jar file as compatible with Android:
Building and using plug-ins for Android

文章图片

Using Java plug-ins Unity uses the  Java Native Interface (JNI)  both when calling code from Java and when interacting with Java or the Java VM(Virtual Machine) from native code or C# scripts.
2)Using your Java plug-in from C# scripts with helper classes
The  AndroidJNIHelper  and  AndroidJNI  Unity API classes are used as a wrapper around a “raw” JNI interface.
The AndroidJavaObject and AndroidJavaClass Unity API classes automate a lot of tasks when using JNI calls, and they also use caching to make calls to Java faster. The combination of  AndroidJavaObject  and  AndroidJavaClass  is built on top ofAndroidJNI  and  AndroidJNIHelper, but also has some additional functionality.
Building and using plug-ins for Android

文章图片
Building and using plug-ins for Android

文章图片
AndroidJavaObject jo = new AndroidJavaObject("java.lang.String", "some_string"); // jni.FindClass("java.lang.String"); // jni.GetMethodID(classID, "< init> ", "(Ljava/lang/String; )V"); // jni.NewStringUTF("some_string"); // jni.NewObject(classID, methodID, javaString); int hash = jo.Call< int> ("hashCode"); // jni.GetMethodID(classID, "hashCode", "()I"); // jni.CallIntMethod(objectID, methodID);

View Code
Building and using plug-ins for Android

文章图片
Building and using plug-ins for Android

文章图片
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); // jni.FindClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject jo = jc.GetStatic AndroidJavaObject> ("currentActivity"); // jni.GetStaticFieldID(classID, "Ljava/lang/Object; "); // jni.GetStaticObjectField(classID, fieldID); // jni.FindClass("java.lang.Object"); Debug.Log(jo.Call AndroidJavaObject> ("getCacheDir").Call< string> ("getCanonicalPath")); // jni.GetMethodID(classID, "getCacheDir", "()Ljava/io/File; "); // or any baseclass thereof! // jni.CallObjectMethod(objectID, methodID); // jni.FindClass("java.io.File"); // jni.GetMethodID(classID, "getCanonicalPath", "()Ljava/lang/String; "); // jni.CallObjectMethod(objectID, methodID); // jni.GetStringUTFChars(javaString);

View Code
Building and using plug-ins for Android

文章图片
Building and using plug-ins for Android

文章图片
public class NewBehaviourScript : MonoBehaviour { void Start () { AndroidJNIHelper.debug = true; using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { jc.CallStatic("UnitySendMessage", "Main Camera", "JavaMessage", "NewMessage"); } } void JavaMessage(string message) { Debug.Log("message from java: " + message); } }

View CodeThe Mono garbage collector should release all created instances of  AndroidJavaObject  and  AndroidJavaClass  after use, but it is advisable to keep them in a  using(){}  statement to ensure they are deleted as soon as possible.
//Getting the system language safely void Start () { using (AndroidJavaClass cls = new AndroidJavaClass("java.util.Locale")) { using(AndroidJavaObject locale = cls.CallStatic< AndroidJavaObject> ("getDefault")) { Debug.Log("current lang = " + locale.Call< string> ("getDisplayLanguage")); } } }

 

    推荐阅读