Associating markers with planets

To associate markers with planet views, we need to write our own marker handler script. We will build this script incrementally, so it's easy to see what is going on.

First, we'll write in the code to register our callback function to VuMark Manager. This is a standard pattern in Unity C# for handling events:

  1. In Hierarchy, select GameController and go to Add Component | New Script; name it PlanetMarkerHandler.

Open the script in your editor and write it as follows:

File: PlanetMarkerHandler.cs 
using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using Vuforia; 
 
public class PlanetMarkerHandler : MonoBehaviour { 
    private VuMarkManager mVuMarkManager; 
 
    void Start () { 
        // register callbacks to VuMark Manager 
        mVuMarkManager = TrackerManager.Instance.GetStateManager().GetVuMarkManager(); 
        mVuMarkManager.RegisterVuMarkDetectedCallback(OnVuMarkDetected); 
    } 
 
    public void OnVuMarkDetected(VuMarkTarget target) { 
        Debug.Log("New VuMark: " + target.InstanceId.StringValue); 
    } 
} 

We included the line using Vuforia to get access to their SDK, including VuMarkerManager. We then registered our callback function, OnVuMarkDetected, with TrackerManager. The callback, for now, just prints to the debug console the name (StringValue) of the marker it finds when one is detected.

  1. Save the script and press Play in Unity. Show the camera to different marks, and the console will print the mark ID it finds.
To open the console, go to Window | Console and dock it in the Unity editor.

The target ID is a string (text) that we obtained using target.InstanceId.StringValue in the VuMark00 format. We need it as an integer. So we will now write a little helper function, markIdToInt, to convert it from a string to an integer (it finds the substring starting at position 6 and takes the next two characters, for example, 00) and print that to the console instead. Change the script as follows:

    public void OnVuMarkDetected(VuMarkTarget target) { 
        int id = markIdToInt(target.InstanceId.StringValue); 
        Debug.Log("New VuMark: " + id); 
    } 
 
    private int markIdToInt(string str) { 
        return int.Parse(str.Substring(6, 2)); 
    } 

Now we will give the handler a list of planetary bodies. The list is indexed from 0 through 9.

Add this line to the top of the PlanetMarkerHandler class (inside curly braces):

public class PlanetMarkerHandler : MonoBehaviour { 
    public List<Transform> bodies = new List<Transform>(); 
  1. Go to Unity and do the following:
    1. You will see a list of planetary bodies is now available in the Planet Marker Handler component. Give it size 10.
    2. Then, drag each of the objects from Hierarchy to the corresponding slots, starting with Sun for 0, Mercury for 1, and so on through Pluto. As before, be sure to use Earth-Moon for the earth system.

Your Planet Market Handler component should now look like this:

Back in the code, all we need to do is set the Planet value of the Planet View component to the one identified by the VuMark marker. The completed script looks like this:

 
File: PlanetMarkerHandler.cs 
public class PlanetMarkerHandler : MonoBehaviour { 
    public List<Transform> bodies = new List<Transform>(); 
    private VuMarkManager mVuMarkManager; 
    private PlanetView planetView; 
 
    void Start () { 
        // get the Planet View component 
        planetView = GetComponent<PlanetView>(); 
 
        // register callbacks to VuMark Manager 
        mVuMarkManager = TrackerManager.Instance.GetStateManager().GetVuMarkManager(); 
        mVuMarkManager.RegisterVuMarkDetectedCallback(OnVuMarkDetected); 
    } 
 
    public void OnVuMarkDetected(VuMarkTarget target) { 
        int id = markIdToInt(target.InstanceId.StringValue); 
        Debug.Log("Changing view: " + bodies[id].name); 
        planetView.planet = bodies[id]; 
    } 
 
    private int markIdToInt(string str) { 
        return int.Parse(str.Substring(6, 2)); 
    } 
} 

The new code gets the integer ID by calling markIdToInt on the instance ID. It then looks up the corresponding object in our list and assigns it to planetView.planet, thereby switching which planet is now in the center of our view in the app.

When you show your camera to different markers, you will see different planets.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset