VolumePanel.cs 3.3 KB

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