AudioMixerMgr.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Audio;
  5. using wf;
  6. public class AudioMixerMgr : MonoBehaviour
  7. {
  8. public AudioMixerGroup this[AudioMixerMgr.Group group]
  9. {
  10. get
  11. {
  12. return this.group_dic_[group];
  13. }
  14. }
  15. public AudioMixerGroup this[AudioSourceMgr.Type type]
  16. {
  17. get
  18. {
  19. switch (type)
  20. {
  21. case AudioSourceMgr.Type.Bgm:
  22. return this.group_dic_[AudioMixerMgr.Group.BGM];
  23. case AudioSourceMgr.Type.Se:
  24. return this.group_dic_[AudioMixerMgr.Group.SE];
  25. case AudioSourceMgr.Type.System:
  26. return this.group_dic_[AudioMixerMgr.Group.System];
  27. case AudioSourceMgr.Type.Env:
  28. return this.group_dic_[AudioMixerMgr.Group.Env];
  29. case AudioSourceMgr.Type.Voice:
  30. return this.group_dic_[AudioMixerMgr.Group.Voice];
  31. case AudioSourceMgr.Type.VoiceHeroine:
  32. return this.group_dic_[AudioMixerMgr.Group.VoiceHeroine];
  33. case AudioSourceMgr.Type.VoiceSub:
  34. return this.group_dic_[AudioMixerMgr.Group.VoiceSub];
  35. case AudioSourceMgr.Type.VoiceExtra:
  36. return this.group_dic_[AudioMixerMgr.Group.VoiceExtra];
  37. case AudioSourceMgr.Type.VoiceMob:
  38. return this.group_dic_[AudioMixerMgr.Group.VoiceMob];
  39. default:
  40. return this.group_dic_[AudioMixerMgr.Group.BGM];
  41. }
  42. }
  43. }
  44. public void Awake()
  45. {
  46. string[] array = new string[]
  47. {
  48. "Master",
  49. "Master/BGM",
  50. "Master/Dance",
  51. "Master/System",
  52. "Master/Voice",
  53. "Master/Voice/Heroine",
  54. "Master/Voice/Sub",
  55. "Master/Voice/Extra",
  56. "Master/Voice/Mob",
  57. "Master/SE",
  58. "Master/Env"
  59. };
  60. foreach (string text in array)
  61. {
  62. AudioMixerGroup[] array3 = this.Mixer.FindMatchingGroups(text);
  63. if (array3 == null || array3.Length <= 0)
  64. {
  65. Debug.Log("オーディオ初期化エラー");
  66. Debug.Log(text + "が失敗");
  67. NUty.WinMessageBox(NUty.GetWindowHandle(), "オーディオ機能が利用できません", "オーディオ初期化エラー", 16);
  68. Application.Quit();
  69. return;
  70. }
  71. }
  72. this.group_dic_ = new Dictionary<AudioMixerMgr.Group, AudioMixerGroup>();
  73. this.group_dic_.Add(AudioMixerMgr.Group.Master, this.Mixer.FindMatchingGroups("Master")[0]);
  74. this.group_dic_.Add(AudioMixerMgr.Group.BGM, this.Mixer.FindMatchingGroups("Master/BGM")[0]);
  75. this.group_dic_.Add(AudioMixerMgr.Group.Dance, this.Mixer.FindMatchingGroups("Master/Dance")[0]);
  76. this.group_dic_.Add(AudioMixerMgr.Group.System, this.Mixer.FindMatchingGroups("Master/System")[0]);
  77. this.group_dic_.Add(AudioMixerMgr.Group.Voice, this.Mixer.FindMatchingGroups("Master/Voice")[0]);
  78. this.group_dic_.Add(AudioMixerMgr.Group.VoiceHeroine, this.Mixer.FindMatchingGroups("Master/Voice/Heroine")[0]);
  79. this.group_dic_.Add(AudioMixerMgr.Group.VoiceSub, this.Mixer.FindMatchingGroups("Master/Voice/Sub")[0]);
  80. this.group_dic_.Add(AudioMixerMgr.Group.VoiceExtra, this.Mixer.FindMatchingGroups("Master/Voice/Extra")[0]);
  81. this.group_dic_.Add(AudioMixerMgr.Group.VoiceMob, this.Mixer.FindMatchingGroups("Master/Voice/Mob")[0]);
  82. this.group_dic_.Add(AudioMixerMgr.Group.SE, this.Mixer.FindMatchingGroups("Master/SE")[0]);
  83. this.group_dic_.Add(AudioMixerMgr.Group.Env, this.Mixer.FindMatchingGroups("Master/Env")[0]);
  84. }
  85. public void Start()
  86. {
  87. this.call_start = true;
  88. foreach (AudioMixerMgr.VolumePropData volumePropData in this.VolumePropDatas)
  89. {
  90. this.Mixer.GetFloat(volumePropData.name, out volumePropData.val);
  91. volumePropData.val = Utility.DecibelToVolume(volumePropData.val);
  92. this.volume_prop_acc_.Add(volumePropData.group, volumePropData);
  93. }
  94. for (int j = 0; j < this.delay_setting_.Count; j++)
  95. {
  96. this.SetVolume(this.delay_setting_[j].Key, this.delay_setting_[j].Value);
  97. }
  98. this.delay_setting_.Clear();
  99. }
  100. public void SetVolume(AudioMixerMgr.Group group, float volume)
  101. {
  102. if (!this.call_start)
  103. {
  104. if (this.delay_setting_ == null)
  105. {
  106. this.delay_setting_ = new List<KeyValuePair<AudioMixerMgr.Group, float>>();
  107. }
  108. this.delay_setting_.Add(new KeyValuePair<AudioMixerMgr.Group, float>(group, volume));
  109. return;
  110. }
  111. if (this.volume_prop_acc_[group].val != volume)
  112. {
  113. this.volume_prop_acc_[group].val = volume;
  114. this.Mixer.SetFloat(this.volume_prop_acc_[group].name, Utility.VolumeToDecibel(volume));
  115. }
  116. }
  117. public const int kGroupMax = 11;
  118. public AudioMixer Mixer;
  119. public AudioMixerMgr.VolumePropData[] VolumePropDatas;
  120. private Dictionary<AudioMixerMgr.Group, AudioMixerMgr.VolumePropData> volume_prop_acc_ = new Dictionary<AudioMixerMgr.Group, AudioMixerMgr.VolumePropData>();
  121. private Dictionary<AudioMixerMgr.Group, AudioMixerGroup> group_dic_ = new Dictionary<AudioMixerMgr.Group, AudioMixerGroup>();
  122. private bool call_start;
  123. private List<KeyValuePair<AudioMixerMgr.Group, float>> delay_setting_;
  124. public enum Group
  125. {
  126. Master,
  127. System,
  128. BGM,
  129. Dance,
  130. Voice,
  131. VoiceHeroine,
  132. VoiceSub,
  133. VoiceExtra,
  134. VoiceMob,
  135. SE,
  136. Env
  137. }
  138. [Serializable]
  139. public class VolumePropData
  140. {
  141. public AudioMixerMgr.Group group;
  142. public string name;
  143. public float val;
  144. }
  145. }