| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.Security;
- namespace Flux
- {
- [FEvent("Skill/动作相关/技能吟唱")]
- public class FSkillSingEvent : FEvent
- {
- [HideInInspector]
- public string _animName;
- [HideInInspector]
- [Tooltip("What's the offset from where we play the animation?")]
- public int _startOffset;
- [SerializeField]
- private AudioClip _audioClip = null;
- public AudioClip AudioClip { get { return _audioClip; } }
- [HideInInspector]
- [SerializeField]
- private int _effectId;
- public int EffectId
- {
- get { return _effectId; }
- }
- [HideInInspector]
- [SerializeField]
- private GameObject _effectPrefab = null;
- public GameObject EffectPrefab
- {
- get { return _effectPrefab; }
- }
- [HideInInspector]
- [SerializeField]
- private string _effectLink;
- public string EffectLink
- {
- get { return _effectLink; }
- }
- private Animator _animator = null;
- public Animator animator
- {
- get { return _animator; }
- }
- private AudioSource _source;
- private ParticleSystem _particleSystem = null;
- private GameObject _effectGo = null;
- private float _previousTimeSinceTrigger = 0;
- private float _previousSpeed = 0;
-
- public FSkillSingEvent()
- {
- _eventType = SkillActionFrameEventType.FE_Skill_Sing;
- }
- protected override void OnSetDefaultValues()
- {
- base.OnSetDefaultValues();
- if (_casterTrans != null)
- {
- _animator = _casterTrans.gameObject.GetComponent<Animator>();
- }
- }
- protected override void OnInit()
- {
- if (_animator != null)
- _animator.enabled = false;
- }
- protected override void OnTrigger(float timeSinceTrigger)
- {
- if (Application.isPlaying)
- {
- _source = Owner.GetComponent<AudioSource>();
- if (_source == null)
- _source = Owner.gameObject.AddComponent<AudioSource>();
- _source.volume = 1;
- _source.loop = false;
- _source.clip = _audioClip;
- if (Sequence.IsPlaying)
- _source.Play();
- }
- else
- {
- #if UNITY_EDITOR
- UnityEditor.AudioUtility.PlayClip(_audioClip);
- #endif
- }
- if(_effectPrefab != null)
- {
- Vector3 pos = _casterTrans.position;
- Transform linkPoint = UnityEngineUtils.RecurisiveFindTransformChild(_casterTrans, _effectLink);
- if (linkPoint != null)
- {
- pos = linkPoint.position;
- }
- _effectGo = (GameObject)Instantiate(_effectPrefab);
- _effectGo.transform.SetParent(linkPoint);
- _effectGo.transform.localPosition = Vector3.zero;
- _effectGo.transform.localScale = Vector3.one;
- _effectGo.transform.localRotation = Quaternion.identity;
- _particleSystem = _effectGo.GetComponentInChildren<ParticleSystem>();
- if (_particleSystem != null)
- {
- ParticleSystem.MainModule mainModule = _particleSystem.main;
- mainModule.simulationSpeed = Sequence.Speed;
- }
- }
-
- if (_particleSystem != null && Sequence.IsPlaying && Sequence.IsPlayingForward)
- _particleSystem.Play(true);
-
- if(_animator!=null)
- {
- _animator.enabled = true;
- int id = GetId();
- #if UNITY_EDITOR
- if (_animator.enabled && (!_track.HasCache || Application.isPlaying))
- #else
- if( _animator.enabled )
- #endif
- {
- _animator.SetLayerWeight(0, 1);
- if (id == 0 || _track.Events[id - 1].End < Start)
- {
- _animator.Play(_animName,0);
- }
- if (timeSinceTrigger > 0)
- {
- // - 0.001f because if you pass the length of the animation
- // it seems that it will go over it and miss the condition
- _animator.Update(timeSinceTrigger - 0.001f);
- }
- else
- _animator.Update(0f);
- }
- }
- _previousTimeSinceTrigger = 0;
- _previousSpeed = Sequence.Speed;
- }
- protected override void OnPause()
- {
- if(Application.isPlaying)
- {
- if (_source)
- _source.Pause();
- }else
- {
- #if UNITY_EDITOR
- UnityEditor.AudioUtility.PauseClip(_audioClip);
- #endif
- }
-
- if (_particleSystem != null)
- _particleSystem.Pause();
- if(_animator!=null)
- _animator.enabled = false;
- }
- protected override void OnResume()
- {
- if(Application.isPlaying)
- {
- if (Sequence.IsPlaying)
- {
- if (_source)
- _source.Play();
- }
- }
- else
- {
- #if UNITY_EDITOR
- UnityEditor.AudioUtility.PlayClip(_audioClip);
- #endif
- }
-
- if (_particleSystem != null && Sequence.IsPlayingForward)
- _particleSystem.Play(true);
- if(_animator!=null)
- _animator.enabled = true;
- }
- protected override void OnFinish()
- {
- if(Application.isPlaying)
- {
- if (_source)
- {
- if (_source.clip == _audioClip && _source.isPlaying)
- {
- _source.Stop();
- _source.clip = null;
- }
- }
- }
- else
- {
- #if UNITY_EDITOR
- UnityEditor.AudioUtility.StopClip(_audioClip);
- #endif
- }
- if (_animator && (IsLastEvent || _track.GetEvent(GetId() + 1).Start != End))
- {
- _animator.SetLayerWeight(0, 0);
- _animator.enabled = false;
- }
- if (_particleSystem != null)
- _particleSystem.Stop(true);
- if(_effectGo!=null)
- {
- GameObject.DestroyImmediate(_effectGo);
- _effectGo = null;
- }
- }
- protected override void OnStop()
- {
- if (Application.isPlaying)
- {
- if (_source)
- {
- if (_source.clip == _audioClip && _source.isPlaying)
- {
- _source.Stop();
- _source.clip = null;
- }
- }
- }
- else
- {
- #if UNITY_EDITOR
- UnityEditor.AudioUtility.StopClip(_audioClip);
- #endif
- }
- int id = GetId();
- if (_animator && (id == 0 || _track.GetEvent(id - 1).End != Start))
- {
- _animator.Play("f_idle", 0);
- _animator.Update(0);
- }
-
- if (_particleSystem != null)
- {
- _particleSystem.Stop(true);
- _particleSystem.Clear(true);
- }
- if (_effectGo != null)
- {
- GameObject.DestroyImmediate(_effectGo);
- _effectGo = null;
- }
- }
- protected override void OnUpdateEvent(float timeSinceTrigger)
- {
- float delta = timeSinceTrigger - _previousTimeSinceTrigger;
- _previousTimeSinceTrigger = timeSinceTrigger;
- if (_animator!=null)
- {
- if (!_animator.enabled)
- _animator.enabled = true;
- if(delta > 0)
- {
- _animator.SetFloat("moveSpeed", 1);
- _animator.Update(delta);
- }
- else if(delta < 0)
- {
- _animator.SetFloat("moveSpeed", -1);
- _animator.Update(-delta);
- }
- }
- if(_source!=null && _source.clip!=null)
- {
- _source.PlayScheduled(delta);
- }
- if (_particleSystem != null)
- {
- if (!Sequence.IsPlaying || !Sequence.IsPlayingForward)
- {
- _previousSpeed = 1;
- ParticleSystem.MainModule mainModule = _particleSystem.main;
- mainModule.simulationSpeed = _previousSpeed;
- if (Sequence.IsPlayingForward && delta > 0)
- {
- _particleSystem.Simulate(delta, true, false);
- }
- }
- else if (_previousSpeed != Sequence.Speed)
- {
- _previousSpeed = Sequence.Speed;
- ParticleSystem.MainModule mainModule = _particleSystem.main;
- mainModule.simulationSpeed = Mathf.Abs(_previousSpeed);
- }
- }
- }
- public override int GetMaxLength()
- {
- return base.GetMaxLength();
- }
- public override string Text
- {
- get { return "吟唱"; }
- set { }
- }
- public override SecurityElement SaveToXml()
- {
- SecurityElement node = base.SaveToXml();
- SecurityElement paramNode = WriteParamNode("animName", _animName, "string");
- if (paramNode != null)
- node.AddChild(paramNode);
- paramNode = WriteParamNode("hiteffect", _effectId.ToString(), "int");
- node.AddChild(paramNode);
- if (_effectPrefab != null)
- {
- #if UNITY_EDITOR
- paramNode = WriteParamNode("effectPrefab", UnityEditor.AssetDatabase.GetAssetPath(_effectPrefab), "string");
- node.AddChild(paramNode);
- #endif
- }
- paramNode = WriteParamNode("effectLink", _effectLink, "string");
- if (paramNode != null)
- node.AddChild(paramNode);
- if (_audioClip != null)
- {
- #if UNITY_EDITOR
- string audioPath = UnityEditor.AssetDatabase.GetAssetPath(_audioClip);
- string relativeName = FileUtils.RemoveParent(Constants.FightAudioPath, audioPath);
- paramNode = WriteParamNode("sound", relativeName, "string");
- node.AddChild(paramNode);
- paramNode = WriteParamNode("soundPath", audioPath, "string");
- node.AddChild(paramNode);
- #endif
- }
- return node;
- }
- public override void LoadFromXml(SecurityElement eventNode)
- {
- base.LoadFromXml(eventNode);
- _animName = GetSParam("animName");
- _effectId = GetNParam("hiteffect");
-
- string effectPrefab = GetSParam("effectPrefab");
-
- if(!string.IsNullOrEmpty(effectPrefab))
- {
- #if UNITY_EDITOR
- _effectPrefab = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(effectPrefab);
- #endif
- }
- _effectLink = GetSParam("effectLink");
- string soundPath = GetSParam("soundPath");
- if(!string.IsNullOrEmpty(soundPath))
- {
- #if UNITY_EDITOR
- _audioClip = UnityEditor.AssetDatabase.LoadAssetAtPath<AudioClip>(soundPath);
- #endif
- }
- }
- }
- }
|