- Home /
Exploding texture into it's individual pixels
I'm trying to implement a system where upon taking damage, an enemy sprite loses one of it's pixels, until it reaches a point where it's health = 0, whereby all remaining pixels "explode".
Removing individual pixels is fine, I get a random coordinate for the pixel to remove, set it to Color.clear, create a new sprite with the modified texture. I then create a new empty gameObject for the removed pixel, create a 1x1 texture with the color of the chosen pixel, position it correctly, add a RigidBody2D and 1x1 Collider2D, and apply a upward velocity and gravity to it.
This all works fine, what is struggling is when I explode the sprite, I'm creating a lot of these individual pixels all at once, which is having an impact on performance. I've tried some optimisations, I'm storing all the unique textures in a dictionary on start and grabbing them when necessary, rather than creating them from scratch. I'm at a loss though, I assume there's more I can do to optimise this but I'm thinking maybe this is just too intensive, I assume this can't be achieved with a particle effect, because I want the pixels to already be created and be in their correct position and also be affected by gravity and physics.
Sorry for the wall of text, if I've missed anything just ask.
Yeah that would normally be a particle effect as particles are individually light weight - what you are trying to do sounds like it's a pretty big load on the system presu$$anonymous$$g your objects are a decent size. A particle system would allow you to place each individual particle with scripting and presumably there is only one texture with a different color on the material?
Well the sprite's textures are pretty small, and in the current one I'm testing there are only around 15 different colours, so 15 texture2D's are created when it's initialized. There is just one texture for the enemy sprite though yes, not sure what you mean about the material? So are you saying you think it'd be possible to do it with a particle system and have the particles initialised in the correct positions?
I'll check that out, thanks for the quick response, I'll let you know how it goes!
Are you creating new game objects for each of the exploded pixels at the time of the explosion?
You might get a speed up by keeping a collection of game objects lying around (disabled/invisible) and just setting their colours, positions, etc. at the time of the explosion.
Okay I implemented an object pool, however if didn't really have any effect on performance, which makes me think my issue is down to all the pixels having rigidBody / Collider components. I also tried using the SetParticles idea, but I can't seem to set an initial velocity for the particle. All I'm doing at the $$anonymous$$ute is this:
ParticleSystem.Particle[] particles;
void Start()
{
particles = new ParticleSystem.Particle[10];
for (int i = 0; i < 10; i++)
{
particles[i].position = new Vector3(Random.Range(-20, 20), Random.Range(-20, 20));
particles[i].color = new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f));
particles[i].size = Random.Range(1, 10);
particles[i].velocity = new Vector3(Random.Range(-20, 20), Random.Range(-20, 20));
}
particleSystem.SetParticles(particles, particles.Length);
}
The particles don't move though, am I missing something obvious?
Answer by Globotix · May 25, 2017 at 04:24 PM
The trick is to use an object pool and create all your pixel sprites before hand. I created an asset that works this way https://www.assetstore.unity3d.com/en/#!/content/89628
Answer by whydoidoit · Feb 17, 2014 at 11:26 AM
Yes you can set an array of particles using SetParticles and that allows you to control a bunch of the useful stuff.
I may not have exactly visualised what you are doing - but hopefully this will help...
Your answer
Follow this Question
Related Questions
How to Address Texture2D Elements from a Sprite with Sprite Mode: Multiple, in Code? 1 Answer
BoxCast, OverlapArea, and Raycasting; ground detection questions 0 Answers
PackTextures Horizontally? (1 Row) or Alternative Method 1 Answer
2D movement stutter 4 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers