不同的声音,不同的imageTargets和一个按钮。 Vuforia(Unity3d)

时间:2019-04-01 08:20:04

标签: c# unity3d vuforia

我将识别很少的imageTargets,并且我想使用onClick(button)播放声音。此按钮位于画布上,在应用程序生命周期中始终位于顶部。因此您可以随时看到它。在此按钮后面,您可以查看相机视图并识别标记。

F.e我有两个标记:“狗”和“牛”。 狗已分配音频-吠叫。 Cow已经分配了音频-muu。

当我识别出母牛->单击该按钮时,它应该给我muu声音,但是当我识别出Dog时,单击该按钮时应该给我吠声。 这是个问题。我无法解决。我认为我应该为此按钮编写一个脚本,以播放适当的标记声onClick,但我不知道如何告诉按钮现在我可以看到Cow,而又一次可以看到Dog。

我制作了一个脚本,可以在识别图像时播放声音,但是我想用按钮来完成。

如果有一些不清楚的地方,请告诉我,我会写得更好或更。

到目前为止,在我的“默认可跟踪事件处理程序”中是值得的:

using UnityEngine;
using Vuforia;

public class DefaultTrackableEventHandlerEng : MonoBehaviour, ITrackableEventHandler
{
    //------------Begin Sound----------
    public AudioSource soundTarget;
    public AudioClip clipTarget;
    private AudioSource[] allAudioSources;

    //function to stop all sounds
    void StopAllAudio()
    {
        allAudioSources = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
        foreach (AudioSource audioS in allAudioSources)
        {
            audioS.Stop();
        }
    }

    //function to play sound
    void playSound(string ss)
    {
        clipTarget = (AudioClip)Resources.Load(ss);
        soundTarget.clip = clipTarget;
        soundTarget.loop = false;
        soundTarget.playOnAwake = false;
        soundTarget.Play();
    }

    //-----------End Sound------------


    #region PROTECTED_MEMBER_VARIABLES

    protected TrackableBehaviour mTrackableBehaviour;
    protected TrackableBehaviour.Status m_PreviousStatus;
    protected TrackableBehaviour.Status m_NewStatus;

    #endregion // PROTECTED_MEMBER_VARIABLES

    #region UNITY_MONOBEHAVIOUR_METHODS

    protected virtual void Start()
    {
        mTrackableBehaviour = GetComponent<TrackableBehaviour>();
        if (mTrackableBehaviour)
            mTrackableBehaviour.RegisterTrackableEventHandler(this);
        //Register / add the AudioSource as object
        soundTarget = (AudioSource)gameObject.AddComponent<AudioSource>();

    }

    protected virtual void OnDestroy()
    {
        if (mTrackableBehaviour)
            mTrackableBehaviour.UnregisterTrackableEventHandler(this);
    }

    #endregion // UNITY_MONOBEHAVIOUR_METHODS

    #region PUBLIC_METHODS

    /// <summary>
    ///     Implementation of the ITrackableEventHandler function called when the
    ///     tracking state changes.
    /// </summary>
    /// 
    public void OnTrackableStateChanged(
        TrackableBehaviour.Status previousStatus,
        TrackableBehaviour.Status newStatus)
    {
        m_PreviousStatus = previousStatus;
        m_NewStatus = newStatus;


        if (newStatus == TrackableBehaviour.Status.DETECTED ||
            newStatus == TrackableBehaviour.Status.TRACKED ||
            newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
        {
            Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");

            if (mTrackableBehaviour.TrackableName == "1")
            {
                playSound("audio/1_eng");
            }

            if (mTrackableBehaviour.TrackableName == "2")
            {
                playSound("audio/2_eng");
            }

            if (mTrackableBehaviour.TrackableName == "3")
            {
                playSound("audio/3_eng");
            }

            OnTrackingFound();
        }
        else if (previousStatus == TrackableBehaviour.Status.TRACKED &&
                 newStatus == TrackableBehaviour.Status.NO_POSE)
        {
            Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");

            StopAllAudio();

            OnTrackingLost();
        }
        else
        {
            // For combo of previousStatus=UNKNOWN + newStatus=UNKNOWN|NOT_FOUND
            // Vuforia is starting, but tracking has not been lost or found yet
            // Call OnTrackingLost() to hide the augmentations
            OnTrackingLost();
        }
    }

    #endregion // PUBLIC_METHODS

    #region PROTECTED_METHODS

    protected virtual void OnTrackingFound()
    {
        var rendererComponents = GetComponentsInChildren<Renderer>(true);
        var colliderComponents = GetComponentsInChildren<Collider>(true);
        var canvasComponents = GetComponentsInChildren<Canvas>(true);

        // Enable rendering:
        foreach (var component in rendererComponents)
            component.enabled = true;

        // Enable colliders:
        foreach (var component in colliderComponents)
            component.enabled = true;

        // Enable canvas':
        foreach (var component in canvasComponents)
            component.enabled = true;
    }


    protected virtual void OnTrackingLost()
    {
        var rendererComponents = GetComponentsInChildren<Renderer>(true);
        var colliderComponents = GetComponentsInChildren<Collider>(true);
        var canvasComponents = GetComponentsInChildren<Canvas>(true);

        // Disable rendering:
        foreach (var component in rendererComponents)
            component.enabled = false;

        // Disable colliders:
        foreach (var component in colliderComponents)
            component.enabled = false;

        // Disable canvas':
        foreach (var component in canvasComponents)
            component.enabled = false;
    }

    #endregion // PROTECTED_METHODS
}

1 个答案:

答案 0 :(得分:1)

因此,如果我理解正确,您的代码基本上可以正常工作,但是与其直接播放已识别目标的相应声音,不如只单击按钮就播放它,对吧?

您只需添加方法PlayCurrentSound并在onClick中引用它即可:

// SET THIS NAME INSTEAD OF DIRECTLY PLAYING IT
private string currentSoundName;

// THIS IS THE METHOD CALLED BY THE BUTTON
public void PlayCurrentSound()
{
    if(!string.IsNullOrWhiteSpace(currentSoundName)) playSound(currentSoundName);
}

,而在OnTrackableStateChanged中仅更改currentSoundName的值,而不是直接重播

public void OnTrackableStateChanged(TrackableBehaviour.Status previousStatus, TrackableBehaviour.Status newStatus)
{
    m_PreviousStatus = previousStatus;
    m_NewStatus = newStatus;

    if (newStatus == TrackableBehaviour.Status.DETECTED ||
        newStatus == TrackableBehaviour.Status.TRACKED ||
        newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
    {
        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");

        // HERE BETTER USE A SWITCH INSTEAD
        switch(mTrackableBehaviour.TrackableName)
        {
            case "1":
                currentSoundName = "audio/1_eng";
                break;

            case "2":
                currentSoundName = "audio/2_eng";
                break;

            case "3":
                currentSoundName = "audio/3_eng";
                break;

            default:
                currentSoundName = "";
                break;
        }
        // OR ALTERNATIVELY IF YOU ANYWAY WANT TO
        // SET THE NAME FOR ALL POSSIBLE NAMES YOU COULD EVEN GO
        currentSoundName = string.Format("audio/{0}_eng", mTrackableBehaviour.TrackableName);

        OnTrackingFound();
    }
    else if (previousStatus == TrackableBehaviour.Status.TRACKED &&
             newStatus == TrackableBehaviour.Status.NO_POSE)
    {
        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");

        StopAllAudio();

        // RESET currentSoundName 
        currentSoundName = "";

        OnTrackingLost();
    }
    else
    {
        // For combo of previousStatus=UNKNOWN + newStatus=UNKNOWN|NOT_FOUND
        // Vuforia is starting, but tracking has not been lost or found yet
        // Call OnTrackingLost() to hide the augmentations
        OnTrackingLost();

        // RESET currentSoundName 
        currentSoundName = "";
    }
}

不过,我还有一些其他细微的变化:

soundTarget.loop = false;
soundTarget.playOnAwake = false;

这可以在游戏开始时完成,只有一次,并且不应每次都重复。这样做:

private void Awake()
{
    soundTarget.loop = false;
    soundTarget.playOnAwake = false;
}

clipTarget = (AudioClip)Resources.Load(ss);

从资源中反复加载(也许)相同的声音...效率不是很高。您宁愿要在引用加载后保留引用,例如

private Dictionary<string, AudioClip> clips = new Dictionary<string, AudioClip>();

void playSound(string ss)
{
    if(clips.ContainsKey(ss) && clip[ss] != null)
    {
        clipTarget = clips[ss];
    else
    {
        clip = (AudioClip)Resources.Load(ss);
        if(clipTarget == null) 
        {
            Debug.LogError("Couldn't get clip for " + ss, this);
            return;
        }

        clips.Add(ss, clipTarget);
    }

    soundTarget.clip = clipTarget;
    soundTarget.Play();
}

您可能还想使用soundTarget.PlayOneShot(clipTarget)。区别在于PlayOneShot播放整个声音并允许并发声音,而Play中断当前声音并开始新声音(取决于您的需要)。