Vuforia & Unity for Android Mobile Devices – What Developers Need To Know!

So you want to integrate custom Android plugins written in Java, into a Vuforia-based Unity project…

In this post, we’ll show you how, and how to 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.

Steps to follow

  • Create a Unity project; for ease of reference we refer to it as “QcarWithPlugin.”
  • Import the Image Targets sample package for Unity (Menu->Assets->Import custom package…)
  • Double-click the Image-Targets scene in the project view, so that the three Image Targets and their associated teapot models appear in the scene view.
  • Create a new Android project in Eclipse; for ease of reference we call it MyPlugin; we assume the main activity will then be called MyPluginActivity.
  • Right-click on the project in Eclipse, go to Properties > Java Build Path > Libraries, and add the following two libraries as “external jars” (both located under the Assets/Plugins/Android/ folder of your Unity project):
    • QCAR.jar
    • QCARUnityPlayer.jar
  • Open the MyPluginActivity.java file in Eclipse, and make the following code changes:
    • Make MyPluginActivity extend QCARPlayerActivity instead of Activity (press Ctrl-Shift-O to automatically adjust the imports).
    • Remove the line “setContentView(…)” from the onCreate() method.
    • Add a custom public method called showMessage() to the MyPluginActivity class, and fill it with some code to show a Toast message with text.

The code of your MyPluginActivity should look like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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();
   }
  });
    }
}

  • Right-click on the project, go to Properties -> Android, tick the “IsLibrary” checkbox to turn your Android project into a library; this should make Eclipse generate a .JAR file (e.g., called “myplugin.jar”) and store it in the “/bin” folder of your Eclipse project.
  • Open the “/bin” folder and copy the JAR library from that folder to the “Assets/Plugins/Android/” folder of your Unity project.
  • Open and edit the AndroidManifest.xml file located in the same “Assets/Plugins/Android/” directory.
  • Replace the name of the main activity with your fully qualified Activity name, e.g., “com.my.org.MyPluginActivity.”
  • Create a C# script, call it MyPluginCaller for example, and attach it to each of your Image Targets in the scene (e.g., Chips, Stones, Tarmac).
  • Copy the following code in your script, which will make your script invoke the showMessage() method of your MyPluginActivity class whenever a target is detected:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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 your project should be ready to build and run on your Android device!

Like this post?

RELATED WORK

RELATED ARTICLES