I’m trying to replicate the Windows game I made 6 years ago Gravity Wars this time for iOS
Coding it was simple: I painted a pixel where I wanted on screen and thus created trails for my flying projectiles. I thought I could use "Trail Renderer " in unity, but when I destroy the object that creates the trail, the trail goes away. Also, It looks like it will be a mess trying to dim each trail by some amount over time (in the original game I just drew a black rectangle across the screen at low opacity and was done).
Could anyone suggest what method would be best? I saw Textur2D.SetPixel option, but there’s a warning that it’s an “expensive operation”.
SetPixel isn’t expensive; the warning is about uploading the texture, which is expensive—the bigger the texture, the slower it is, which makes it unfeasible for realtime updating of screen-size textures. Instead, you can divide the screen into many smaller textures and just update the ones that actually need to change.
If you want a simpler, more performant way, there’s an asset for line-drawing. Can’t remember the name…starts with “V” or something…
When dividing into smaller textures, beware the obvious fact that draw calls become much higher which in itself can slow things down. Would be so much better for Unity to offer uploads to a) only a portion of an existing texture, not the whole thing, and b) zapping a byte array to a portion of an existing texture without having to go through any conversion or internal storage or whatever. It should be so much faster than it is.
The lines you are drawing on the screen seem relatively smooth for the most part, so you might be able to replace the “drawing to texture” thing by leaving behind 1x10 or so Sprite objects that would make up the line. You would do that until you have too many Sprites, then you would basically do one huge draw call on the texture and delete the sprites, then start over again.
That way you could avoid uploading a texture to GPU for most of the frames, although it is more complicated to implement…
Sorry for answering an old post, but I can’t resist. I would advice not to do this using pixels there is no need in Unity. For Mammoth Gravity Battles (a gravity wars update, would you believe), I use Unity’s trail renderer, and it is perfect. Your particles trace a path and a beautiful line is left behind.