Files
Main/Assets/SkillEditor/Script/VFX/EditorVFXPlayer.cs

77 lines
2.1 KiB
C#
Raw Normal View History

2025-01-25 04:38:09 +08:00
using System.Collections.Generic;
using UnityEngine;
namespace Thousandto.SkillEditor.Support
{
public class EditorVFXPlayer
{
private const string VFXRootName = "[VFXRoot]";
private Transform _root = null;
private List<EditorVFXScript> _vfxs = new List<EditorVFXScript>();
private static EditorVFXPlayer _instance = null;
public static EditorVFXPlayer Instance
{
get
{
if (_instance == null)
{
_instance = new EditorVFXPlayer();
var rootGo = GameObject.Find(VFXRootName);
if (rootGo == null)
{
rootGo = new GameObject(VFXRootName);
GameObject.DontDestroyOnLoad(rootGo);
_instance._root = rootGo.transform;
}
}
return _instance;
}
}
public EditorVFXScript PlayVFX(string path, Transform root = null)
{
if (root == null)
{
root = _root;
}
var vfx = EditorVFXLoader.Load(path);
if (vfx != null)
{
vfx.transform.parent = root;
vfx.transform.localPosition = Vector3.zero;
vfx.transform.localRotation = Quaternion.identity;
vfx.transform.localScale = Vector3.one;
vfx.Play();
_vfxs.Add(vfx);
}
return vfx;
}
public void StopAll()
{
for (int i = _vfxs.Count - 1; i >= 0; --i)
{
var vfx = _vfxs[i];
vfx.Stop();
}
}
public void Update()
{
for (int i = _vfxs.Count - 1; i >= 0; --i)
{
var vfx = _vfxs[i];
vfx.UpdateVFX();
if (vfx.IsEnd)
{
_vfxs.RemoveAt(i);
GameObject.DestroyImmediate(vfx.gameObject);
}
}
}
}
}