Vuforia is one of the widely used augmented reality SDK for mobile devices that enables the creation of augmented reality applications. This blog post is all about the best way to write your custom logic on object recognitions that is scalable and maintainable.
The DefaultTrackableEventHandler is the script the deals with all kind of event registrations for your target like OnTrackingFound (what to do on successful tracking), OnTrackingLost (what to do when tracking is lost). There is a good chance that devs will inject code into DefaultTrackableEventHandler to intercept and write their own custom logic.
Assume that you want to playback an audio when an object is tracked and stop playback of audio when object lost its tracking. Below is a typical messy code.
Over a period of time, this looks messy and expands to large if-else ladder to accommodate various scenarios.
But, how can you add custom logic that is maintainable and scalable? Here is what you need to do.
- Inherit the DefaultTrackableEventHandler
- Override OnTrackingFound() and OnTrackingLost()
- Remove DefaultTrackableEventHandler component from your Vuforia Target element and add your inherited class.
As sample code snippet is below:
With this, you are creating new classes that just deals with what it is supposed to do.
Hope this helps.