252 lines
5.8 KiB
C#
252 lines
5.8 KiB
C#
using BrewMonster.Scripts;
|
|
using UnityEngine;
|
|
|
|
namespace BrewMonster.PerfectWorld.Scripts.Vfx
|
|
{
|
|
public enum GFX_STATE
|
|
{
|
|
ST_STOP = 0,
|
|
ST_PLAY = 1,
|
|
ST_PAUSE = 2,
|
|
ST_EDITMODE = 3
|
|
}
|
|
public class BaseVfxObject : MonoBehaviour
|
|
{
|
|
[SerializeField] private ParticleSystem _vfx;
|
|
private string _vfxPath;
|
|
private Vector3 _originalScale = Vector3.one;
|
|
private Vector3 _offset = Vector3.zero;
|
|
private GFX_STATE m_iState;
|
|
|
|
private void Awake()
|
|
{
|
|
if (_vfx == null)
|
|
{
|
|
_vfx = GetComponent<ParticleSystem>();
|
|
}
|
|
|
|
if (_vfx != null)
|
|
{
|
|
_originalScale = _vfx.transform.localScale;
|
|
}
|
|
}
|
|
private void OnDestroy()
|
|
{
|
|
if(!string.IsNullOrEmpty(_vfxPath))
|
|
{
|
|
AddressableManager.Instance.UnloadAsset(_vfxPath);
|
|
}
|
|
}
|
|
|
|
public void Init(string vfxPath)
|
|
{
|
|
_vfxPath = vfxPath;
|
|
}
|
|
|
|
#region EC
|
|
|
|
public GFX_STATE GetState() { return m_iState; }
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Set VFX scale
|
|
/// </summary>
|
|
public void SetScale(float scale)
|
|
{
|
|
SetScale(new Vector3(scale, scale, scale));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set VFX scale with vector
|
|
/// </summary>
|
|
public void SetScale(Vector3 scale)
|
|
{
|
|
if (_vfx != null)
|
|
{
|
|
_vfx.transform.localScale = Vector3.Scale(_originalScale, scale);
|
|
|
|
// 调整粒子大小 / Adjust particle size
|
|
var main = _vfx.main;
|
|
main.startSizeMultiplier *= scale.magnitude / Mathf.Sqrt(3);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set VFX offset
|
|
/// </summary>
|
|
public void SetOffset(Vector3 offset)
|
|
{
|
|
_offset = offset;
|
|
if (_vfx != null)
|
|
{
|
|
_vfx.transform.localPosition = offset;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set VFX world position
|
|
/// </summary>
|
|
public void SetPosition(Vector3 position)
|
|
{
|
|
if (_vfx != null)
|
|
{
|
|
_vfx.transform.position = position;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set VFX rotation
|
|
/// </summary>
|
|
public void SetRotation(Quaternion rotation)
|
|
{
|
|
if (_vfx != null)
|
|
{
|
|
_vfx.transform.rotation = rotation;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set VFX rotation with euler angles
|
|
/// </summary>
|
|
public void SetRotation(Vector3 eulerAngles)
|
|
{
|
|
SetRotation(Quaternion.Euler(eulerAngles));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set VFX color
|
|
/// </summary>
|
|
public void SetColor(Color color)
|
|
{
|
|
if (_vfx != null)
|
|
{
|
|
var main = _vfx.main;
|
|
main.startColor = color;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set VFX duration
|
|
/// </summary>
|
|
public void SetDuration(float duration)
|
|
{
|
|
if (_vfx != null)
|
|
{
|
|
var main = _vfx.main;
|
|
main.duration = duration;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set VFX playback speed
|
|
/// </summary>
|
|
public void SetSpeed(float speed)
|
|
{
|
|
if (_vfx != null)
|
|
{
|
|
var main = _vfx.main;
|
|
main.simulationSpeed = speed;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Play VFX
|
|
/// </summary>
|
|
public void Play()
|
|
{
|
|
Stop();
|
|
if (_vfx != null)
|
|
{
|
|
m_iState = GFX_STATE.ST_PLAY;
|
|
_vfx.Play();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Play VFX with children
|
|
/// </summary>
|
|
public void PlayWithChildren()
|
|
{
|
|
if (_vfx != null)
|
|
{
|
|
m_iState = GFX_STATE.ST_PLAY;
|
|
_vfx.Play(true);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Stop VFX
|
|
/// </summary>
|
|
public void Stop()
|
|
{
|
|
if (_vfx != null)
|
|
{
|
|
m_iState = GFX_STATE.ST_STOP;
|
|
_vfx.Stop();
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Pause VFX
|
|
/// </summary>
|
|
public void Pause()
|
|
{
|
|
if (_vfx != null)
|
|
{
|
|
m_iState = GFX_STATE.ST_PAUSE;
|
|
_vfx.Pause();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clear all VFX particles
|
|
/// </summary>
|
|
public void Clear()
|
|
{
|
|
if (_vfx != null)
|
|
{
|
|
_vfx.Clear();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 重新开始播放 / Restart VFX
|
|
/// </summary>
|
|
public void Restart()
|
|
{
|
|
if (_vfx != null)
|
|
{
|
|
_vfx.Stop();
|
|
_vfx.Clear();
|
|
_vfx.Play();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查特效是否正在播放 / Check if VFX is playing
|
|
/// </summary>
|
|
public bool IsPlaying()
|
|
{
|
|
return _vfx != null && _vfx.isPlaying;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查特效是否已结束 / Check if VFX is stopped
|
|
/// </summary>
|
|
public bool IsStopped()
|
|
{
|
|
return _vfx != null && _vfx.isStopped;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取粒子系统 / Get ParticleSystem component
|
|
/// </summary>
|
|
public ParticleSystem GetParticleSystem()
|
|
{
|
|
return _vfx;
|
|
}
|
|
}
|
|
}
|