VolumePanel.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. public class VolumePanel : MonoBehaviour
  5. {
  6. private static WaitForSeconds _waitForSeconds0_15 = new WaitForSeconds(0.15f);
  7. private static readonly int CloseHash = Animator.StringToHash("Close");
  8. private static readonly int OpenHash = Animator.StringToHash("Open");
  9. [Header("SEスライダー")]
  10. public Slider SESlier;
  11. [Header("BGMスライダー")]
  12. public Slider BGMSlier;
  13. [Header("アニメーター")]
  14. public Animator anim;
  15. public void Open()
  16. {
  17. SESlier.onValueChanged.RemoveAllListeners();
  18. BGMSlier.onValueChanged.RemoveAllListeners();
  19. if (ContinuousController.instance != null)
  20. {
  21. SESlier.value = ConvertPlayerPrefsValueToSliderValue(ContinuousController.instance.SEVolume, SESlier);
  22. BGMSlier.value = ConvertPlayerPrefsValueToSliderValue(ContinuousController.instance.BGMVolume, BGMSlier);
  23. }
  24. SESlier.onValueChanged.AddListener(value =>
  25. {
  26. value = ConvertSliderValueToPlayerPrefsValue(value, SESlier);
  27. SetSEVlolume(value);
  28. PlaySampleSE(value);
  29. });
  30. BGMSlier.onValueChanged.AddListener(value =>
  31. {
  32. value = ConvertSliderValueToPlayerPrefsValue(value, BGMSlier);
  33. SetBGMVlolume(value);
  34. });
  35. gameObject.SetActive(true);
  36. anim.SafeSetInt(OpenHash, 1);
  37. anim.SafeSetInt(CloseHash, 0);
  38. }
  39. public void Off()
  40. {
  41. this.gameObject.SetActive(false);
  42. }
  43. public void Close()
  44. {
  45. Close_(true);
  46. }
  47. public void Close_(bool playSE)
  48. {
  49. if (playSE)
  50. {
  51. if (Opening.instance != null)
  52. {
  53. Opening.instance.PlayCancelSE();
  54. }
  55. else if (GManager.instance != null)
  56. {
  57. GManager.instance.PlayCancelSE();
  58. }
  59. }
  60. anim.SafeSetInt(OpenHash, 0);
  61. anim.SafeSetInt(CloseHash, 1);
  62. }
  63. float ConvertPlayerPrefsValueToSliderValue(float playerPrefsValue, Slider slider)
  64. {
  65. return playerPrefsValue * (slider.wholeNumbers ? slider.maxValue : 1);
  66. }
  67. float ConvertSliderValueToPlayerPrefsValue(float sliderValue, Slider slider)
  68. {
  69. return sliderValue / (slider.wholeNumbers && slider.maxValue > 0 ? slider.maxValue : 1);
  70. }
  71. public void Init()
  72. {
  73. Off();
  74. }
  75. public void SetSEVlolume(float value)
  76. {
  77. if (ContinuousController.instance != null)
  78. {
  79. ContinuousController.instance.SetSEVolume(value);
  80. }
  81. }
  82. bool _notPlaySample = false;
  83. public void PlaySampleSE(float _)
  84. {
  85. if (Opening.instance != null)
  86. {
  87. if (!_notPlaySample)
  88. {
  89. if (Opening.instance != null)
  90. {
  91. Opening.instance.PlayDecisionSE();
  92. }
  93. else if (GManager.instance != null)
  94. {
  95. GManager.instance.PlayDecisionSE();
  96. }
  97. ContinuousController.instance.StartCoroutine(TimerCoroutine());
  98. }
  99. }
  100. }
  101. IEnumerator TimerCoroutine()
  102. {
  103. _notPlaySample = true;
  104. yield return _waitForSeconds0_15;
  105. _notPlaySample = false;
  106. }
  107. public void SetBGMVlolume(float value)
  108. {
  109. if (ContinuousController.instance != null)
  110. {
  111. ContinuousController.instance.SetBGMVolume(value);
  112. }
  113. }
  114. }