FPlaySoundEvent.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Security;
  4. namespace Flux
  5. {
  6. [FEvent("Skill/美术/播放声音")]
  7. public class FPlaySoundEvent : FEvent
  8. {
  9. [SerializeField]
  10. private AudioClip _soundClip = null;
  11. public AudioClip SoundClip { get { return _soundClip; } }
  12. [Range(0f, 1f)]
  13. [SerializeField]
  14. private float _volume = 1f;
  15. [SerializeField]
  16. private bool _loop = false;
  17. public bool Loop { get { return _loop; } }
  18. [SerializeField]
  19. private bool _speedDeterminesPitch = true;
  20. public bool SpeedDeterminesPitch { get { return _speedDeterminesPitch; } set { _speedDeterminesPitch = value; } }
  21. private AudioSource _source;
  22. private float _previousTimeSinceTrigger = 0;
  23. private float _previousSpeed = 0;
  24. public FPlaySoundEvent()
  25. {
  26. _eventType = SkillActionFrameEventType.FE_PlaySound;
  27. }
  28. protected override void OnTrigger(float timeSinceTrigger)
  29. {
  30. if(Application.isPlaying)
  31. {
  32. _source = Owner.GetComponent<AudioSource>();
  33. if (_source == null)
  34. _source = Owner.gameObject.AddComponent<AudioSource>();
  35. _source.volume = _volume;
  36. _source.loop = _loop;
  37. _source.clip = _soundClip;
  38. if (Sequence.IsPlaying)
  39. _source.Play();
  40. _source.time = timeSinceTrigger;
  41. if (SpeedDeterminesPitch)
  42. _source.pitch = Sequence.Speed * Time.timeScale;
  43. }
  44. else
  45. {
  46. #if UNITY_EDITOR
  47. UnityEditor.AudioUtility.PlayClip(_soundClip);
  48. #endif
  49. }
  50. }
  51. protected override void OnPause()
  52. {
  53. if(Application.isPlaying)
  54. {
  55. _source.Pause();
  56. }
  57. else
  58. {
  59. #if UNITY_EDITOR
  60. UnityEditor.AudioUtility.PauseClip(_soundClip);
  61. #endif
  62. }
  63. }
  64. protected override void OnResume()
  65. {
  66. if (Application.isPlaying)
  67. {
  68. if (Sequence.IsPlaying)
  69. _source.Play();
  70. }
  71. else
  72. {
  73. #if UNITY_EDITOR
  74. UnityEditor.AudioUtility.PlayClip(_soundClip);
  75. #endif
  76. }
  77. }
  78. protected override void OnFinish()
  79. {
  80. if (Application.isPlaying)
  81. {
  82. if (_source.clip == _soundClip && _source.isPlaying)
  83. {
  84. _source.Stop();
  85. _source.clip = null;
  86. }
  87. }
  88. else
  89. {
  90. #if UNITY_EDITOR
  91. UnityEditor.AudioUtility.StopClip(_soundClip);
  92. #endif
  93. }
  94. }
  95. protected override void OnStop()
  96. {
  97. if (Application.isPlaying)
  98. {
  99. if (_source.clip == _soundClip && _source.isPlaying)
  100. {
  101. _source.Stop();
  102. _source.clip = null;
  103. }
  104. }
  105. else
  106. {
  107. #if UNITY_EDITOR
  108. UnityEditor.AudioUtility.StopClip(_soundClip);
  109. #endif
  110. }
  111. }
  112. public override int GetMaxLength()
  113. {
  114. if (_loop || _soundClip == null)
  115. return base.GetMaxLength();
  116. return Mathf.RoundToInt(_soundClip.length * Sequence.FrameRate);
  117. }
  118. public int GetMaxStartOffset()
  119. {
  120. if (_soundClip == null)
  121. return 0;
  122. int maxFrames = Mathf.RoundToInt(_soundClip.length * Sequence.FrameRate);
  123. if (_loop)
  124. return maxFrames;
  125. return maxFrames - Length;
  126. }
  127. public override string Text
  128. {
  129. get { return "播放声音"; }
  130. set { }
  131. }
  132. public override SecurityElement SaveToXml()
  133. {
  134. SecurityElement node = base.SaveToXml();
  135. if (_soundClip != null)
  136. {
  137. #if UNITY_EDITOR
  138. string audioPath = UnityEditor.AssetDatabase.GetAssetPath(_soundClip);
  139. string relativeName = FileUtils.RemoveParent(Constants.FightAudioPath, audioPath);
  140. SecurityElement paramNode = WriteParamNode("sound", relativeName, "string");
  141. node.AddChild(paramNode);
  142. paramNode = WriteParamNode("soundPath", audioPath, "string");
  143. node.AddChild(paramNode);
  144. #endif
  145. }
  146. return node;
  147. }
  148. public override void LoadFromXml(SecurityElement eventNode)
  149. {
  150. base.LoadFromXml(eventNode);
  151. string soundPath = GetSParam("soundPath");
  152. if(!string.IsNullOrEmpty(soundPath))
  153. {
  154. #if UNITY_EDITOR
  155. _soundClip = UnityEditor.AssetDatabase.LoadAssetAtPath<AudioClip>(soundPath);
  156. #endif
  157. }
  158. }
  159. }
  160. }