- Home /
(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!
Is there a question here? You need to make the Particle[] yourself, then let the GetParticles() fill it for you.
At least I think that's what you want to know?
Well, I've created a ParticleSystem GameObject in Unity, but I want to get the list of particles created by this ParticleSystem as an array to use in C#. How can I achieve this?
GetParticles() doesn't seem to work, but maybe I'm using it wrong. The documentation contains no example.
Answer by Jamora · Oct 03, 2013 at 08:38 PM
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
}
Ahh, thanks! This way of doing things feels really counter-intuitive (at least, for someone co$$anonymous$$g from Java).
Answer by tarahugger · Jul 31, 2014 at 08:27 AM
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 [i] = func.Invoke (particles [i]);
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;
});
}
Answer by titulus · Jul 31, 2014 at 10:55 AM
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[i];
par.size = 10f;
}
}
By doing this way you don't allocate memory for new particle array in every Update method call