FSkillSingEvent.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Security;
  5. namespace Flux
  6. {
  7. [FEvent("Skill/动作相关/技能吟唱")]
  8. public class FSkillSingEvent : FEvent
  9. {
  10. [HideInInspector]
  11. public string _animName;
  12. [HideInInspector]
  13. [Tooltip("What's the offset from where we play the animation?")]
  14. public int _startOffset;
  15. [SerializeField]
  16. private AudioClip _audioClip = null;
  17. public AudioClip AudioClip { get { return _audioClip; } }
  18. [HideInInspector]
  19. [SerializeField]
  20. private int _effectId;
  21. public int EffectId
  22. {
  23. get { return _effectId; }
  24. }
  25. [HideInInspector]
  26. [SerializeField]
  27. private GameObject _effectPrefab = null;
  28. public GameObject EffectPrefab
  29. {
  30. get { return _effectPrefab; }
  31. }
  32. [HideInInspector]
  33. [SerializeField]
  34. private string _effectLink;
  35. public string EffectLink
  36. {
  37. get { return _effectLink; }
  38. }
  39. private Animator _animator = null;
  40. public Animator animator
  41. {
  42. get { return _animator; }
  43. }
  44. private AudioSource _source;
  45. private ParticleSystem _particleSystem = null;
  46. private GameObject _effectGo = null;
  47. private float _previousTimeSinceTrigger = 0;
  48. private float _previousSpeed = 0;
  49. public FSkillSingEvent()
  50. {
  51. _eventType = SkillActionFrameEventType.FE_Skill_Sing;
  52. }
  53. protected override void OnSetDefaultValues()
  54. {
  55. base.OnSetDefaultValues();
  56. if (_casterTrans != null)
  57. {
  58. _animator = _casterTrans.gameObject.GetComponent<Animator>();
  59. }
  60. }
  61. protected override void OnInit()
  62. {
  63. if (_animator != null)
  64. _animator.enabled = false;
  65. }
  66. protected override void OnTrigger(float timeSinceTrigger)
  67. {
  68. if (Application.isPlaying)
  69. {
  70. _source = Owner.GetComponent<AudioSource>();
  71. if (_source == null)
  72. _source = Owner.gameObject.AddComponent<AudioSource>();
  73. _source.volume = 1;
  74. _source.loop = false;
  75. _source.clip = _audioClip;
  76. if (Sequence.IsPlaying)
  77. _source.Play();
  78. }
  79. else
  80. {
  81. #if UNITY_EDITOR
  82. UnityEditor.AudioUtility.PlayClip(_audioClip);
  83. #endif
  84. }
  85. if(_effectPrefab != null)
  86. {
  87. Vector3 pos = _casterTrans.position;
  88. Transform linkPoint = UnityEngineUtils.RecurisiveFindTransformChild(_casterTrans, _effectLink);
  89. if (linkPoint != null)
  90. {
  91. pos = linkPoint.position;
  92. }
  93. _effectGo = (GameObject)Instantiate(_effectPrefab);
  94. _effectGo.transform.SetParent(linkPoint);
  95. _effectGo.transform.localPosition = Vector3.zero;
  96. _effectGo.transform.localScale = Vector3.one;
  97. _effectGo.transform.localRotation = Quaternion.identity;
  98. _particleSystem = _effectGo.GetComponentInChildren<ParticleSystem>();
  99. if (_particleSystem != null)
  100. {
  101. ParticleSystem.MainModule mainModule = _particleSystem.main;
  102. mainModule.simulationSpeed = Sequence.Speed;
  103. }
  104. }
  105. if (_particleSystem != null && Sequence.IsPlaying && Sequence.IsPlayingForward)
  106. _particleSystem.Play(true);
  107. if(_animator!=null)
  108. {
  109. _animator.enabled = true;
  110. int id = GetId();
  111. #if UNITY_EDITOR
  112. if (_animator.enabled && (!_track.HasCache || Application.isPlaying))
  113. #else
  114. if( _animator.enabled )
  115. #endif
  116. {
  117. _animator.SetLayerWeight(0, 1);
  118. if (id == 0 || _track.Events[id - 1].End < Start)
  119. {
  120. _animator.Play(_animName,0);
  121. }
  122. if (timeSinceTrigger > 0)
  123. {
  124. // - 0.001f because if you pass the length of the animation
  125. // it seems that it will go over it and miss the condition
  126. _animator.Update(timeSinceTrigger - 0.001f);
  127. }
  128. else
  129. _animator.Update(0f);
  130. }
  131. }
  132. _previousTimeSinceTrigger = 0;
  133. _previousSpeed = Sequence.Speed;
  134. }
  135. protected override void OnPause()
  136. {
  137. if(Application.isPlaying)
  138. {
  139. if (_source)
  140. _source.Pause();
  141. }else
  142. {
  143. #if UNITY_EDITOR
  144. UnityEditor.AudioUtility.PauseClip(_audioClip);
  145. #endif
  146. }
  147. if (_particleSystem != null)
  148. _particleSystem.Pause();
  149. if(_animator!=null)
  150. _animator.enabled = false;
  151. }
  152. protected override void OnResume()
  153. {
  154. if(Application.isPlaying)
  155. {
  156. if (Sequence.IsPlaying)
  157. {
  158. if (_source)
  159. _source.Play();
  160. }
  161. }
  162. else
  163. {
  164. #if UNITY_EDITOR
  165. UnityEditor.AudioUtility.PlayClip(_audioClip);
  166. #endif
  167. }
  168. if (_particleSystem != null && Sequence.IsPlayingForward)
  169. _particleSystem.Play(true);
  170. if(_animator!=null)
  171. _animator.enabled = true;
  172. }
  173. protected override void OnFinish()
  174. {
  175. if(Application.isPlaying)
  176. {
  177. if (_source)
  178. {
  179. if (_source.clip == _audioClip && _source.isPlaying)
  180. {
  181. _source.Stop();
  182. _source.clip = null;
  183. }
  184. }
  185. }
  186. else
  187. {
  188. #if UNITY_EDITOR
  189. UnityEditor.AudioUtility.StopClip(_audioClip);
  190. #endif
  191. }
  192. if (_animator && (IsLastEvent || _track.GetEvent(GetId() + 1).Start != End))
  193. {
  194. _animator.SetLayerWeight(0, 0);
  195. _animator.enabled = false;
  196. }
  197. if (_particleSystem != null)
  198. _particleSystem.Stop(true);
  199. if(_effectGo!=null)
  200. {
  201. GameObject.DestroyImmediate(_effectGo);
  202. _effectGo = null;
  203. }
  204. }
  205. protected override void OnStop()
  206. {
  207. if (Application.isPlaying)
  208. {
  209. if (_source)
  210. {
  211. if (_source.clip == _audioClip && _source.isPlaying)
  212. {
  213. _source.Stop();
  214. _source.clip = null;
  215. }
  216. }
  217. }
  218. else
  219. {
  220. #if UNITY_EDITOR
  221. UnityEditor.AudioUtility.StopClip(_audioClip);
  222. #endif
  223. }
  224. int id = GetId();
  225. if (_animator && (id == 0 || _track.GetEvent(id - 1).End != Start))
  226. {
  227. _animator.Play("f_idle", 0);
  228. _animator.Update(0);
  229. }
  230. if (_particleSystem != null)
  231. {
  232. _particleSystem.Stop(true);
  233. _particleSystem.Clear(true);
  234. }
  235. if (_effectGo != null)
  236. {
  237. GameObject.DestroyImmediate(_effectGo);
  238. _effectGo = null;
  239. }
  240. }
  241. protected override void OnUpdateEvent(float timeSinceTrigger)
  242. {
  243. float delta = timeSinceTrigger - _previousTimeSinceTrigger;
  244. _previousTimeSinceTrigger = timeSinceTrigger;
  245. if (_animator!=null)
  246. {
  247. if (!_animator.enabled)
  248. _animator.enabled = true;
  249. if(delta > 0)
  250. {
  251. _animator.SetFloat("moveSpeed", 1);
  252. _animator.Update(delta);
  253. }
  254. else if(delta < 0)
  255. {
  256. _animator.SetFloat("moveSpeed", -1);
  257. _animator.Update(-delta);
  258. }
  259. }
  260. if(_source!=null && _source.clip!=null)
  261. {
  262. _source.PlayScheduled(delta);
  263. }
  264. if (_particleSystem != null)
  265. {
  266. if (!Sequence.IsPlaying || !Sequence.IsPlayingForward)
  267. {
  268. _previousSpeed = 1;
  269. ParticleSystem.MainModule mainModule = _particleSystem.main;
  270. mainModule.simulationSpeed = _previousSpeed;
  271. if (Sequence.IsPlayingForward && delta > 0)
  272. {
  273. _particleSystem.Simulate(delta, true, false);
  274. }
  275. }
  276. else if (_previousSpeed != Sequence.Speed)
  277. {
  278. _previousSpeed = Sequence.Speed;
  279. ParticleSystem.MainModule mainModule = _particleSystem.main;
  280. mainModule.simulationSpeed = Mathf.Abs(_previousSpeed);
  281. }
  282. }
  283. }
  284. public override int GetMaxLength()
  285. {
  286. return base.GetMaxLength();
  287. }
  288. public override string Text
  289. {
  290. get { return "吟唱"; }
  291. set { }
  292. }
  293. public override SecurityElement SaveToXml()
  294. {
  295. SecurityElement node = base.SaveToXml();
  296. SecurityElement paramNode = WriteParamNode("animName", _animName, "string");
  297. if (paramNode != null)
  298. node.AddChild(paramNode);
  299. paramNode = WriteParamNode("hiteffect", _effectId.ToString(), "int");
  300. node.AddChild(paramNode);
  301. if (_effectPrefab != null)
  302. {
  303. #if UNITY_EDITOR
  304. paramNode = WriteParamNode("effectPrefab", UnityEditor.AssetDatabase.GetAssetPath(_effectPrefab), "string");
  305. node.AddChild(paramNode);
  306. #endif
  307. }
  308. paramNode = WriteParamNode("effectLink", _effectLink, "string");
  309. if (paramNode != null)
  310. node.AddChild(paramNode);
  311. if (_audioClip != null)
  312. {
  313. #if UNITY_EDITOR
  314. string audioPath = UnityEditor.AssetDatabase.GetAssetPath(_audioClip);
  315. string relativeName = FileUtils.RemoveParent(Constants.FightAudioPath, audioPath);
  316. paramNode = WriteParamNode("sound", relativeName, "string");
  317. node.AddChild(paramNode);
  318. paramNode = WriteParamNode("soundPath", audioPath, "string");
  319. node.AddChild(paramNode);
  320. #endif
  321. }
  322. return node;
  323. }
  324. public override void LoadFromXml(SecurityElement eventNode)
  325. {
  326. base.LoadFromXml(eventNode);
  327. _animName = GetSParam("animName");
  328. _effectId = GetNParam("hiteffect");
  329. string effectPrefab = GetSParam("effectPrefab");
  330. if(!string.IsNullOrEmpty(effectPrefab))
  331. {
  332. #if UNITY_EDITOR
  333. _effectPrefab = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(effectPrefab);
  334. #endif
  335. }
  336. _effectLink = GetSParam("effectLink");
  337. string soundPath = GetSParam("soundPath");
  338. if(!string.IsNullOrEmpty(soundPath))
  339. {
  340. #if UNITY_EDITOR
  341. _audioClip = UnityEditor.AssetDatabase.LoadAssetAtPath<AudioClip>(soundPath);
  342. #endif
  343. }
  344. }
  345. }
  346. }