Skip to content

Instantly share code, notes, and snippets.

@nothke
Last active July 25, 2016 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nothke/c3986e6ad1ba5cfbc6d0f293c104dd71 to your computer and use it in GitHub Desktop.
Save nothke/c3986e6ad1ba5cfbc6d0f293c104dd71 to your computer and use it in GitHub Desktop.
//
// ParticleSystem extensions for changes introduced in Unity 5.3
// by Nothke, unlicensed
//
// Each module in ParticleSystem in Unity 5.3 is now a struct,
// which makes setting some simple stuff more cumbersome
// These extensions provide shortcuts to simplify those tasks
//
// For example: setting constant emission rate now takes 3 lines at minimum,
// rather than a single line like before.
//
using UnityEngine;
using System.Collections;
public static class ParticleSystemExtensions
{
// Emission
/// <summary>
/// Sets constant emission rate. Replaces particleSystem.emissionRate
/// </summary>
/// <param name="rate">Constant emission rate</param>
public static void SetEmissionRate(this ParticleSystem particleSystem, float rate)
{
ParticleSystem.EmissionModule em = particleSystem.emission;
ParticleSystem.MinMaxCurve rCurve = new ParticleSystem.MinMaxCurve(rate);
em.rate = rCurve;
}
/// <summary>
/// Sets emission rate between two random values
/// </summary>
/// <param name="rateRandomMin">Minimum random value</param>
/// <param name="rateRandomMax">Maximum random value</param>
public static void SetEmissionRate(this ParticleSystem particleSystem, float rateRandomMin, float rateRandomMax)
{
ParticleSystem.EmissionModule em = particleSystem.emission;
ParticleSystem.MinMaxCurve rCurve = new ParticleSystem.MinMaxCurve(rateRandomMin, rateRandomMax);
em.rate = rCurve;
}
/// <summary>
/// Sets emission rate to a curve
/// </summary>
/// <param name="curve">Curve</param>
/// <param name="scale">Scalar for the curve</param>
public static void SetEmissionRate(this ParticleSystem particleSystem, AnimationCurve curve, float scale = 1)
{
ParticleSystem.EmissionModule em = particleSystem.emission;
ParticleSystem.MinMaxCurve rCurve = new ParticleSystem.MinMaxCurve(scale, curve);
em.rate = rCurve;
}
/// <summary>
/// Enables or disables emission. Replaces particleSystem.enableEmission
/// </summary>
/// <param name="particleSystem"></param>
/// <param name="enable"></param>
public static void EnableEmission(this ParticleSystem particleSystem, bool enable)
{
ParticleSystem.EmissionModule em = particleSystem.emission;
em.enabled = enable;
}
// Shape
/// <summary>
/// Sets particle casting shape angle. Only works if the shape is a cone
/// </summary>
public static void SetShapeAngle(this ParticleSystem particleSystem, float angle)
{
ParticleSystem.ShapeModule shape = particleSystem.shape;
shape.angle = angle;
}
/// <summary>
/// Sets particle casting shape's radius. Only works if the shape is a sphere, hemisphere or a circle
/// </summary>
public static void SetShapeRadius(this ParticleSystem particleSystem, float radius)
{
ParticleSystem.ShapeModule shape = particleSystem.shape;
shape.radius = radius;
}
/// <summary>
/// Sets particle casting shape's box extents. Only works if the shape is a box
/// </summary>
public static void SetShapeBoxSize(this ParticleSystem particleSystem, Vector3 boxSize)
{
ParticleSystem.ShapeModule shape = particleSystem.shape;
shape.box = boxSize;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment