How to use one button to play different audio for different Targets?
Hi I have a problem. Here is description. I use Vuforia library for Unity.
I am going to recognize few imageTargets and I would like to play sound with onClick(button). This button is on canvas and it is always on top during the app lifetime. So You can see it always. Behind this button You can see camera view and recognize markers.
F.e I have two markers: Dog and Cow. Dog has assigned audio - bark. Cow has assigned audio - muu.
When I recognize Cow -> click the button and it should give me muu sound, but when I recognize Dog, the same button, when clicked should give me bark sound. Here is a problem. I cannot resolve it. I think I should write a script for this button to play sound onClick for appropriate marker, but I do not know how to tell button that now I can see Cow (play muu onClick) and another time I can see Dog (play bark onClick). To sum up - How to give button info which audiofile should it use.
I made a script which plays sound when image is recognized, but I would like to do it with button.
Here: GetComponent<AudioSource>().Play();
It plays audio which is assigned to marker, but... It starts when camera recognize it (and without button). I would like to allow play proper sounds for Cow and Dog using only one, the same button. Below is full script:
using UnityEngine;
using Vuforia;
public class TargetDogPlayAudio : MonoBehaviour,
ITrackableEventHandler
{
public AudioClip DogSound;
private TrackableBehaviour mTrackableBehaviour;
void Start()
{
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
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>().Play();
}
else
{
// Stop audio when target is lost
GetComponent<AudioSource>().Stop();
}
}
}
If something is not clear enough - let me know and I will write it better or again.