0
\$\begingroup\$

My main goal is to create an explosion effect when I clicked the object. Therefore, I searched in Unity Assets, and found a cool framework called CartoonFX, which is really ok for me. Cartoon FX - Unity Store

I am using prefab -> CFXR4 Firework 1 Cyan-Purple. However, the particles scattered around are default particles, they are provided by framework itself. I want to change them with my own particle sprites. Therefore, the explosion will look more realistic, as the object itself is really exploding.

What I tried?

  1. I opened the prefab, and its particle effect system. In there, I enabled Texture Sheet Animation, change the mode to "Sprite" from "Grid". I did not add a sprite here, because I want to add the particle sprite in the script itself, since an object could be different types, like Red, Blue etc.
  2. I tried to use Claude for that, because I could not write it myself. I am sorry but I just needed to see a working prototype, then I could fix it later. However, it only gave me unvalid code pieces (like there are no attributes of ParticleSystem that Claude claims).

I already have a particle sprite, that I load it on the script, called particleSprites, a Sprite array. However, I could not change the default particle sprite of the explosion prefab to my particle. I am not very familiar with ParticleSystem and framework itself, so I could not guess what to do at this point. I want to keep all of the other things same (how particles scatter around, how they move etc.), but only change the particle sprite element.

If you can help me, or show me a way, or a resource, I would be glad! This type of things really takes so much time, and I am really searching for it for a while. Other than that, I can understand the code part itself, its OK. Thanks in advance, love you guys.

\$\endgroup\$
2
  • \$\begingroup\$ Your question is specifically how to set the texture used by a particle system from C# code, correct? If so, remember to include the C# tag. \$\endgroup\$ Commented Aug 4, 2024 at 22:25
  • \$\begingroup\$ @DMGregory Thanks, I corrected it. \$\endgroup\$ Commented Aug 5, 2024 at 9:57

1 Answer 1

0
\$\begingroup\$

You do this more or less the same way you'd change the texture on any 3D object in Unity.

First, get a reference to the object's Renderer component. In this case, that's a ParticleSystemRenderer. This component isn't shown as its own separate chunk in the inspector - the custom editor for particle systems lumps its properties in with the other particle behaviour modules in the checklist - but it's there all the same!

if (!myParticleSystem.TryGetComponent(out ParticleSystemRenderer renderer)) {
    Debug.LogError($"No Particle System Renderer found on object {myParticleSystem.name}");
    return;
}

Then we just get material that renderer is using, and change its main texture property:

renderer.material.mainTexture = myNewTexture;

Note that the first time you read .material from a given renderer, it creates a copy of the material asset unique to this object. That way you can change the texture on one instance of the effect without changing it on all instances. But if you're repeatedly assigning the same texture to several instances, you end up with redundant copies of the material taking up memory and draw calls. To control for that, you can create each texture variant of the material once, cache it, and re-use it the next time you want to use the same texture. You can also use the .sharedMaterial getter if you want to bypass this copy-on-access behaviour.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.