Okay, I asked this question yesterday anonymously thus I couldn't edit it so I'm remaking it.

Short version: I have a transparent map with black borders (Colored map) and I want those shapes formed by these borders to become individual objects in Unity. Is it even possible and if it is, how?

Long version: I want to make a Grand Strategy type game with provinces and countries. However, I'm not really an experienced graphics nor Unity programmer.

I already have a map drawn with the desired black borders with each province being defined by these very borders and each has an unique color (sort of an identifier). Having just this seems plain and complicated to use so I'd like for each province to emit a color corresponding with a selected map type (diplomacy, terrain, domains, etc.). Obviously, this would be hard to do with just a single plain texture therefore I need to make objects and this is where my inexperience blocks me from continuing.

Is there anything I can do (in Unity and/or Photoshop) that could make the provinces be objects without manually isolating, importing and matching (as a puzzle) each single one?

I've already looked at countless questions and answers but still none fit my problem. I'm at my wits' end with this to the point where I'm actually thinking about scrapping the project.

  • \$\begingroup\$ Instead of creating duplicate questions, you should merge your user profiles so that you can edit and improve your existing post. \$\endgroup\$
    – DMGregory
    Feb 19 '17 at 14:58
  • \$\begingroup\$ I added the previous one anonymously. The previous question should be deleted then. \$\endgroup\$
    – Branimir
    Feb 19 '17 at 16:28

This could be done with some custom code. You could write this as part of your game code and then disable it after it runs once, or make some kind of Unity extension to run it when needed.

Here's how I'd structure it:

  • Get the map pixels into an array, and make a second array of the same size.
  • Clear the second array to 0,0,0,0.
  • Walk through the map array; when a non-black pixel comes up, run a flood-fill algorithm starting from there. Fill the map pixels with black, and track the filled pixels by writing them to the same positions in the second array. Also keep track of the min and max X and Y that the flood reaches.
  • Create a texture, sized to (max X - min X, max Y - min Y), and copy those pixels from your second array into this new texture. Save this image to disk, and also save the min and max X and Y somewhere.
  • Clear the second array again, and continue walking the map array.

You'll need to keep track of which images go with which min and max positions from the original map so you can position the sprites or quads or images and apply the images to them.

You could also do this all at load time and immediately create whatever geometry you want.

One way to use the generated images in your game would be to create a prefab that has a SpriteRenderer component on it, instantiate one instance of the prefab for each region, and set the SpriteRenderer's "sprite" to the region image. You can use the min and max X and Y values to figure out where to position these prefabs so they reconstruct the original map. You can then use the color property of the sprite renderer to represent whatever information you want.

  • \$\begingroup\$ Excuse me for my lateness and ineptness, but I just need a bit of elaboration. The second array would just be contrasted transparent layer, ie parts of the original map that are transparent will be shaded black on the second map (array)? If I did assume correctly, how would this exactly create objects within Unity? I sincerely thank you for your reply :) \$\endgroup\$
    – Branimir
    Feb 27 '17 at 20:37
  • \$\begingroup\$ That's close to correct, but not quite what I meant. The second array is temporary storage that you use as you are detecting each map region. I'll edit my response with more information. \$\endgroup\$
    – Victor T.
    Feb 28 '17 at 21:21
  • \$\begingroup\$ You could write this as part of your game code and then disable it after it runs once, or make some kind of Unity extension to run it when needed. Not really sure what you mean by "some kind of Unity extension" but it's really easy to write editor scripts in Unity. \$\endgroup\$
    – jhocking
    Feb 28 '17 at 22:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .