Log in or register to post comments

How to create Frame Marker at runtime on Unity?

February 29, 2012 - 9:26pm #1

Hi.
I found this Feature "Added support for creating Frame Markers at runtime" in unity-release-notes-1-5-9.pdf

I can not found sample code.
How to create Frame Marker at runtime on Unity script?

versions:
Vufoira Unity Extension for iOS 1.5.9
Unity 3.4

thanks.

How to create Frame Marker at runtime on Unity?

June 9, 2015 - 6:09am #6
void initTracker(){
 mbs = new MarkerBehaviour[512];
 tm = TrackerManager.Instance;
 tb = tm.GetTracker<MarkerTracker> (); // This line is what I'm talking about
 if (tb == null) {
  Debug.Log ("misserfolg");
 }
}
 
void createFrameMarker(int markerId){
 MarkerTracker markerTracker = (MarkerTracker)tb;
 
 if (markerId < 0) {
  markerId = 0;
 } else if (markerId > 511) {
  markerId = 511;
 }
  
 mbs[markerId] = (MarkerBehaviour)markerTracker.CreateMarker(markerId, "marker" + markerId, 100); // NullReferenceException here
 if (mbs [markerId] != null) {
  mbs [markerId].gameObject.AddComponent<DefaultTrackableEventHandler> ();
  GameObject cube = GameObject.CreatePrimitive (PrimitiveType.Cube);
  cube.transform.parent = mbs [markerId].transform;
  cube.transform.localScale = new Vector3 (0.5f, 0.5f, 0.5f);
  cube.transform.localPosition = new Vector3 (0.0f, 0.25f, 0.0f);
 }
}

I'm on Unity 5.0.2 and Vuforia 4.2.3 and this didn't work for me. No matter if I use InitTracker or GetTracker, it always returns null.

Also I'm scripting in C# Script.

I call all of this in Start() and changed the order, so QCARBehaviour runs before Default Time and my Script after Default Time. But it's still null. I couldn't find much help, hope somebody has an idea of what's wrong.

(I do realize, that my versions are like 3 years newer, but it's the same problem.)

 

 

Re: How to create Frame Marker at runtime on Unity?

March 4, 2012 - 6:02pm #5

Oh, my method was called from Awake method...
I changed it to call from Start method, upgraded Unity 3.5 and set my script's execution order after QCARBehaviour.

So, It's running!!

Thanks!

Re: How to create Frame Marker at runtime on Unity?

March 2, 2012 - 1:42pm #4

Ah, if you're trying to call this from a Start method you need to make sure the tracker has had a chance to initialize first. That happens in the QCARBehaviour.Start method. You can use the script ordering feature in Unity 3.5 to make sure you script is called after the rest. Or, you can initialize your markers in the Update method (and guard it to make sure it only happens once).

- Kim

Re: How to create Frame Marker at runtime on Unity?

March 2, 2012 - 4:38am #3

Thanks, Kim.

I tried the code.
but "TrackerManager.Instance.GetTracker(Tracker.Type.MARKER_TRACKER);" is always returned null.
Can we use this method on Vuforia 1.5 ?

When I tried "tracker = new MarkerTracker()" On Unity Editor, It looks runnning.
but not running on iOS.

Re: How to create Frame Marker at runtime on Unity?

March 1, 2012 - 3:18pm #2

Sorry, that needs to be added to the documentation. Here's a code snippet:

MarkerTracker markerTracker = (MarkerTracker)TrackerManager.Instance.GetTracker(Tracker.Type.MARKER_TRACKER);

int markerId = 0;  // between 0 and 511

MarkerBehaviour mb = markerTracker.CreateMarker(markerId, "marker" + markerId, 100);
if (mb != null)
{
    mb.gameObject.AddComponent<DefaultTrackableEventHandler>();
    GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    cube.transform.parent = mb.transform;
    cube.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
    cube.transform.localPosition = new Vector3(0.0f, 0.25f, 0.0f);
}

- Kim

Log in or register to post comments