FSkillCastingEvent.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Security;
  4. using UnityEngine;
  5. namespace Flux
  6. {
  7. [FEvent("Skill/动作相关/技能引导")]
  8. public class FSkillCastingEvent : 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 GameObject _effectPrefab = null;
  21. public GameObject EffectPrefab
  22. {
  23. get { return _effectPrefab; }
  24. }
  25. [HideInInspector]
  26. [SerializeField]
  27. private string _effectLink;
  28. public string EffectLink
  29. {
  30. get { return _effectLink; }
  31. }
  32. [HideInInspector]
  33. [SerializeField]
  34. private int _effectId;
  35. public int EffectId
  36. {
  37. get { return _effectId; }
  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 FSkillCastingEvent()
  50. {
  51. _eventType = SkillActionFrameEventType.FE_Repeat_Caster;
  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. _animator.Update(timeSinceTrigger - 0.001f);
  125. }
  126. else
  127. _animator.Update(0f);
  128. }
  129. }
  130. _previousTimeSinceTrigger = 0;
  131. _previousSpeed = Sequence.Speed;
  132. }
  133. protected override void OnPause()
  134. {
  135. if(Application.isPlaying)
  136. {
  137. if (_source)
  138. _source.Pause();
  139. }
  140. else
  141. {
  142. #if UNITY_EDITOR
  143. UnityEditor.AudioUtility.PauseClip(_audioClip);
  144. #endif
  145. }
  146. if (_particleSystem != null)
  147. _particleSystem.Pause();
  148. if (_animator != null)
  149. _animator.enabled = false;
  150. }
  151. protected override void OnResume()
  152. {
  153. if(Application.isPlaying)
  154. {
  155. if (Sequence.IsPlaying)
  156. {
  157. if (_source)
  158. _source.Play();
  159. }
  160. }
  161. else
  162. {
  163. #if UNITY_EDITOR
  164. UnityEditor.AudioUtility.PlayClip(_audioClip);
  165. #endif
  166. }
  167. if (_particleSystem != null && Sequence.IsPlayingForward)
  168. _particleSystem.Play(true);
  169. if (_animator != null)
  170. _animator.enabled = true;
  171. }
  172. protected override void OnFinish()
  173. {
  174. if(Application.isPlaying)
  175. {
  176. if (_source)
  177. {
  178. if (_source.clip == _audioClip && _source.isPlaying)
  179. {
  180. _source.Stop();
  181. _source.clip = null;
  182. }
  183. }
  184. }
  185. else
  186. {
  187. #if UNITY_EDITOR
  188. UnityEditor.AudioUtility.StopClip(_audioClip);
  189. #endif
  190. }
  191. if (_animator && (IsLastEvent || _track.GetEvent(GetId() + 1).Start != End))
  192. {
  193. _animator.SetLayerWeight(0, 0);
  194. _animator.enabled = false;
  195. }
  196. if (_particleSystem != null)
  197. _particleSystem.Stop(true);
  198. if (_effectGo != null)
  199. {
  200. GameObject.DestroyImmediate(_effectGo);
  201. _effectGo = null;
  202. }
  203. }
  204. protected override void OnStop()
  205. {
  206. if(Application.isPlaying)
  207. {
  208. if (_source)
  209. {
  210. if (_source.clip == _audioClip && _source.isPlaying)
  211. {
  212. _source.Stop();
  213. _source.clip = null;
  214. }
  215. }
  216. }
  217. else
  218. {
  219. #if UNITY_EDITOR
  220. UnityEditor.AudioUtility.StopClip(_audioClip);
  221. #endif
  222. }
  223. int id = GetId();
  224. if (_animator && (id == 0 || _track.GetEvent(id - 1).End != Start))
  225. {
  226. _animator.Play("f_idle", 0);
  227. _animator.Update(0);
  228. }
  229. if (_particleSystem != null)
  230. {
  231. _particleSystem.Stop(true);
  232. _particleSystem.Clear(true);
  233. }
  234. if (_effectGo != null)
  235. {
  236. GameObject.DestroyImmediate(_effectGo);
  237. _effectGo = null;
  238. }
  239. }
  240. protected override void OnUpdateEvent(float timeSinceTrigger)
  241. {
  242. float delta = timeSinceTrigger - _previousTimeSinceTrigger;
  243. _previousTimeSinceTrigger = timeSinceTrigger;
  244. if (_animator != null)
  245. {
  246. if (!_animator.enabled)
  247. _animator.enabled = true;
  248. if(delta > 0)
  249. {
  250. _animator.SetFloat("moveSpeed", 1);
  251. _animator.Update(delta);
  252. }
  253. else if(delta < 0)
  254. {
  255. _animator.SetFloat("moveSpeed", -1);
  256. _animator.Update(-delta);
  257. }
  258. }
  259. if (_particleSystem != null)
  260. {
  261. if (!Sequence.IsPlaying || !Sequence.IsPlayingForward)
  262. {
  263. _previousSpeed = 1;
  264. ParticleSystem.MainModule mainModule = _particleSystem.main;
  265. mainModule.simulationSpeed = _previousSpeed;
  266. if (Sequence.IsPlayingForward && delta > 0)
  267. {
  268. _particleSystem.Simulate(delta, true, false);
  269. }
  270. }
  271. else if (_previousSpeed != Sequence.Speed)
  272. {
  273. _previousSpeed = Sequence.Speed;
  274. ParticleSystem.MainModule mainModule = _particleSystem.main;
  275. mainModule.simulationSpeed = Mathf.Abs(_previousSpeed);
  276. }
  277. }
  278. }
  279. public override int GetMaxLength()
  280. {
  281. return base.GetMaxLength();
  282. }
  283. public override string Text
  284. {
  285. get { return "引导动作"; }
  286. set { }
  287. }
  288. public override SecurityElement SaveToXml()
  289. {
  290. SecurityElement node = base.SaveToXml();
  291. SecurityElement paramNode = WriteParamNode("animName", _animName, "string");
  292. if (paramNode != null)
  293. node.AddChild(paramNode);
  294. paramNode = WriteParamNode("hiteffect", _effectId.ToString(), "int");
  295. node.AddChild(paramNode);
  296. if(_effectPrefab!=null)
  297. {
  298. #if UNITY_EDITOR
  299. paramNode = WriteParamNode("effectPrefab", UnityEditor.AssetDatabase.GetAssetPath(_effectPrefab), "string");
  300. node.AddChild(paramNode);
  301. #endif
  302. }
  303. paramNode = WriteParamNode("effectLink", _effectLink, "string");
  304. if (paramNode != null)
  305. node.AddChild(paramNode);
  306. if (_audioClip != null)
  307. {
  308. #if UNITY_EDITOR
  309. string audioPath = UnityEditor.AssetDatabase.GetAssetPath(_audioClip);
  310. string relativeName = FileUtils.RemoveParent(Constants.FightAudioPath, audioPath);
  311. paramNode = WriteParamNode("sound", relativeName, "string");
  312. node.AddChild(paramNode);
  313. paramNode = WriteParamNode("soundPath", audioPath, "string");
  314. node.AddChild(paramNode);
  315. #endif
  316. }
  317. return node;
  318. }
  319. public override void LoadFromXml(SecurityElement eventNode)
  320. {
  321. base.LoadFromXml(eventNode);
  322. _animName = GetSParam("animName");
  323. _effectId = GetNParam("hiteffect");
  324. string effectPrefab = GetSParam("effectPrefab");
  325. if (!string.IsNullOrEmpty(effectPrefab))
  326. {
  327. #if UNITY_EDITOR
  328. _effectPrefab = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(effectPrefab);
  329. #endif
  330. }
  331. _effectLink = GetSParam("effectLink");
  332. string soundPath = GetSParam("soundPath");
  333. if (!string.IsNullOrEmpty(soundPath))
  334. {
  335. #if UNITY_EDITOR
  336. _audioClip = UnityEditor.AssetDatabase.LoadAssetAtPath<AudioClip>(soundPath);
  337. #endif
  338. }
  339. }
  340. }
  341. }