Skip to content

Instantly share code, notes, and snippets.

@oismaelash
Created November 10, 2017 00:35
Show Gist options
  • Save oismaelash/13e89db79567dcfc284b267c3d91e444 to your computer and use it in GitHub Desktop.
Save oismaelash/13e89db79567dcfc284b267c3d91e444 to your computer and use it in GitHub Desktop.
using Vuforia;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ImageTargetPlayAudio : MonoBehaviour, ITrackableEventHandler
{
public TrackableBehaviour mTrackableBehaviour;
void Start()
{
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
GetComponent<AudioSource>().enable = false;
if (mTrackableBehaviour)
{
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
}
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus)
{
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
{
// Play audio when target is found
GetComponent<AudioSource>().enable = true;
GetComponent<AudioSource>().Play();
}
else
{
// Stop audio when target is lost
GetComponent<AudioSource>().Stop();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment