• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

  • 共151篇

    Unity - 游戏引擎

关闭

返回栏目

关闭

返回Unity - 游戏引擎栏目

111 - Manager - AudioManager.cs - 经理_音乐

作者:

贺及楼

成为作者

更新日期:2023-09-17 10:59:05

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class AudioManager : BaseManager {
  5. public AudioManager(GameFacade facade) : base(facade) { }
  6. private const string Sound_Prefix = "Sounds/";
  7. public const string Sound_Alert = "Alert";
  8. public const string Sound_ArrowShoot = "ArrowShoot";
  9. public const string Sound_Bg_Fast = "Bg(fast)";
  10. public const string Sound_Bg_Moderate = "Bg(moderate)";
  11. public const string Sound_ButtonClick = "ButtonClick";
  12. public const string Sound_Miss = "Miss";
  13. public const string Sound_ShootPerson = "ShootPerson";
  14. public const string Sound_Timer = "Timer";
  15. private AudioSource bgAudioSource;
  16. private AudioSource normalAudioSource;
  17. public override void OnInit()
  18. {
  19. GameObject audioSourceGO = new GameObject("AudioSource(GameObject)");
  20. bgAudioSource = audioSourceGO.AddComponent<AudioSource>();
  21. normalAudioSource = audioSourceGO.AddComponent<AudioSource>();
  22. PlaySound(bgAudioSource, LoadSound(Sound_Bg_Moderate),0.5f, true);
  23. }
  24. public void PlayBgSound(string soundName)
  25. {
  26. PlaySound(bgAudioSource, LoadSound(soundName), 0.5f, true);
  27. }
  28. public void PlayNormalSound(string soundName)
  29. {
  30. PlaySound(normalAudioSource, LoadSound(soundName), 1f);
  31. }
  32. private void PlaySound( AudioSource audioSource,AudioClip clip,float volume, bool loop=false)
  33. {
  34. audioSource.clip = clip;
  35. audioSource.volume = volume;
  36. audioSource.loop = loop;
  37. audioSource.Play();
  38. }
  39. private AudioClip LoadSound(string soundsName)
  40. {
  41. return Resources.Load<AudioClip>(Sound_Prefix + soundsName);
  42. }
  43. }