How to change Particle texture of a particle system dynamically
I have a particle system and what I want to do is have the particle image change from what it is at first to something different every 5 seconds. I have 4 different images that I want these particles to rotate between. I do not know how to write the code for this however. I have made all 4 of the images into materials that are particles/additive types. Any help would be greatly appreciated.
Answer by ifurkend · Apr 11, 2018 at 03:44 PM
If you mean changing texture/material of the whole system at once, you must write a script to handle this. Your script should have a public Material array var to be assigned with your target materials. Then a public “ParticleSystemRenderer” var to be assigned the namesake component (this and “ParticleSystem” may look like the same entity, they are in fact treated separately in the engine.) And finally it is just a question of assigning your target material of specific index to the renderer’s “material” property. I haven’t tried it myself, but preferably change “sharedMaterial” property instead if it’s possible for ParticleSystemRenderer.
Edit for reply:
Not for loop. Assuming you are constantly changing between the mats as long as the script is enabled, you can call InvokeRepeating in Start() instead of Update(). You also need a float var to define the start time and interval (assuming both are identical), and an integer var for the mats array index, something like this:
float t = 2f;
int i = 0;
void Start(){
InvokeRepeating(“SwitchMaterial”, t, t);
}
void SwitchMaterial(){
if (i = mats.length){
i = 0;
}
psRenderer.material = mats[i + 1];
}
Answer by jbgaines · Apr 11, 2018 at 06:54 PM
So from what you have said this is what I have so far. I have dragged my 4 materials into the correct slots in the inspector for the 4 different materials, but I am unsure of what to do now.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class colorChange : MonoBehaviour {
public Material[] mats = new Material[4];
public ParticleSystemRenderer psRenderer;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
for (int i = 0; i < 4; i++) {
psRenderer.material = mats [i];
}
}
}
Answer by rhodnius · Mar 20, 2020 at 09:31 PM
I know this is old, but for anyone looking for something similar You could use Texture sheet animation, and just use that with the timing you want