SFXClip.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Audio;
  5. using Sirenix.OdinInspector;
  6. #if UNITY_EDITOR
  7. using Unity.EditorCoroutines.Editor;
  8. #endif
  9. namespace KairoEngine.SFX
  10. {
  11. public enum SFXClipType
  12. {
  13. Single,
  14. RandomList,
  15. Steps
  16. }
  17. public enum SFXSpace
  18. {
  19. Stereo,
  20. GameWorld,
  21. Mix
  22. }
  23. [HideMonoScript]
  24. [CreateAssetMenu(menuName = "KairoEngine/SFX/Clip", fileName = "SFXClip", order = 1)]
  25. public class SFXClip : ScriptableObject
  26. {
  27. [Space, Title("Audio Clip")]
  28. [PropertyOrder(0)] public SFXClipType sfxType = SFXClipType.Single;
  29. [PropertyOrder(2), Required, ShowIf("@sfxType == SFXClipType.Single")]
  30. public AudioClip clip;
  31. [PropertyOrder(3), Required, ShowIf("@sfxType == SFXClipType.RandomList")]
  32. public List<AudioClip> clips;
  33. [PropertyOrder(4), Required, ShowIf("@sfxType == SFXClipType.Steps"), ShowInInspector]
  34. public List<SFXClipStep> clipSteps;
  35. #region PlayButtons
  36. #if UNITY_EDITOR
  37. private bool stopSteps = false;
  38. [PropertyOrder(5), Button("Stop"), HorizontalGroup("Controls")]
  39. public void StopEditor()
  40. {
  41. if(sfxType == SFXClipType.Single && clip != null) SFXUtil.StopClip(clip);
  42. else if(sfxType == SFXClipType.RandomList && clips != null)
  43. {
  44. if(clips.Count > 0)
  45. {
  46. foreach (var clip in clips) SFXUtil.StopClip(clip);
  47. }
  48. }
  49. else if(sfxType == SFXClipType.Steps && clipSteps != null)
  50. {
  51. stopSteps = true;
  52. foreach (var step in clipSteps) step.clip.StopEditor();
  53. }
  54. SFXUtil.StopAllClips();
  55. }
  56. [PropertyOrder(6), Button("Play"), HorizontalGroup("Controls")]
  57. public void PlayEditor()
  58. {
  59. if(sfxType == SFXClipType.Single && clip != null) SFXUtil.PlayClip(clip);
  60. else if(sfxType == SFXClipType.RandomList && clips != null)
  61. {
  62. if(clips.Count > 0)
  63. {
  64. SFXUtil.PlayClip(clips[Random.Range(0, clips.Count)]);
  65. }
  66. }
  67. else if(sfxType == SFXClipType.Steps && clipSteps != null)
  68. {
  69. stopSteps = false;
  70. foreach (var step in clipSteps)
  71. {
  72. EditorCoroutineUtility.StartCoroutineOwnerless(PlayStep(step.startTime, step.clip));
  73. }
  74. }
  75. }
  76. public IEnumerator PlayStep(float waitTime, SFXClip clip)
  77. {
  78. bool hasPlayed = false;
  79. var waitFor = new EditorWaitForSeconds(waitTime);
  80. while (hasPlayed == false)
  81. {
  82. yield return waitFor;
  83. if(stopSteps == false)
  84. {
  85. hasPlayed = true;
  86. if(clip.sfxType == SFXClipType.Single) SFXUtil.PlayClip(clip.clip);
  87. else if (clip.sfxType == SFXClipType.RandomList)
  88. {
  89. if(clip.clips != null)
  90. {
  91. if(clip.clips.Count > 0)
  92. {
  93. SFXUtil.PlayClip(clip.clips[Random.Range(0, clip.clips.Count)]);
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. #endif
  101. #endregion
  102. [Title("Clip Settings")]
  103. [PropertyOrder(7), ShowIf("@sfxType == SFXClipType.Single || sfxType == SFXClipType.RandomList")] public bool loop = false;
  104. [PropertyOrder(8), Range(0f, 1f)] public float volume = 1f;
  105. [PropertyOrder(9), Range(0f, 0.2f)] public float volumeVariation = 0.05f;
  106. [PropertyOrder(10), Range(0f, 2f)] public float pitch = 1f;
  107. [PropertyOrder(11), Range(0f, 0.2f)] public float pitchVariation = 0.05f;
  108. [PropertyOrder(12), HorizontalGroup("Trim"), LabelText("@trim ? \"Trim Start/Stop\" : \"Trim\"")] public bool trim = false;
  109. [PropertyOrder(13), HorizontalGroup("Trim", Width = 0.272f), HideLabel, SuffixLabel("seconds", true), ShowIf("@trim")] public float trimStartDuration = 0;
  110. [PropertyOrder(14), HorizontalGroup("Trim", Width = 0.272f), HideLabel, SuffixLabel("seconds", true), ShowIf("@trim")] public float trimEndDuration = 0;
  111. [PropertyOrder(15), HorizontalGroup("FadeIn"), LabelText("@fadeIn ? \"Fade In Duration\" : \"Fade In\"")] public bool fadeIn = false;
  112. [PropertyOrder(16), HorizontalGroup("FadeIn", Width = 0.55f), HideLabel, SuffixLabel("seconds", true), ShowIf("@fadeIn")] public float fadeInDuration = 1f;
  113. [PropertyOrder(17), HorizontalGroup("FadeOut"), LabelText("@fadeOut ? \"Fade Out Duration\" : \"Fade Out\"")] public bool fadeOut = false;
  114. [PropertyOrder(18), HorizontalGroup("FadeOut", Width = 0.55f), HideLabel, SuffixLabel("seconds", true), ShowIf("@fadeOut")] public float fadeOutDuration = 1f;
  115. [PropertyOrder(19)] public SFXSpace soundSpace = SFXSpace.GameWorld;
  116. [PropertyOrder(20), Range(-1f, 1f), ShowIf("@soundSpace == SFXSpace.Stereo || soundSpace == SFXSpace.Mix")] public float pan = 0f;
  117. [PropertyOrder(21), Range(0f, 0.2f), ShowIf("@soundSpace == SFXSpace.Stereo || soundSpace == SFXSpace.Mix")] public float panVariation = 0.05f;
  118. [PropertyOrder(22), Range(0f, 1f), ShowIf("@soundSpace == SFXSpace.Mix")] public float spatialBlend = 0f;
  119. [PropertyOrder(23),Range(0f, 0.2f), ShowIf("@soundSpace == SFXSpace.Mix")] public float spatialBlendVariation = 0.05f;
  120. [PropertyOrder(24), ShowIf("@soundSpace == SFXSpace.GameWorld || soundSpace == SFXSpace.Mix"), LabelText("Distance")]
  121. [HorizontalGroup("Distance"), SuffixLabel("min", true)]
  122. public float minDistance = 3f;
  123. [PropertyOrder(25), ShowIf("@soundSpace == SFXSpace.GameWorld || soundSpace == SFXSpace.Mix"), HideLabel()]
  124. [HorizontalGroup("Distance", Width = 0.3f), SuffixLabel("max", true)]
  125. public float maxDistance = 100f;
  126. [PropertyOrder(26), Required, HideIf("@sfxType == SFXClipType.Steps")] public AudioMixerGroup audioMixerChannel;
  127. [PropertyOrder(27)] public bool removeOnSceneChange = false;
  128. [Title("World Settings")]
  129. [PropertyOrder(28)] public bool sensorDetection = true;
  130. [PropertyOrder(29), ShowIf(@"sensorDetection"), Range(0f, 100f)] public float soundRadius = 2f;
  131. }
  132. }