SoundManager.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SoftKitty
  5. {
  6. /// <summary>
  7. /// This module plays a sound from the Resources folder. Place your sound clips in "Resources/Sounds/"
  8. /// </summary>
  9. public class SoundManager : MonoBehaviour
  10. {
  11. #region Variables
  12. private static SoundManager instance;
  13. private static Dictionary<string, AudioClip> SoundDic = new Dictionary<string, AudioClip>();
  14. public AudioSource SoundPlayer;
  15. #endregion
  16. #region Internal Methods
  17. private void Awake()
  18. {
  19. instance = this;
  20. SoundDic.Clear();
  21. }
  22. private static void CreateInstance()
  23. {
  24. GameObject newObj = Instantiate(Resources.Load<GameObject>("SoftKittyShared/SoundManager"));
  25. newObj.transform.position = Vector3.zero;
  26. newObj.transform.localScale = Vector3.one;
  27. instance = newObj.GetComponent<SoundManager>();
  28. }
  29. public void PlaySound3D(string _soundName, Vector3 _position, float _volumn)
  30. {
  31. try
  32. {
  33. AudioSource.PlayClipAtPoint(GetClip(_soundName), _position, _volumn);
  34. }
  35. catch
  36. {
  37. }
  38. }
  39. public void PlaySound2D(string _soundName, float _volumn)
  40. {
  41. try
  42. {
  43. SoundPlayer.PlayOneShot(GetClip(_soundName), _volumn);
  44. }
  45. catch
  46. {
  47. }
  48. }
  49. public AudioClip GetClip(string _soundName)
  50. {
  51. if (SoundDic.ContainsKey(_soundName))
  52. {
  53. return SoundDic[_soundName];
  54. }
  55. else
  56. {
  57. try
  58. {
  59. AudioClip _clip = Resources.Load<AudioClip>("Sounds/" + _soundName);
  60. if (_clip != null)
  61. {
  62. SoundDic.Add(_soundName, _clip);
  63. return _clip;
  64. }
  65. }
  66. catch
  67. {
  68. return null;
  69. }
  70. }
  71. return null;
  72. }
  73. #endregion
  74. /// <summary>
  75. /// Plays a 2D sound with the provided audio clip name
  76. /// </summary>
  77. /// <param name="_soundName"></param>
  78. /// <param name="_volumn"></param>
  79. public static void Play2D(string _soundName, float _volumn=1F)
  80. {
  81. if (instance == null)
  82. {
  83. CreateInstance();
  84. }
  85. instance.PlaySound2D(_soundName, _volumn);
  86. }
  87. /// <summary>
  88. /// Plays a 3D sound at the specified position using the provided audio clip name.
  89. /// </summary>
  90. /// <param name="_soundName"></param>
  91. /// <param name="_position"></param>
  92. /// <param name="_volumn"></param>
  93. public static void Play3D(string _soundName, Vector3 _position, float _volumn = 1F)
  94. {
  95. if (instance == null)
  96. {
  97. CreateInstance();
  98. }
  99. instance.PlaySound3D(_soundName, _position, _volumn);
  100. }
  101. /// <summary>
  102. /// In order to save performance, after playing an AudioClip,
  103. /// the script will keep it in memory because most likely it going to be played again later.
  104. /// Calling this will clear all those data to release memory.
  105. /// </summary>
  106. public static void ClearSoundCache()
  107. {
  108. SoundDic.Clear();
  109. }
  110. }
  111. }