- Home /
How to change the mesh of a particle system at runtime C#
Hi guys,
I would like to change the mesh of a particle system at runtime, but I have no clue how I get the "Renderer"-part of the Particle System Component.
Does anyone here has experience with this or knows the answer?
Thanks for any help =)
Answer by CHPedersen · Jul 15, 2013 at 10:54 AM
There is no single instance of the Mesh class for a particle system. Rather, it is a collection of meshes where each particle is a 2-triangle plane that is essentially a textured billboard. You can't access these in the same sense you access the .mesh property of a MeshFilter component.
But, depending on what you want to do with it, you have some options. MonoBehavior exposes a property called particleEmitter that allows you to access the ParticleEmitter component. It in turn exposes an array which contains instances of the Particle class. These contain the data you would typically associate with a single particle, such as its velocity, color, position, etc. You may then change these properties as you see fit, and assign the changed array of particles back to the emitter when done, like so:
Particle[] particles = particleEmitter.particles;
particles[0].position = new Vector3(0, 0, 0);
particleEmitter.particles = particles;
This is somewhat akin to how you would modify the individual vertices of a Mesh and then assign the changed vertex array back to the mesh to see your changes take effect. Have a look at the particleEmitter (and, possibly, the particleSystem) property in scripts, and see if they satisfy your needs. If you use an editor that doesn't offer code completion (Intellisense), you can see the available properties of class ParticleEmitter here:
http://docs.unity3d.com/Documentation/ScriptReference/ParticleEmitter.html
And class Particle here:
http://docs.unity3d.com/Documentation/ScriptReference/Particle.html
Answer by NathanJSmith · Oct 23, 2018 at 04:19 AM
You can access the mesh of Particle Fx via ParticleSystemRenderer.mesh
For example:
GetComponent<ParticleSystemRenderer>().mesh =
Resources.GetBuiltinResource("Capsule.fbx");