VolumePanel.cs 3.5 KB

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