zoukankan      html  css  js  c++  java
  • 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:

      

      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 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")); 
    
            } 
        } 
    }
  • 相关阅读:
    mysql命令集锦
    linux 删除文件名带括号的文件
    linux下的cron定时任务
    struts2文件下载的实现
    贴一贴自己写的文件监控代码python
    Service Unavailable on IIS6 Win2003 x64
    'style.cssText' is null or not an object
    "the current fsmo could not be contacted" when change rid role
    远程激活程序
    新浪图片病毒
  • 原文地址:https://www.cnblogs.com/tekkaman/p/7738239.html
Copyright © 2011-2022 走看看