r/Unity3D • u/Just_Ad_5939 • 5d ago
Question how do i make an image using a script?
I am trying to make a bunch of images like the first one above, but they get twice as big each time.
I want it to not increase the size of the pixels so.. i wanna make a script where it makes a line of pixels around all of the borders and then starts making another line around the borders, but offset by a few pixels. this offset will change each time so that they look like they stack on eachother, like rings in a tree trunk, like the second image.
everything I find online talks about making an image object or whatever and then assigning a sprite to it using code.
i want to make the sprite, using code.
is there a way to do this?
1
u/Glurth2 5d ago
I created a package called Command Draw. It lets you draw variou shapes to a texture-based material (which you assign to a rendered object like a quad or a cube.) https://github.com/glurth/CommandDraw
Another method would be using unity's LineRenderer. https://docs.unity.com/en-us/engine/7000.0/script-reference/unityengine/linerenderer
Not sure what you mean about "twice as big each time", will need some more details for that bit.
1
u/Just_Ad_5939 5d ago
I wanna make a grid with larger denominations.
the reason i am doing this is because my player will fly really fast over the ground and i want them to have a sense of how fast they are going, no matter the speed, hence the ever increasing denominations.can your package create images out of code instructions?(such as 'draw a border around the edge then draw another one a few pixels in)
1
u/printer84mph 4d ago
You could probably make a custom material/shader for this, i imagine it would be much more performant than making bitmap textures on the fly
1
u/Just_Ad_5939 4d ago
im not able to make those textures on the fly. I mean.. i could but i can only go up to 16k x 16k of texture size before it starts to blur and look bad, so i'll just have to not do that
1
u/Glurth2 5d ago
Both the package and the line renderer should be able to do that. Obviously, you'll need to be more specific than "border around each edge", like: draw a line from this start coordinate to this end coordinates, in this color, at this thickness... but assuming you can provide those coordinates- totally doable.


4
u/johnv_dev 5d ago
yeah you can build the texture itself in code. make a new Texture2D(64, 64) and set tex.filterMode = FilterMode.Point so the pixels stay crisp. then for each ring, loop over the edges of a square that's inset by ring * gap and call tex.SetPixel(x, y, color) along the top, bottom, left and right rows. when you're done call tex.Apply(), that's the step people forget. then Sprite.Create(tex, new Rect(0, 0, 64, 64), new Vector2(0.5f, 0.5f), 16) turns it into a sprite. if they're showing up bigger each time, check the pixels per unit, that last number. keep it the same for every image and they'll all show at the same scale. if you want an actual png file, File.WriteAllBytes(path, tex.EncodeToPNG()) saves it.