SoundExtension.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 Jiang Yin. All rights reserved.
  4. // Homepage: https://gameframework.cn/
  5. // Feedback: mailto:ellan@gameframework.cn
  6. //------------------------------------------------------------
  7. using GameFramework;
  8. using GameFramework.DataTable;
  9. using GameFramework.Sound;
  10. using UnityGameFramework.Runtime;
  11. namespace MetaClient
  12. {
  13. public static class SoundExtension
  14. {
  15. private const float FadeVolumeDuration = 1f;
  16. private static int? s_MusicSerialId = null;
  17. public static int? PlayMusic(this SoundComponent soundComponent, int musicId, object userData = null)
  18. {
  19. soundComponent.StopMusic();
  20. IDataTable<DRMusic> dtMusic = GameEntry.DataTable.GetDataTable<DRMusic>();
  21. DRMusic drMusic = dtMusic.GetDataRow(musicId);
  22. if (drMusic == null)
  23. {
  24. Log.Warning("Can not load music '{0}' from data table.", musicId.ToString());
  25. return null;
  26. }
  27. PlaySoundParams playSoundParams = PlaySoundParams.Create();
  28. playSoundParams.Priority = 64;
  29. playSoundParams.Loop = true;
  30. playSoundParams.VolumeInSoundGroup = 1f;
  31. playSoundParams.FadeInSeconds = FadeVolumeDuration;
  32. playSoundParams.SpatialBlend = 0f;
  33. s_MusicSerialId = soundComponent.PlaySound(AssetUtility.GetMusicAsset(drMusic.AssetName), "Music", Constant.AssetPriority.MusicAsset, playSoundParams, null, userData);
  34. return s_MusicSerialId;
  35. }
  36. public static void StopMusic(this SoundComponent soundComponent)
  37. {
  38. if (!s_MusicSerialId.HasValue)
  39. {
  40. return;
  41. }
  42. soundComponent.StopSound(s_MusicSerialId.Value, FadeVolumeDuration);
  43. s_MusicSerialId = null;
  44. }
  45. public static int? PlaySound(this SoundComponent soundComponent, int soundId, Entity bindingEntity = null, object userData = null)
  46. {
  47. IDataTable<DRSound> dtSound = GameEntry.DataTable.GetDataTable<DRSound>();
  48. DRSound drSound = dtSound.GetDataRow(soundId);
  49. if (drSound == null)
  50. {
  51. Log.Warning("Can not load sound '{0}' from data table.", soundId.ToString());
  52. return null;
  53. }
  54. PlaySoundParams playSoundParams = PlaySoundParams.Create();
  55. playSoundParams.Priority = drSound.Priority;
  56. playSoundParams.Loop = drSound.Loop;
  57. playSoundParams.VolumeInSoundGroup = drSound.Volume;
  58. playSoundParams.SpatialBlend = drSound.SpatialBlend;
  59. return soundComponent.PlaySound(AssetUtility.GetSoundAsset(drSound.AssetName), "Sound", Constant.AssetPriority.SoundAsset, playSoundParams, bindingEntity != null ? bindingEntity.Entity : null, userData);
  60. }
  61. public static int? PlayUISound(this SoundComponent soundComponent, int uiSoundId, object userData = null)
  62. {
  63. IDataTable<DRUISound> dtUISound = GameEntry.DataTable.GetDataTable<DRUISound>();
  64. DRUISound drUISound = dtUISound.GetDataRow(uiSoundId);
  65. if (drUISound == null)
  66. {
  67. Log.Warning("Can not load UI sound '{0}' from data table.", uiSoundId.ToString());
  68. return null;
  69. }
  70. PlaySoundParams playSoundParams = PlaySoundParams.Create();
  71. playSoundParams.Priority = drUISound.Priority;
  72. playSoundParams.Loop = false;
  73. playSoundParams.VolumeInSoundGroup = drUISound.Volume;
  74. playSoundParams.SpatialBlend = 0f;
  75. return soundComponent.PlaySound(AssetUtility.GetUISoundAsset(drUISound.AssetName), "UISound", Constant.AssetPriority.UISoundAsset, playSoundParams, userData);
  76. }
  77. public static bool IsMuted(this SoundComponent soundComponent, string soundGroupName)
  78. {
  79. if (string.IsNullOrEmpty(soundGroupName))
  80. {
  81. Log.Warning("Sound group is invalid.");
  82. return true;
  83. }
  84. ISoundGroup soundGroup = soundComponent.GetSoundGroup(soundGroupName);
  85. if (soundGroup == null)
  86. {
  87. Log.Warning("Sound group '{0}' is invalid.", soundGroupName);
  88. return true;
  89. }
  90. return soundGroup.Mute;
  91. }
  92. public static void Mute(this SoundComponent soundComponent, string soundGroupName, bool mute)
  93. {
  94. if (string.IsNullOrEmpty(soundGroupName))
  95. {
  96. Log.Warning("Sound group is invalid.");
  97. return;
  98. }
  99. ISoundGroup soundGroup = soundComponent.GetSoundGroup(soundGroupName);
  100. if (soundGroup == null)
  101. {
  102. Log.Warning("Sound group '{0}' is invalid.", soundGroupName);
  103. return;
  104. }
  105. soundGroup.Mute = mute;
  106. GameEntry.Setting.SetBool(Utility.Text.Format(Constant.Setting.SoundGroupMuted, soundGroupName), mute);
  107. GameEntry.Setting.Save();
  108. }
  109. public static float GetVolume(this SoundComponent soundComponent, string soundGroupName)
  110. {
  111. if (string.IsNullOrEmpty(soundGroupName))
  112. {
  113. Log.Warning("Sound group is invalid.");
  114. return 0f;
  115. }
  116. ISoundGroup soundGroup = soundComponent.GetSoundGroup(soundGroupName);
  117. if (soundGroup == null)
  118. {
  119. Log.Warning("Sound group '{0}' is invalid.", soundGroupName);
  120. return 0f;
  121. }
  122. return soundGroup.Volume;
  123. }
  124. public static void SetVolume(this SoundComponent soundComponent, string soundGroupName, float volume)
  125. {
  126. if (string.IsNullOrEmpty(soundGroupName))
  127. {
  128. Log.Warning("Sound group is invalid.");
  129. return;
  130. }
  131. ISoundGroup soundGroup = soundComponent.GetSoundGroup(soundGroupName);
  132. if (soundGroup == null)
  133. {
  134. Log.Warning("Sound group '{0}' is invalid.", soundGroupName);
  135. return;
  136. }
  137. soundGroup.Volume = volume;
  138. GameEntry.Setting.SetFloat(Utility.Text.Format(Constant.Setting.SoundGroupVolume, soundGroupName), volume);
  139. GameEntry.Setting.Save();
  140. }
  141. }
  142. }