271 lines
5.9 KiB
C#
271 lines
5.9 KiB
C#
![]() |
// ***********************************************************************
|
|||
|
// Assembly : Unity
|
|||
|
// Author : Kimch
|
|||
|
// Created : 2019-06-17
|
|||
|
// Description :
|
|||
|
// Last Modified By :
|
|||
|
// Last Modified On :
|
|||
|
// ***********************************************************************
|
|||
|
// <copyright file= "KFx" company="DefaultCompany"></copyright>
|
|||
|
// <summary></summary>
|
|||
|
// ***********************************************************************
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
namespace G
|
|||
|
{
|
|||
|
public class KFx : MonoBehaviour
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
public enum FreeType
|
|||
|
{
|
|||
|
Nothing,
|
|||
|
Disable,
|
|||
|
Despawn,
|
|||
|
Destory,
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// The fx level require level
|
|||
|
/// </summary>
|
|||
|
public int fxLevel;
|
|||
|
/// <summary>
|
|||
|
/// The timer destroy duration value <= 0 don't destory , value > 0 use time destory
|
|||
|
/// </summary>
|
|||
|
[Tooltip("< 0 don't destory , value > 0 use time destory value == 0 use partical.main.duration")]
|
|||
|
public float fxDuration = 0f;
|
|||
|
/// <summary>
|
|||
|
/// The fx loop duration <= 0 don't loop , value > 0 manual loop
|
|||
|
/// </summary>
|
|||
|
[Tooltip("<= 0 don't loop , value > 0 manual loop")]
|
|||
|
public float fxLoopDuration = -1f;
|
|||
|
/// <summary>
|
|||
|
/// The fx loop duration range
|
|||
|
/// </summary>
|
|||
|
public float fxLoopDurationFilter = 0f;
|
|||
|
/// <summary>
|
|||
|
/// The fx play delay start play delay
|
|||
|
/// </summary>
|
|||
|
public float fxFirstDelay = 0.0001f;
|
|||
|
/// <summary>
|
|||
|
/// The free type
|
|||
|
/// </summary>
|
|||
|
public FreeType freeType = FreeType.Despawn;
|
|||
|
/// <summary>
|
|||
|
/// The particle fx prefab
|
|||
|
/// </summary>
|
|||
|
public GameObject fxPrefab;
|
|||
|
/// <summary>
|
|||
|
/// The sound fx clips
|
|||
|
/// </summary>
|
|||
|
public AudioClip[] soundFxClips;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// The _sound fx index
|
|||
|
/// </summary>
|
|||
|
private int _soundFxIndex = -1;
|
|||
|
/// <summary>
|
|||
|
/// The _destory timer
|
|||
|
/// </summary>
|
|||
|
private float _destoryTimer;
|
|||
|
/// <summary>
|
|||
|
/// The _FX loop timer
|
|||
|
/// </summary>
|
|||
|
private float _fxLoopTimer;
|
|||
|
/// <summary>
|
|||
|
/// The _audio source
|
|||
|
/// </summary>
|
|||
|
private AudioSource _audioSource;
|
|||
|
/// <summary>
|
|||
|
/// The _particle fx
|
|||
|
/// </summary>
|
|||
|
private ParticleSystem _particleFx;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
public int soundFxIndex
|
|||
|
{
|
|||
|
set
|
|||
|
{
|
|||
|
_soundFxIndex = Mathf.Clamp(value, -1, this.soundFxClips.Length);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Gets the sound fx clip.
|
|||
|
/// </summary>
|
|||
|
/// <value>
|
|||
|
/// The sound fx clip.
|
|||
|
/// </value>
|
|||
|
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];
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Despawns this instance.
|
|||
|
/// </summary>
|
|||
|
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
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
public ParticleSystem particle
|
|||
|
{
|
|||
|
get { return _particleFx; }
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Plays the particle.
|
|||
|
/// </summary>
|
|||
|
private void PlayParticle()
|
|||
|
{
|
|||
|
if (_particleFx)
|
|||
|
{
|
|||
|
_particleFx.Clear();
|
|||
|
_particleFx.Play();
|
|||
|
}
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// Plays the sound.
|
|||
|
/// </summary>
|
|||
|
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<AudioSource>();
|
|||
|
}
|
|||
|
|
|||
|
if (this.fxLevel <= 0)
|
|||
|
{
|
|||
|
if (this.fxPrefab)
|
|||
|
{
|
|||
|
_particleFx = Instantiate(this.fxPrefab, this.transform).GetComponentInChildren<ParticleSystem>();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_particleFx = GetComponentInChildren<ParticleSystem>();
|
|||
|
}
|
|||
|
|
|||
|
//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();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 调转特效方向
|
|||
|
/// </summary>
|
|||
|
/// <param name="angle"></param>
|
|||
|
public void RotatePartical(float angle)
|
|||
|
{
|
|||
|
ParticleSystem[] particles = this.GetComponentsInChildren<ParticleSystem>();
|
|||
|
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
|
|||
|
}
|
|||
|
}
|