Log in or register to post comments

Use OnTrackingFound/Lost in additional script

April 12, 2013 - 5:50am #1

Hi - I'm getting more and more to grips with Vuforia and C# in general, and liking it! 

 

What I'd like to do, is add a script to an image target prefab (which thus holds the DefaultTrackableEventHandler). 

In this additional script, I'd like to create some actions which need to be triggered when the image target is recognized. 

Currently, I'm duplicating the DefaultTrackableEventHandler, give it its own name (e.g. TargetXTrackableEventHandler), insert my actions in the script and apply it to Target X. 

However, I'd like to have these additional actions in a separate script (so on top of the DefaultHandler), to prevent endless duplicates or breaking it accidentally. 

 

I'm guessing it's something in the area of private vs public methods, so this is probably more of a generic C# question. 

Still, would be great to find out how this would work in this particular situation. 

 

Thanks a lot! 

Jan Paul

 

Use OnTrackingFound/Lost in additional script

April 12, 2013 - 6:24am #2

Hi, you can split your functionalities in multiple scripts, that's not a problem;

for example, suppose you have two scripts called MyTrackableEventHandler and MyAction;

you could attach an instance of each of those scripts to one of your ImageTargets;

then, when a target is detected, you could invoke some functionality in the MyAction script;

here below I put some simple code showcasing that:

 

public class MyTrackableEventHandler : MonoBehaviour, ITrackableEventHandler {

private MyAction myAction = null;
    private TrackableBehaviour mTrackableBehaviour;
    
    void Start() {
        mTrackableBehaviour = GetComponent<TrackableBehaviour>();
        if (mTrackableBehaviour)
        {
            mTrackableBehaviour.RegisterTrackableEventHandler(this);
        }

        myAction = GetComponent<MyAction>();
    }

    // Update is called once per frame
    void Update () {
    }

    public void OnTrackableStateChanged(
        TrackableBehaviour.Status previousStatus,
        TrackableBehaviour.Status newStatus) {

        myAction.DoSomething();
    }
}

 

And this is the MyAction script code:
public class MyAction : MonoBehaviour {

  // Use this for initialization
  void Start () {

  }

  // Update is called once per frame
  void Update () {

  }

  public void DoSomething() {
  // TODO do something
    Debug.Log ("DoSomething() is executed");
  }
}

 

 

 

Log in or register to post comments