0
\$\begingroup\$

As far as I can tell, System.Drawing namespace is not available in unity. I there an alternative library to load and manipulate or generate images on the fly?

Example: Imagine you want to make a jigsaw puzzle game and want to load and chop random images to use as textures or such.

EDIT: specifically I want to crop images and draw patches from one image onto another image. Or overlay images with grids.
Ultimately all the resulting images need to end up as textures on game objects. If need be, I can use texture.EncodeToPNG to save a copy for other purposes or for as a cached result.

EDIT AGAIN: Let's stick to a concrete example.
Player selects a photo from their iPhone library. It is chopped into pieces and appears on 9 cubes. the player need to arrange the cubes in the correct way to recreate the original image.

\$\endgroup\$
  • \$\begingroup\$ Since one of the largest responsibilities of a game engine is rendering, this functionality is found in quite a few different classes - Texture2D, RenderTexture, Material, Camera, Graphics, Renderer... You're unlikely to find a 1:1 replacement for the way you'd do it in GDI+, but if you give more details about your specific use case we can suggest Unity-friendly ways to achieve it. \$\endgroup\$ – DMGregory Mar 27 '16 at 23:08
  • \$\begingroup\$ Thanks @DMGregory I edited and added a more specific example of what I want to do. \$\endgroup\$ – Ali Mar 27 '16 at 23:34
  • \$\begingroup\$ Thank you very much @DMGregory . I edited the question again. Basically all these will end up being textures on game objects. and I guess if I need a copy on disk I can save the texture as png. \$\endgroup\$ – Ali Mar 28 '16 at 0:17
  • \$\begingroup\$ I'm still trying to visualize exactly what you're trying to do here. Try describing it from the player's point of view: what is the result they should see & play with? Forget about implementation - whether it's drawing textures onto other textures or just rendering shifted UVs - that's the unknown the answers will provide. When you rush to describe an implementation rather than a goal (especially in terms of a development environment that's unfamiliar to you), you risk running into an XY Problem \$\endgroup\$ – DMGregory Mar 28 '16 at 0:24
  • \$\begingroup\$ Really appreciate it @DMGregory. I added a concrete example. \$\endgroup\$ – Ali Mar 28 '16 at 0:42
3
\$\begingroup\$

Quoting my first comment as requested:

Since one of the largest responsibilities of a game engine is rendering, this functionality is found in quite a few different classes - Texture2D, RenderTexture, Material, Camera, Graphics, Renderer... You're unlikely to find a 1:1 replacement for the way you'd do it in GDI+, but if you give more details about your specific use case we can suggest Unity-friendly ways to achieve it

In this case, it sounds like we won't need to do any image modification or generation to get this outcome in Unity. We can use the existing texture scaling & offset capabilities of the built-in shaders.

Here's an example of one image split across 9 quads: One image split into 9 quads

Here all I've done is create a material instance for each piece. I've set the tiling x & y to 1/3, and used a different offset for each piece (every unique x-y combination of 0, 1/3, 2/3)

All of the materials are referencing the same single texture in memory, with no need to modify it or duplicate it into separate sections.

You can either prepare these materials ahead of time in the editor, and assign their texture to reference the one the user selected at runtime, or you can create these materials wholly at runtime and align their texture sections using the Material.mainTextureScale and Material.mainTextureOffset setters.

The same trick works for cubes (you just need to be careful of the face orientation)

One corner of the image mapped to all sides of a cube

(This does result in one draw call per piece, but for such a small number of pieces this isn't anything to worry about. If you were dealing with much more complex scenes, you could instead create a mesh where each quad's UVs reference only the part of the texture it needs, then render the whole mesh with one material, but that's overkill for a small puzzle)

\$\endgroup\$
  • \$\begingroup\$ perfect. Thanks. Would you also please add your first comment on my question to your answer. that would make it easier for people who later visit this question to get the full answer in one place, rather than going back and forth between comments and answers. \$\endgroup\$ – Ali Mar 28 '16 at 1:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.