Задать вопрос
@antonghdi3

Почему 1 событие проигрывания звука перестаю срабатывать если перезайти на сцену?

я кнопкам повесил 2 события, 1 событие - функция проигрывания звука, 2 - другое

и это на 1 сцене, потом я на 2 перешел и снова на 1 и у мя звук перестал проигрываться при клике, но 2 функция которая другое делает нормально работала, что не так?

6849205a1431d525503307.png

using System;
using UnityEngine;
using UnityEngine.SceneManagement;

public class AudioManager : MonoBehaviour
{
  public static AudioManager Instance;

  public Sound[] sfxSounds, musicSounds;
  public AudioSource sfxSource, musicSource;

  private void Awake()
  {
    if (Instance == null)
    {
      Instance = this;
      DontDestroyOnLoad(gameObject);
      SceneManager.sceneLoaded += OnSceneLoaded;
    }
    else
    {
      Destroy(gameObject);
      return;
    }

    if (!PlayerPrefs.HasKey(PlayerPrefsKeys.MusicVolume))
      PlayerPrefs.SetFloat(PlayerPrefsKeys.MusicVolume, 0.28f);
    if (!PlayerPrefs.HasKey(PlayerPrefsKeys.SfxVolume))
      PlayerPrefs.SetFloat(PlayerPrefsKeys.SfxVolume, 1f);

    musicSource.volume = PlayerPrefs.GetFloat(PlayerPrefsKeys.MusicVolume);
    sfxSource.volume = PlayerPrefs.GetFloat(PlayerPrefsKeys.SfxVolume);
  }

  private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
  {
    if (scene.name == "Menu")
    {
      PlayMusic("MenuMusic");
    }
    else if (scene.name == "PvpArena")
    {
      PlayMusic("PvpMusic");
    }
  }

  public void PlayMusic(string name)
  {
    Sound s = Array.Find(musicSounds, x => x.name == name);

    if (s == null)
    {
      Debug.Log("Sound not found");
    }
    else
    {
      musicSource.clip = s.clip;
      musicSource.Play();
    }
  }

  public void PlaySfx(string name)
  {
    Sound s = Array.Find(sfxSounds, x => x.name == name);

    if (s == null)
    {
      Debug.Log("Sound not found");
    }
    else
    {
      sfxSource.PlayOneShot(s.clip);
    }
  }
}
  • Вопрос задан
  • 41 просмотр
Подписаться 1 Простой 2 комментария
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы