Building and using plug-ins for Android

來源:互聯網
上載者:User

標籤:lang   direct   技術分享   pre   sys   char   providing   include   some   

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:

  

  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

  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:

  

  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.

 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
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
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 Code

  The 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 safelyvoid Start () {     using (AndroidJavaClass cls = new AndroidJavaClass("java.util.Locale")) {         using(AndroidJavaObject locale = cls.CallStatic<AndroidJavaObject>("getDefault")) {             Debug.Log("current lang = " + locale.Call<string>("getDisplayLanguage"));         }     } }

 

Building and using plug-ins for Android

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.