Skip to content

Instantly share code, notes, and snippets.

@tklee1975
Created October 18, 2019 16:22
Show Gist options
  • Select an option

  • Save tklee1975/2a2abd26d22c7816f7c9b13704618436 to your computer and use it in GitHub Desktop.

Select an option

Save tklee1975/2a2abd26d22c7816f7c9b13704618436 to your computer and use it in GitHub Desktop.
A simple unity script used to breakdown the Particle VFX so that users can check each particle VFX one by one.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ParticleAnalyzer : MonoBehaviour
{
[SerializeField] protected ParticleSystem particle;
protected ParticleSystemRenderer[] allParticles;
// Start is called before the first frame update
void Start()
{
}
void OnGUI()
{
//Debug.Log("Looping Particle: p=" + IsLoopParticle(particle));
OnGUIMainParticlePanel(particle);
allParticles = ListParticle(particle);
if (allParticles != null)
{
foreach (ParticleSystemRenderer p in allParticles)
{
GUILayout.BeginHorizontal();
bool newFlag = GUILayout.Toggle(IsParticleVisible(p), p.name);
ChangeParticle(p, newFlag);
GUILayout.EndHorizontal();
}
}
}
void OnGUIMainParticlePanel(ParticleSystem p)
{
//
GUILayout.BeginHorizontal();
OnGUIMainParticlePlayStop(p);
OnGUIShowHideAll(p);
GUILayout.EndHorizontal();
}
void OnGUIShowHideAll(ParticleSystem p)
{
if (GUILayout.Button("Show All"))
{
SetParticleRendererVisible(true);
}
if (GUILayout.Button("Hide All"))
{
SetParticleRendererVisible(false);
}
}
void OnGUIMainParticlePlayStop(ParticleSystem p)
{
if (IsLoopParticle(particle) == false)
{
bool isPlaying = IsPlayingParticle(particle);
string btnName = isPlaying ? "Stop" : "Play";
if (GUILayout.Button(btnName))
{
if (isPlaying)
{
particle.Stop();
}
else
{
particle.Play();
}
}
}
}
public ParticleSystemRenderer[] ListParticle(ParticleSystem mainParticle)
{
if (mainParticle == null)
{
return new ParticleSystemRenderer[0];
}
return mainParticle.GetComponentsInChildren<ParticleSystemRenderer>(true);
}
public void ChangeParticle(ParticleSystemRenderer p, bool newFlag)
{
if (newFlag == IsParticleVisible(p))
{
return;
}
SetParticleVisible(p, newFlag);
}
void SetParticleRendererVisible(bool flag)
{
foreach(ParticleSystemRenderer r in allParticles)
{
SetParticleVisible(r, flag);
}
}
public void SetParticleVisible(ParticleSystemRenderer p, bool flag)
{
if (p == null)
{
return;
}
p.enabled = flag;
}
public bool IsPlayingParticle(ParticleSystem p)
{
return p.isPlaying;
}
public bool IsLoopParticle(ParticleSystem p)
{
return p.main.loop;
}
public void PlayParticle(ParticleSystem p)
{
p.Play();
}
public bool IsParticleVisible(ParticleSystemRenderer p)
{
return p.enabled;
}
// Update is called once per frame
void Update()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment