AudioExample.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace QFramework.Example
  7. {
  8. public class AudioExample : MonoBehaviour
  9. {
  10. private void Awake()
  11. {
  12. var btnPlayHome = transform.Find("BtnPlayHome").GetComponent<Button>();
  13. var btnPlayGame = transform.Find("BtnPlayGame").GetComponent<Button>();
  14. var btnPlaySound = transform.Find("BtnPlaySoundClick").GetComponent<Button>();
  15. var btnPlayVoiceA = transform.Find("BtnPlayVoice").GetComponent<Button>();
  16. var btnSoundOn = transform.Find("BtnSoundOn").GetComponent<Button>();
  17. var btnSoundOff = transform.Find("BtnSoundOff").GetComponent<Button>();
  18. var btnMusicOn = transform.Find("BtnMusicOn").GetComponent<Button>();
  19. var btnMusicOff = transform.Find("BtnMusicOff").GetComponent<Button>();
  20. var btnVoiceOn = transform.Find("BtnVoiceOn").GetComponent<Button>();
  21. var btnVoiceOff = transform.Find("BtnVoiceOff").GetComponent<Button>();
  22. var musicVolumeSlider = transform.Find("MusicVolume").GetComponent<Slider>();
  23. var voiceVolumeSlider = transform.Find("VoiceVolume").GetComponent<Slider>();
  24. var soundVolumeSlider = transform.Find("SoundVolume").GetComponent<Slider>();
  25. bool supportResKit = true;
  26. if (supportResKit)
  27. {
  28. // AudioKit.Config.AudioLoaderPool = new ResKitAudioLoaderPool();
  29. btnPlayHome.onClick.AddListener(() => { AudioKit.PlayMusic("resources://home_bg"); });
  30. btnPlayGame.onClick.AddListener(() => { AudioKit.PlayMusic("resources://game_bg"); });
  31. btnPlaySound.onClick.AddListener(() => { AudioKit.PlaySound("resources://game_bg"); });
  32. btnPlayVoiceA.onClick.AddListener(() => { AudioKit.PlayVoice("resources://game_bg"); });
  33. }
  34. else
  35. {
  36. AudioKit.Config.AudioLoaderPool = new DefaultAudioLoaderPool();
  37. btnPlayHome.onClick.AddListener(() => { AudioKit.PlayMusic("home_bg"); });
  38. btnPlayGame.onClick.AddListener(() => { AudioKit.PlayMusic("game_bg"); });
  39. btnPlaySound.onClick.AddListener(() => { AudioKit.PlaySound("game_bg"); });
  40. btnPlayVoiceA.onClick.AddListener(() => { AudioKit.PlayVoice("game_bg"); });
  41. }
  42. btnSoundOn.onClick.AddListener(() => { AudioKit.Settings.IsSoundOn.Value = true; });
  43. btnSoundOff.onClick.AddListener(() => { AudioKit.Settings.IsSoundOn.Value = false; });
  44. btnMusicOn.onClick.AddListener(() => { AudioKit.Settings.IsMusicOn.Value = true; });
  45. btnMusicOff.onClick.AddListener(() => { AudioKit.Settings.IsMusicOn.Value = false; });
  46. btnVoiceOn.onClick.AddListener(() => { AudioKit.Settings.IsVoiceOn.Value = true; });
  47. btnVoiceOff.onClick.AddListener(() => { AudioKit.Settings.IsVoiceOn.Value = false; });
  48. AudioKit.Settings.MusicVolume.BindWithInitialValue(v => musicVolumeSlider.value = v);
  49. AudioKit.Settings.VoiceVolume.BindWithInitialValue(v => voiceVolumeSlider.value = v);
  50. AudioKit.Settings.SoundVolume.BindWithInitialValue(v => soundVolumeSlider.value = v);
  51. musicVolumeSlider.onValueChanged.AddListener(v => { AudioKit.Settings.MusicVolume.Value = v; });
  52. voiceVolumeSlider.onValueChanged.AddListener(v => { AudioKit.Settings.VoiceVolume.Value = v; });
  53. soundVolumeSlider.onValueChanged.AddListener(v => { AudioKit.Settings.SoundVolume.Value = v; });
  54. }
  55. }
  56. }