/// <summary> /// Spawns prefab instances from particles that have died since the last update. /// </summary> void SpawnInstancesFromParticles() { // If there are no alive sorted particles, then skip doing anything else. if (sortedParticleCount <= 0) { return; } // Determine the number of newly destroyed particles. int particleCount = SpriteExploder.GetParticleCount(); int destroyedCount = sortedParticleCount - particleCount; // If there are newly destroyed particles, then we'll potentially spawn // instances of the prefab in their place. if (destroyedCount > 0) { Texture2D texture = SpriteExploder.GetTexture(); Vector2 flipVector = SpriteExploder.GetFlipVector2(); // Enumerate through the destroyed count and spawn instances where // the particle tile's texture color isn't clear in the center. for (int i = 0; i < destroyedCount; i++) { // The Sprite Exploder works by sending index values in the custom // data stream to the shader. The index value is stored in the x property. int tileIndex = (int)customDatas[i].x; // With the particle tile index, we can get the tile X and y values // from the Sprite Exploder. int tileX = SpriteExploder.GetTileX(tileIndex, tileCountX); if (flipVector.x < 0) { tileX = tileCountX - tileX; } int tileY = SpriteExploder.GetTileY(tileIndex, tileCountY); if (flipVector.y < 0) { tileY = tileCountY - tileY; } // With tile x and y values, we can determing the texture x and y // values of particle tile. int tileCenterPixelX = tileX * pixelSize + centerOffset; int tileCenterPixelY = tileY * pixelSize + centerOffset; Color color = texture.GetPixel(tileCenterPixelX, tileCenterPixelY); // If the center particle is totally clear then skip making an // instance since the tile will likely look empty or too small. if (color.a <= 0.0f) { continue; } // Get a reference to the particle and its current color. ParticleSystem.Particle particle = particles[i]; Color32 spawnColor = particle.GetCurrentColor(spriteExploderParticleSystem); // Spawn an instance of the prefab with the particle's transform data and color GameObject instance = Spawn(particle.position, particleScale, particle.rotation, particle.velocity, particle.angularVelocity, spawnColor); // Invoke the OnInstanceCreated event for any outside class that are handling that. OnInstanceCreated?.Invoke(instance); } } // Update the custom particle data UpdateCustomParticleData(); // If there are no more particles, then invoke the completed event. if (sortedParticleCount <= 0) { OnCompleted?.Invoke(); } }