Vuforia How To Use Android(安卓)Plugins in Unity Apps
16lz
2021-12-04
How To Use Android Plugins in Unity Apps
This article explains how to do two things:
- Take custom Android plugins that are written in Java and integrate them into a Vuforia-based Unity project
- Invoke methods defined in the Java plugin from a Unity C# script.
Integrate plugins into a Vuforia-based Unity project
- Create a Unity project. For ease of reference we refer to it as QcarWithPlugin.
- Import the Image Targets sample package for Unity
(Menu>Assets >Importcustompackage) - 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.
- 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.
- 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
- Open theMyPluginActivity.javafile in Eclipse, and make the following code changes:
- Make MyPluginActivity extendQCARPlayerActivityinstead of Activity. Press Ctrl-Shift-O to adjust the imports automatically.
- From theonCreate()method, remove the linesetContentView(…).
- Add a custom public method calledshowMessage()to the MyPluginActivity class. Fill the method with some code to display a Toast message with text.
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
- 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. - Open the/binfolder and copy the JAR library from that folder to theAssets/Plugins/Android/folder of your Unity project.
- Open and edit theAndroidManifest.xmlfile located in the sameAssets/Plugins/Android/ directory.
- Replace the name of the main activity with your fully qualified Activity name, for example, com.my.org.MyPluginActivity.
- Create a C# script, call itMyPluginCaller,for example, and attach it to each of your Image Targets in the scene (Chips, Stones, Tarmac, etc.).
- 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,最新的方法点击下面:
原文地址点击这里
更多相关文章
- 浅谈Java中Collections.sort对List排序的两种方法
- Python list sort方法的具体使用
- python list.sort()根据多个关键字排序的方法实现
- android之网络请求框架OKhttp、原生http请求
- android加载.swf flash文件
- android仿苹果Iphone桌面源码
- android抓log方法
- android 登陆右上角 的关闭按钮的使用方法
- android 全屏 去掉标题栏