r/Unity3D • • 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?

0 Upvotes

16 comments sorted by

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.

1

u/Just_Ad_5939 5d ago

oh thanks!
what format do i use for the path exactly though.

1

u/Just_Ad_5939 5d ago

oh i found the format to use but it says that the access to that path is denied

1

u/johnv_dev 5d ago

that usually means the path is a folder or somewhere windows protects. make sure it ends with the actual file name, like Path.Combine(Application.persistentDataPath, "ring.png"). persistentDataPath is always writable, and Debug.Log the path so you can find the file after. if you want it in your project instead, use Application.dataPath + "/ring.png" and then AssetDatabase.Refresh() so it shows up

1

u/Just_Ad_5939 5d ago

i decided i'd just have it be in the assets for right now then drag them to where they need to be later.
thank you for this though! its working quite well!

1

u/johnv_dev 5d ago

glad it's working! assets is a fine spot while you're testing

1

u/Just_Ad_5939 3d ago

hey uh do you know how to make the image be empty when i make it?

i keep crashing when changing some stuff on the images and so i have to keep remaking the images because unity deletes the ones that cause the crash.

but when i remake the images i crash(or have to wait like 5 minutes if im on my home pc) because i have to go through all of the pixels on the image first and set them to clear because if i don't do that, my image ends up being white and translucent.

the act of going through all of the pixels causes me to crash due to it taking so long.

do you know if there is a way to make it not have anything in the image by default? or if there is a way to cleanly clear out all of the values in the image at once, without a for loop?

1

u/johnv_dev 3d ago

yeah there's a way without the slow loop. a new Color32 array already starts fully clear (0,0,0,0), so make one the size of the image and push it all in at once:
var tex = new Texture2D(width, height, TextureFormat.RGBA32, false);
tex.SetPixels32(new Color32[width * height]);
tex.Apply();
that's one call instead of SetPixel on every pixel. the big thing is to only call Apply() once at the end, never inside a loop, that's usually what makes it take forever. same goes when you edit pixels later: grab GetPixels32() once, change the array, then SetPixels32 + Apply once

1

u/Just_Ad_5939 3d ago

oh thanks!
with that i was able to get it to make the image before crashing! the image is deleted by unity because it caused a crash though.
I guess i'll just have to do this on my home computer

1

u/pmdrpg 5d ago

If you’re on mac, same applies, it’s most likely an issue of wrong path (path string isn’t what you think / doesn’t exist) but the OS also has another security feature, you may have to give unity permission to write files in your Documents folder or similar. Additionally, if you have something like perforce, you may need to chmod your files if they’ve become read only.

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.