Modifying sprites alpha in game, is it possible?

Hi guys,
the 2D new features in Unity are great and I’m interested in create a game with some mechanics saw in games like worms or lemmings, where you can modify the terrain in real time.
So, for make this, I would like to know if it is posible to paint a texture inside other for recreate this effect, for example, to recreate an explosion on the terrain I would like to paint a zero alpha circle in the terrain texture, so the terrain shape must change for make this zone walkable.

See this example in box2D and actionscript:
http://www.emanueleferonato.com/2013/07/11/box2d-destructible-terrain-demo-using-only-geometry/

This is sort of a big topic because there are lots of different ways to do it, each with their own quirks, performance issues, side-effects and properties. The very basic version you’re talking about, to take a texture and just change the alpha values to make holes, is fairly simple. You need to make a texture for the landscape (or several textures). They have to be marked is readable in the texture importer. Then you need to create planes or quads or something in the scene to put the textures onto and position them. You probably want to create a copy of the texture in main memory for each texture, because you’re going to have to upload it from an array of Color32[ ]. Sorry to say only uploading alpha channel is implemented totally awful in Unity - it takes a float to store it in ram and can’t be uploaded without conversion. So use Color32. Then to make a hole you have to use the CPU to change the alpha values by using a script to draw a circle in main memory, and then use SetPixels32() and Apply() to upload the changes. Note you’ll need to cut your landscape into smaller chunks in order for the upload to not take forever, since Unity’s upload speed is also awful. You simultaneously have to be very careful about draw calls because too many separate textures in the camera’s view will mean more draw calls. Aim maybe for 256x256 or 512x512 for your texture size, depending on resolution. Note it may take an entire frame to upload that change on a mobile device.

Another way if you have Unity Pro is to use large rendertextures and then just draw the cutout shape onto the texture… like draw a sprite that has 0 alpha to cut the shape, then draw the rendertextures to the screen.

The bigger question is how the heck you want to do collision detection, bearing in mind if you modify stuff on the GPU side it has to really reflect on the CPU side also… whether it be pixel changes in textures or geometry changes or box2d physics collider changes (which are slow to update) etc, and ideally you want to move as little data over the graphics bus each frame as possible. The ideal way is to get zero graphics bus traffic by using rendertextures - render changes in graphics memory using the GPU and then render the same changes to a copy of the whole thing in main memory using the CPU. Keep them in sync separately and use the main memory images for collision detection. It’s kind of more popular nowadays though to go the geometry route and create physics colliders and let it deal with the whole thing, but real physics can get very slow if you’re not careful.

Hi Imaginaryhuman, thanks for your answer.
I had read about what your are explaining to me, but maybe I explained in wrong way, what I’m looking for is a way to make this more easy using the new sprite class, not a texture in a plane, just building a new sprite using the terrain sprite and a zero alpha circle sprite and upgrading the polygon collider for the terrain.
Is this possible?

The process is exactly the same. A sprite is just two triangles, just as a quad is 2 triangles and a plane is a grid of triangles. It’s just geometry with a texture slapped on it. Call it a sprite, a particle, an object, it’s all the same. You can modify the sprite’s image by modifying the texture. I am not sure if you can recalculate the sprite’s collider at the moment, but in general uploading new colliders to the physics engine is a very slow process unless you want only to have really chunky changes and not fine-grained changes.