- Home /
How to do a selectable map?
Hello, I'm starting with a board game in C# like Risk and I need a selectable world map to put the armies on each region. I have a Raycasting function to select each country but I need to know how to build the board and make the countries selectable.
I have a board like this: http://2.bp.blogspot.com/-vtEHvcmS-Ac/TtHk0IvsxoI/AAAAAAAAAnw/V6e_eGfmCac/s1600/Risk+II+Game+Board.jpg
And I want to be able to select the countries separately, like they're objects on their own. Do you know any ways to do that?
Thanks for the help!
Answer by Bunny83 · Jul 24, 2012 at 03:03 PM
If you want to select countries, i suggest you use a color mask texture. Just an additional texture where each country / region is painted in a different solid color. A grayscale map would be enough. So for example one area that should be selectable is drawn with an RGB value of 20,20,20 the next one would be 21,21,21.
Now you can use the texcoord from your raycast to find the "color" in the colormap. Just use Texture2D.GetPixel to read the color. Just convert it into a Color32 to get byte values for each component. This can be used as index. For this you need of course paint each area / country with a certain color value so you can identify the right area.
This is the most accurate way. Another way is of course to use GUI buttons or Rects, but it won't match the actual shape.
edit
Since some are confused by this approach, here's it a bit more in detail:
I've just created an example selection texture for the map the OP posted. It uses the green channel to specify the "continent" and the red channel (followed by blue) to specify the region. I used a color steps of 20 so each color channel can have 13 different states (12 are only used because 0== black is never used as region). That means i could have 12*12*12 regions which equals 1728.
In my example to get the continent and the region index i just do
// C#
int continent = (int)(pickedColor.g*12.75f+0.5f);
int regionLow = (int)(pickedColor.r*12.75f+0.5f);
int regionHigh = (int)(pickedColor.b*12.75f+0.5f);
int region = regionHigh*13 + regionLow;
You could use any colorstep but the smaller it gets the more likely errors will come in (see compression issues mentioned in the comments). In theory you could have around 16 million regions in one map. Just to put that in relation: that would be a 4000*4000 texture with every pixel has a distinct color.
Thats such an interesting idea. No use for it right now, but I'll store that for later. Nice one.
Here's a monochrome version of the map. I'm not an artist so i kinda suck when it comes to Photoshop ;) You can probably extract the desired areas more accurate.
Now you just need to color each area with a different color. I'm not sure what areas / countries you need / want since this map lacks of many borderlines, especially in europe. All in all the map looks a bit "morphed".
$$anonymous$$eep in $$anonymous$$d that the color map doesn't nees to match perfectly. The user won't see the colormap. It just should match your desired regions.
I think I explianed myself wrong. I have a plane with that picture of the world map attached as an image. I'm looking for some way to make a map where the countries are selectable objects, or in another case a way to make colliders for each one of them onto that.
Is there any way to create planes with the country shapes easily?
Wow that is grat Bunny83, thank you a lot. I'll try with it!! :)
be aware about texture compression 8) built-in compression will make a piece of garbage from texture with such color mask for countries 8) better to disable compression at all for this.
Answer by Lukas-Barbarossa · Jul 24, 2012 at 02:16 PM
Or, if you want a 3D map, you could make your main landmass a 3D model and then cut it up in Maya or Max into it's constituent territories. Remember though, if you want a 'cliff face' thickness to your map, to make it as a poly not a plane.
Answer by Kronnect · Jul 24, 2015 at 07:53 AM
You can use World Political Map which includes source code (C#) for highlighting and choosing countries with just one line call :-) https://www.assetstore.unity3d.com/en/#!/content/41890
Answer by jc_lvngstn · Jul 24, 2012 at 07:18 PM
I'd check out NGUI. You know how most board games like this have a map/board with old style drawings on the sides, like a whale or waves in the ocean? With NGUI, you would set up a panel with that background texture. Then, you could have each country as a UI element. NGUI can highlight them, make them bounce a little, etc as you select or mouseover them. You just specify the colors, the effect, etc.
Basically, each country would be a button or some sort of UI element. You could create them using something like blender, or use something from the asset store to trace them. And the cool thing is...they would have shader effects. Make them glow during combat, or something.
Really...once you figure it out, it's incredibly powerful. And you could make the entire MAP/game do things like slide into view, spin on game over, all kinds of stuff. Easy. Zoom down to ground level to watch the fighting, if you want to get fancy!
In a nutshell: User NGUI, and have each country as a part of the UI. NGUI can handle the rest, and tell you which one you have selected, mouseover, etc.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Initialising List array for use in a custom Editor 1 Answer
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
Flip over an object (smooth transition) 3 Answers