How to create rainbow particles

I am looking to create an explosion of particles that appear in any of the 7 colours of the rainbow. I could create my own thing to do this with quads but was wondering if someone here knew how to do it with particles, as it’s my understanding that this would be better for performance. It would also need to work on android.

You can do it by directly accessing the particles array. I don’t know if this access eats up the performance gains you’d get from using a ParticleSystem over your own quads in a mesh. The following code expects the start color to be white with an alpha of 0.0, and the colors array to be initialized to be your rainbow of colors:

#pragma strict
 
public var colors : Color[];

private var ps : ParticleSystem;
private var particles : ParticleSystem.Particle[];
 
function Start() {
	ps = particleSystem;
	particles = new ParticleSystem.Particle[ps.maxParticles];
}
 
function Update() {
	var count = ps.GetParticles(particles);
	for (var i = 0; i < count; i++) {
	    var c = particles*.color;*
  •  if (c.r > 0.99 && c.g > 0.99 && c.b > 0.99) {*
    

_ particles*.color = colors[Random.Range(0,colors.Length)];_
_
}_
_
}_
_
ps.SetParticles(particles, count);_
_
}*_

Edit: If your emission rate is low, you could improve performance slightly by not calling SetParticles() if no particle color was changed. Also for some situations you could get ways with calling this in an InvokeRepeating() as a rate slower than the frame rate.