Created
December 4, 2018 23:03
-
-
Save Lachee/57b61f9502eb42156f8de02869b49a57 to your computer and use it in GitHub Desktop.
Vuforia Audio Source
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using Vuforia; | |
[RequireComponent(typeof(AudioSource))] | |
public class ImageTargetAudioSource : MonoBehaviour, ITrackableEventHandler | |
{ | |
[Tooltip("The AudioSource to be activated when the object becomes tracked.")] | |
public AudioSource audioSource; | |
private TrackableBehaviour mTrackableBehaviour; | |
void Reset() | |
{ | |
//On Reset of the component (or when its first added), get the audioSource and prepare it. | |
audioSource = GetComponent<AudioSource>(); | |
if (audioSource) audioSource.playOnAwake = false; | |
} | |
void Start() | |
{ | |
//Get the audio source and the trackable behaviour. If we manage to get a tracking behaviour, subscribe to its event handler. | |
if (audioSource == null) audioSource = GetComponent<AudioSource>(); | |
mTrackableBehaviour = GetComponent<TrackableBehaviour>(); | |
if (mTrackableBehaviour) | |
{ | |
mTrackableBehaviour.RegisterTrackableEventHandler(this); | |
} | |
} | |
public void OnTrackableStateChanged(TrackableBehaviour.Status previousStatus, TrackableBehaviour.Status newStatus) | |
{ | |
//This is called every time the tracking state changes. If we find the audio source and the track, we should play. | |
if (audioSource == null) return; | |
if (newStatus == TrackableBehaviour.Status.DETECTED || | |
newStatus == TrackableBehaviour.Status.TRACKED || | |
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED) | |
{ | |
// Play audio when target is found | |
audioSource.Play(); | |
} | |
else | |
{ | |
// Stop audio when target is lost | |
audioSource.Stop(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment