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")); 
    
            } 
        } 
    }
  • 相关阅读:
    模拟退火求最小覆盖圆和最小覆盖球
    牛客SQL题解-请你对于表actor批量插入如下数据(不能有2条insert语句哦!)
    牛客SQL题解-创建一个actor表,包含如下列信息
    牛客SQL题解-将employees表的所有员工的last_name和first_name拼接起来作为Name,中间以一个空格区分
    牛客SQL题解-查找描述信息中包括robot的电影对应的分类名称以及电影数目
    牛客SQL题解-汇总各个部门当前员工的title类型的分配数目
    JavaScript
    JavaScript-给代码添加注释
    牛客SQL题解-获取员工其当前的薪水比其manager当前薪水还高的相关信息
    牛客SQL题解-获取所有非manager员工当前的薪水情况
  • 原文地址:https://www.cnblogs.com/tekkaman/p/7738239.html
Copyright © 2011-2022 走看看