Skip to content

Instantly share code, notes, and snippets.

@codorizzi
Created June 1, 2021 05:56
Show Gist options
  • Save codorizzi/c5a3f28ae0bed91c791fcb1f23e6315d to your computer and use it in GitHub Desktop.
Save codorizzi/c5a3f28ae0bed91c791fcb1f23e6315d to your computer and use it in GitHub Desktop.
Controller for Particle System Circle
using Sirenix.OdinInspector;
using UnityEngine;
public class ParticleSystemCircle : MonoBehaviour {
private ParticleSystem _particleSystem;
private ParticleSystem.MainModule _main;
private ParticleSystem.ShapeModule _shape;
private ParticleSystem.EmissionModule _emission;
private ParticleSystem.VelocityOverLifetimeModule _velocity;
private ParticleSystem.TrailModule _trail;
private ParticleSystemRenderer _renderer;
[OnValueChanged("UpdateCircle")] public float radius = 1f;
[OnValueChanged("UpdateCircle")] public float thickness = 0.25f;
[OnValueChanged("UpdateMaterial")] public Material material;
[OnValueChanged("UpdateCircle")] public Vector3 orbitSpeed = new Vector3(0, 0, 0.1f);
[OnValueChanged("UpdateCircle")] public bool isSolid = false;
private void Awake() {
_particleSystem = GetComponent<ParticleSystem>();
_main = _particleSystem.main;
_shape = _particleSystem.shape;
_emission = _particleSystem.emission;
_velocity = _particleSystem.velocityOverLifetime;
_renderer = GetComponent<ParticleSystemRenderer>();
}
private void Start() {
UpdateMaterial();
UpdateCircle();
}
void UpdateCircle() {
if(Application.isEditor) {
_particleSystem = GetComponent<ParticleSystem>();
_main = _particleSystem.main;
_shape = _particleSystem.shape;
_emission = _particleSystem.emission;
_velocity = _particleSystem.velocityOverLifetime;
_trail = _particleSystem.trails;
}
SetMain();
SetEmission();
SetVelocity();
SetShape();
SetTrail();
_particleSystem.Stop();
_particleSystem.Play();
}
void UpdateMaterial() {
if(Application.isEditor)
_renderer = GetComponent<ParticleSystemRenderer>();
_renderer.material = material;
_renderer.trailMaterial = material;
}
void SetMain() {
_main.startLifetime = Mathf.Infinity;
_main.prewarm = true;
_main.loop = true;
float perimeter = 2 * Mathf.PI * radius;
_main.maxParticles = Mathf.CeilToInt(perimeter);
_main.startSize = thickness;
}
void SetEmission() {
_emission.rateOverTime = _main.maxParticles;
}
void SetVelocity() {
_velocity.orbitalX = orbitSpeed.x;
_velocity.orbitalY = orbitSpeed.y;
_velocity.orbitalZ = orbitSpeed.z;
}
void SetShape() {
_shape.arc = 360;
_shape.arcMode = ParticleSystemShapeMultiModeValue.BurstSpread;
_shape.arcSpread = 0;
_shape.radius = radius;
}
void SetTrail() {
_trail.enabled = isSolid;
if (isSolid)
_renderer.trailMaterial = material;
}
}
@codorizzi
Copy link
Author

Uses Odin for some editor functionality, which can be stripped if you don't have it in your project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment