(Shuriken) Accessing Particle[] Array Created in Inspector?

Hi,

I’m trying to grab the Particle array attached to a Particle System with the variables set in Inspector. Apparently, ParticleSystem.GetParticles() doesn’t work because the argument for that function requires a Particle, which is what I’m trying to get. The examples I’ve found all create a new Particle array within the script, rather than getting the list of particles from the existing ParticleSystem settings.

I’ve tried:

Particle[] particles = particleSystem.GetParticles();

But the input argument of GetParticles is a Particle. Is there a way to get an array of particles emitted by a ParticleSystem that was created in Inspector, instead of in a script?

Thanks for the help!

You need to first create an array of Particles. The size of that array should be how many particles you want. Then, you pass that array on as the parameter for GetParticles(), which then fill the array for you.

Particle[] allParticles = new Particle[particleSystem.particleCount];
particleSystem.GetParticles(allParticles);

foreach(Particle p in allParticles){
    //work your magic
}

Jamora’s answer is great; Here’s some extensions for working with it in a more familiar way.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Particle = UnityEngine.ParticleSystem.Particle;

public static class ParticleSystemExtensions
{
	/// <summary>
	/// Method that takes a Particle argument
	/// </summary>
	public delegate Particle UpdateParticleDelegate (Particle particle);

	/// <summary>
	/// Get particles Array
	/// </summary>
	public static Particle[] GetParticles (this ParticleSystem particleSystem)
	{
		var particles = new Particle[particleSystem.particleCount];
		particleSystem.GetParticles (particles);				
		return particles;
	}
	
	/// <summary>
	/// Set particles Array
	/// </summary>
	public static void SetParticles (this ParticleSystem particleSystem, Particle[] particles)
	{
		particleSystem.SetParticles (particles, particles.Count ());
	}	

	/// <summary>
	/// Applies the given function to each particle in a ParticleSystem
	/// </summary>
	/// <param name="func">Function to perform on each particle</param>
	public static void UpdateParticles (this ParticleSystem particleSystem, UpdateParticleDelegate func)
	{
		var particles = new Particle[particleSystem.particleCount];
		var particleCount = particleSystem.GetParticles (particles);		
		int i = 0;
		while (i < particleCount) {
			particles  _= func.Invoke (particles *);*_

* i++;*
* }*
* particleSystem.SetParticles (particles, particleCount);*
* } *
*} *
This adds an overloads for parameterless constructor and set without particle count. And you can use the update method like this:
* void LateUpdate ()*
* { *
* particleSystem.UpdateParticles (particle => {
_
particle.color = Color.red; *

* particle.size = 10;*
* return particle;*
* }); *
* }*

I have not tested this, but would try something like this, especially on mobile platforms:

Particle particles = new Particle[maxNumOfParticles];

Update() // or any update method
{
    int numOfWritedParticles =  particleSystem.GetParticles(particles);

    for(int i = 0; i < numOfWritedParticles; i++)
    {
        Particle par = particles*;*

par.size = 10f;
}
}
By doing this way you don’t allocate memory for new particle array in every Update method call