у объекта дополненной реальности есть звук, когда цель найдена, что мне нужно, это как повторить этот звук я - PullRequest
0 голосов
/ 02 декабря 2018

У меня есть мобильное приложение с дополненной реальностью, в котором звук автоматически воспроизводится для определенной цели изображения и останавливается, когда цель теряется.Что мне нужно, так это как мне повторить звук с помощью кнопки, все еще фокусируясь на цели изображения.

Пример:

          Image target - Apple 
          Sound - Letter A sound

         Image target- Banana
         Sound - Letter B sound

Когда я фокусируюсь на цели изображения яблока,Звук играет звук.Как мне повторить этот звук с помощью кнопки, чтобы мне больше не приходилось фокусироваться на цели изображения.

1 Ответ

0 голосов
/ 04 декабря 2018

Есть несколько способов сделать это

Обычно у вас есть компонент с открытым методом, таким как

public class SoundController : MonoBehaviour
{
    // here you reference your Scene's AudioSource in the inspector 
    // e.g. via drag and drop
    [SerializeField] private AudioSource _audioSource;

    // Here you reference the according AudioClips
    [SerializeField] private AudioClip _letterASound;
    [SerializeField] private AudioClip _letterBSound;

    // an enum makes it esier to find the correct letter later 
    public enum Letter
    {
        A,
        B
        // etc
    }

    // store the current Sound to be played
    public Letter CurrentLetter;

    private Button _button;

    // a dictionary makes it easy to find the according sound for a specific letter
    private Dictionary<Letter, AudioClip> _sounds;

    private void Awake()
    {
        // initialize the dictionary
        _sounds = new Dictionary<Letter, AudioClip>()
        {
            {A, _lettarASound},
            {B, _letterBSound}
        }

        // get the Button component
        _button = GetComponent<Button>();

        // add an onclick callback for the button
        // (it is save to allways first remove the callback to avoid double calls)
        _button.onClick.RemoveListener(PlayCurrentSound);
        _button.onClick.AddListener(PlayCurrentSound);
    }

    // make it public so you could even call it from somewhere else if needed
    public void PlayCurrentSound()
    {
        AudioSource.PlayOneShot(_sounds[CurrentLetter]);
    }
}

, который помещается на кнопку рядом с компонентом Button.и ссылаться на AudioSource и AudioClip s (конечно, вы можете добавить больше, если это необходимо).

Теперь все, что осталось, это менять CurrentLetter каждый раз, когда вы наводите курсор мыши на новый элемент письма.Я не знаю, как ваш код выглядит для этого, но вы бы добавили что-то вроде

// here you will have to reference again in the inspector the
// SoundController component from above
[SerializeField] private SoundController _soundController;

// ....

// than whereever you have your code for
// "... plays for a specific image target and it stops when the target is lost ..."
// you add the line like e.g. for letter A
SoundController.CurrentLetter = SoundController.Letter.A;

, поэтому при следующем нажатии кнопки будет воспроизводиться звук для A.

...