// *********************************************************************** // Assembly : Unity // Author : Kimch // Created : 2019-06-17 // Description : // Last Modified By : // Last Modified On : // *********************************************************************** // // // *********************************************************************** using UnityEngine; namespace G { public class KFx : MonoBehaviour { /// /// /// public enum FreeType { Nothing, Disable, Despawn, Destory, } /// /// The fx level require level /// public int fxLevel; /// /// The timer destroy duration value <= 0 don't destory , value > 0 use time destory /// [Tooltip("< 0 don't destory , value > 0 use time destory value == 0 use partical.main.duration")] public float fxDuration = 0f; /// /// The fx loop duration <= 0 don't loop , value > 0 manual loop /// [Tooltip("<= 0 don't loop , value > 0 manual loop")] public float fxLoopDuration = -1f; /// /// The fx loop duration range /// public float fxLoopDurationFilter = 0f; /// /// The fx play delay start play delay /// public float fxFirstDelay = 0.0001f; /// /// The free type /// public FreeType freeType = FreeType.Despawn; /// /// The particle fx prefab /// public GameObject fxPrefab; /// /// The sound fx clips /// public AudioClip[] soundFxClips; /// /// The _sound fx index /// private int _soundFxIndex = -1; /// /// The _destory timer /// private float _destoryTimer; /// /// The _FX loop timer /// private float _fxLoopTimer; /// /// The _audio source /// private AudioSource _audioSource; /// /// The _particle fx /// private ParticleSystem _particleFx; /// /// /// public int soundFxIndex { set { _soundFxIndex = Mathf.Clamp(value, -1, this.soundFxClips.Length); } } /// /// Gets the sound fx clip. /// /// /// The sound fx clip. /// private AudioClip soundFxClip { get { if (_soundFxIndex < 0) { int length = this.soundFxClips.Length; return this.soundFxClips[length > 1 ? Random.Range(0, length) : 0]; } else { return this.soundFxClips[_soundFxIndex]; } } } /// /// Despawns this instance. /// private void Despawn() { if (this.freeType == FreeType.Despawn) { //SpawnPool.Despawn(this.gameObject); } else if (this.freeType == FreeType.Disable) { this.gameObject.SetActive(false); } else if (this.freeType == FreeType.Destory) { Destroy(this.gameObject); } else { } } /// /// /// public ParticleSystem particle { get { return _particleFx; } } /// /// Plays the particle. /// private void PlayParticle() { if (_particleFx) { _particleFx.Clear(); _particleFx.Play(); } } /// /// Plays the sound. /// private void PlaySound() { if (/*KSoundManager.AudioEnable &&*/ this.soundFxClips.Length > 0) { if (_audioSource) { if (_audioSource.loop) { _audioSource.clip = this.soundFxClip; _audioSource.Play(); } else { _audioSource.PlayOneShot(this.soundFxClip); } } else { //SoundManager.Instance.PlaySoundFromPool(this.soundFxClip); Debug.Log("Play Sound"); //KSoundManager.PlayAudio(this.soundFxClip); } } } #region MonoBehaviour private void Start() { if (this.soundFxClips.Length > 0) { _audioSource = this.GetComponent(); } if (this.fxLevel <= 0) { if (this.fxPrefab) { _particleFx = Instantiate(this.fxPrefab, this.transform).GetComponentInChildren(); } else { _particleFx = GetComponentInChildren(); } //if (_particleFx) //{ // this.fxDuration = _particleFx.main.duration; // _particleFx.gameObject.layer = this.gameObject.layer; //} } } private void OnEnable() { _destoryTimer = this.fxDuration; _fxLoopTimer = this.fxFirstDelay; _soundFxIndex = -1; } public void Reset() { _destoryTimer = this.fxDuration; _fxLoopTimer = this.fxFirstDelay; _soundFxIndex = -1; } // Update is called once per frame private void Update() { if (this.fxDuration > 0f) { _destoryTimer -= Time.deltaTime; if (_destoryTimer < 0f) { this.Despawn(); return; } } if (_fxLoopTimer >= 0f) { _fxLoopTimer -= Time.deltaTime; if (_fxLoopTimer < 0f) { _fxLoopTimer = this.fxLoopDuration > 0f ? (this.fxLoopDuration + (this.fxLoopDurationFilter > 0 ? Random.Range(0f, this.fxLoopDurationFilter) : 0f)) : -1f; this.PlayParticle(); this.PlaySound(); } } } /// /// 调转特效方向 /// /// public void RotatePartical(float angle) { ParticleSystem[] particles = this.GetComponentsInChildren(); for (int i = 0; i < particles.Length; i++) { #pragma warning disable CS0618 // “ParticleSystem.startRotation”已过时:“startRotation property is deprecated. Use main.startRotation or main.startRotationMultiplier instead.” particles[i].startRotation = angle; #pragma warning restore CS0618 // “ParticleSystem.startRotation”已过时:“startRotation property is deprecated. Use main.startRotation or main.startRotationMultiplier instead.” } } #endregion } }