ONSPAudioSource.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using UnityEngine;
  3. public class ONSPAudioSource : MonoBehaviour
  4. {
  5. public bool EnableSpatialization
  6. {
  7. get
  8. {
  9. return this.enableSpatialization;
  10. }
  11. set
  12. {
  13. this.enableSpatialization = value;
  14. }
  15. }
  16. public float Gain
  17. {
  18. get
  19. {
  20. return this.gain;
  21. }
  22. set
  23. {
  24. this.gain = Mathf.Clamp(value, 0f, 24f);
  25. }
  26. }
  27. public bool UseInvSqr
  28. {
  29. get
  30. {
  31. return this.useInvSqr;
  32. }
  33. set
  34. {
  35. this.useInvSqr = value;
  36. }
  37. }
  38. public float Near
  39. {
  40. get
  41. {
  42. return this.near;
  43. }
  44. set
  45. {
  46. this.near = Mathf.Clamp(value, 0f, 1000000f);
  47. }
  48. }
  49. public float Far
  50. {
  51. get
  52. {
  53. return this.far;
  54. }
  55. set
  56. {
  57. this.far = Mathf.Clamp(value, 0f, 1000000f);
  58. }
  59. }
  60. public bool DisableRfl
  61. {
  62. get
  63. {
  64. return this.disableRfl;
  65. }
  66. set
  67. {
  68. this.disableRfl = value;
  69. }
  70. }
  71. private void Awake()
  72. {
  73. AudioSource component = base.GetComponent<AudioSource>();
  74. this.SetParameters(ref component);
  75. }
  76. private void Start()
  77. {
  78. }
  79. private void Update()
  80. {
  81. AudioSource component = base.GetComponent<AudioSource>();
  82. this.SetParameters(ref component);
  83. }
  84. public void SetParameters(ref AudioSource source)
  85. {
  86. if (!Application.isPlaying || AudioListener.pause || !source.isPlaying || !source.isActiveAndEnabled)
  87. {
  88. source.spatialize = false;
  89. return;
  90. }
  91. source.spatialize = this.enableSpatialization;
  92. source.SetSpatializerFloat(0, this.gain);
  93. if (this.useInvSqr)
  94. {
  95. source.SetSpatializerFloat(1, 1f);
  96. }
  97. else
  98. {
  99. source.SetSpatializerFloat(1, 0f);
  100. }
  101. source.SetSpatializerFloat(2, this.near);
  102. source.SetSpatializerFloat(3, this.far);
  103. if (this.disableRfl)
  104. {
  105. source.SetSpatializerFloat(4, 1f);
  106. }
  107. else
  108. {
  109. source.SetSpatializerFloat(4, 0f);
  110. }
  111. }
  112. [SerializeField]
  113. private bool enableSpatialization = true;
  114. [SerializeField]
  115. private float gain;
  116. [SerializeField]
  117. private bool useInvSqr;
  118. [SerializeField]
  119. private float near = 1f;
  120. [SerializeField]
  121. private float far = 10f;
  122. [SerializeField]
  123. private bool disableRfl;
  124. }