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

Gravity Jack In the Press

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 ARTICLES

  • The Future of Fashionable AR Glasses Starts with The Apple Vision Pro

    Apple Vision Pro: A Milestone in Fashion-Forward AR Wearables

    Discover the Apple Vision Pro – a game-changer propelling AR glasses towards sleeker horizons with style and innovation as core attributes. In our latest blog, we unravel how Apple’s foray into chic, wearable tech is setting unprecedented standards.

    • Delicate yet Durable Design: Apple’s lightweight frames mingle with minimalist aesthetics, aligning with modern style sensibilities.
    • Potent Technology Constrained: Smaller, more efficient batteries coupled with mighty computing power ensure seamless AR integration in a compact form.

    As industry frontrunners like Google and Meta race to rival the high bar set by Apple, Gravity Jack stands prepared to craft AR solutions that resonate with elegance and efficacy.

    Immerse yourself in our exploration of how the Apple Vision Pro is not just redefining the wearable tech landscape – it’s actively close-knitting technology with personal flair. Read on for insights into how your business’s future can align with this stylish and smart AR evolution.

    Learn more about integrating AR into your strategy with Gravity Jack.

  • The Revolutionary Impact of Virtual Reality in Tourism

    Step Beyond Boundaries: VR’s New Travel Dimension


    Embark on an exploration of how Virtual Reality (VR) is innovatively charting the course of the tourism industry. The VR landscape is bustling, poised to reach a staggering $62.1 billion market value by 2027, with travel experiences contributing a substantial slice to this tech pie. Pioneers like Google Earth VR, Ascape VR, and The Wild Within offer sweeping vistas and interactive treks, inviting users to tour globally from comfort zones.

    • Revolutionizing Exploration: VR transcends geographic limits, enabling virtual visits to the world’s marvels.
    • Tourism Titans: Leaders in tech exemplify VR’s vast potential to reshape how we uncover global wonders.
    • Gravity Jack: As adept navigators in VR and AR realms, we craft immersive voyages that enchant and inform.

    We stand at the cusp of a grand revolution, where virtual journeys ignite the spirit of travel. For an in-depth survey of VR’s trailblazing trajectory, dive into our full discourse on tech-fueled tourism!

  • Crafting Personalized Retail Expereinces With Augmented Reality

    Augmented Reality: Revolutionizing Retail Customization

    In the bustling realm of retail, Augmented Reality (AR) is ushering in an era where shopping becomes a personalized showcase. Imagine trying on clothes without stepping into a dressing room, or placing furniture in your living room with just a tap on your screen. With an anticipated market growth to USD 795.3 billion by 2025, it’s clear that consumers crave these cutting-edge experiences.

    • AR empowers consumers to customize products in real-time.
    • Big names like IKEA, Sephora, and Nike are pioneering this space.
    • Insightful data gleaned from AR interactions are redefining marketing strategies.

    At Gravity Jack, we’re not just observers but active innovators in AR development. We provide immersive solutions that resonate with consumers, paving the way for businesses to weave their narratives into the customer’s world. Dive into our blog to see how the tactile future of digital shopping is already at our fingertips.