How To Use Android Plugins in Unity Apps

This article explains how to do two things:

  1. Take custom Android plugins that are written in Java and integrate them into a Vuforia-based Unity project
  2. Invoke methods defined in the Java plugin from a Unity C# script.
The Image Targets sample is a starting point for a Unity project example.

Integrate plugins into a Vuforia-based Unity project

  1. Create a Unity project. For ease of reference we refer to it as QcarWithPlugin.
  2. Import the Image Targets sample package for Unity
    (Menu>Assets >Importcustompackage)
  3. Double-click the Image-Targets scene in the project view. The three Image Targets and their associated teapot models should appear in the scene view.
  4. Create a new Android project in Eclipse. For ease of reference the project is calledMyPlugin, so it is assumed that the main activity will be calledMyPluginActivity.
  5. Right-click on the project in Eclipse, go toProperties > Java Build Path > Libraries.Add the following two libraries asexternal jars(both located under theAssets/Plugins/Android/folder of the Unity project):
    • QCAR.jar
    • QCARUnityPlayer.jar
  6. Open theMyPluginActivity.javafile in Eclipse, and make the following code changes:
    1. Make MyPluginActivity extendQCARPlayerActivityinstead of Activity. Press Ctrl-Shift-O to adjust the imports automatically.
    2. From theonCreate()method, remove the linesetContentView(…).
    3. Add a custom public method calledshowMessage()to the MyPluginActivity class. Fill the method with some code to display a Toast message with text.
The code of your MyPluginActivity should look like the following code:
package com.qualcomm.plugins; import android.os.Bundle;import android.widget.Toast;import com.qualcomm.QCARUnityPlayer.QCARPlayerActivity; public class MyPluginActivity extends QCARPlayerActivity {     @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);    }       public void showMessage(final String message) {     this.runOnUiThread(new Runnable() {   public void run() {    Toast.makeText(MyPluginActivity.this, message, Toast.LENGTH_SHORT).show();   }  });    }}

Invoke methods defined in the Java plugin

  1. Right-click on your project, go toProperties > Android, and check theIsLibrarycheckbox to turn your Android project into a library.
    This action should make Eclipse generate a .JAR file (for example,myplugin.jar) and store the file in the /binfolder of your Eclipse project.
  2. Open the/binfolder and copy the JAR library from that folder to theAssets/Plugins/Android/folder of your Unity project.
  3. Open and edit theAndroidManifest.xmlfile located in the sameAssets/Plugins/Android/ directory.
  4. Replace the name of the main activity with your fully qualified Activity name, for example, com.my.org.MyPluginActivity.
  5. Create a C# script, call itMyPluginCaller,for example, and attach it to each of your Image Targets in the scene (Chips, Stones, Tarmac, etc.).
  6. Copyand then pastethe following code in your script. The code will make your script invoke theshowMessage()method of your MyPluginActivity class whenever a target is detected:
public class MyPluginCaller : MonoBehaviour, ITrackableEventHandler {    private TrackableBehaviour mTrackableBehaviour;     void Start()    {        mTrackableBehaviour = GetComponent<TrackableBehaviour>();        if (mTrackableBehaviour)        {         mTrackableBehaviour.RegisterTrackableEventHandler(this);        }        OnTrackingLost();    }    public void OnTrackableStateChanged(TrackableBehaviour.Status previousStatus,TrackableBehaviour.Status newStatus)    {        if (newStatus == TrackableBehaviour.Status.DETECTED ||            newStatus == TrackableBehaviour.Status.TRACKED)        {            OnTrackingFound();        }        else        {            OnTrackingLost();        }    }    private void OnTrackingFound()    {  AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");  AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");  //Invoke the "showMessage" method in our Android Plugin Activity  string message = "Detected trackable: " + mTrackableBehaviour.TrackableName;  jo.Call("showMessage", message);    }    private void OnTrackingLost()    {    }}

Now the project should be ready to build and run on your Android device.

上面的方法是高通插件3.0.9的方法,最近更新到4.0.1.3,最新的方法点击下面:
原文地址点击这里

更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. Python list sort方法的具体使用
  3. python list.sort()根据多个关键字排序的方法实现
  4. android之网络请求框架OKhttp、原生http请求
  5. android加载.swf flash文件
  6. android仿苹果Iphone桌面源码
  7. android抓log方法
  8. android 登陆右上角 的关闭按钮的使用方法
  9. android 全屏 去掉标题栏

随机推荐

  1. android 资源列表-史上最有价值的android
  2. android spinner默认样式不支持换行和修
  3. Android消息机制源码分析
  4. 2020新鲜出炉的Android大厂面试题锦集(BA
  5. Android中的几种网络请求方式详解 .
  6. android 回车键事件
  7. 我所理解的Android模块化(三)——模块可插
  8. Android中的AndroidManifest.xml文件
  9. Android的ADB工具使用
  10. Android 中的 framebuffer