Best way to create an interactive map using an existing image - unity3d

I'm trying to create an interactive world map like in Europa Universalis IV or Crusader Kings II using Unity3D. These two games create the map using an existing image (like this https://i.imgur.com/y2gtX2N.png). I have never done something like this before and I'm really confused on which is the best approach/technique to use.
What I need is an idea on how to show the map and be able to select every single province taken from the image.

My method would require you to provide each region as an individual sprite.
1) Add all sprites to the scene and form the world map (SpriteRenderer or UI.Image).
2) Make a Component that handles interaction, and add it to each World Map Part GameObject.
Implement the following interfaces to it, and specify the logic accordingly:
3) Add the Polygon Collider 2D Component to each World Map Part GameObject.
4) Add the Physics Raycaster 2D Component to your Camera.
If you choose to use UI.Image (which I do not recommend), you can skip steps 3 and 4, and instead set alphaHitTestMinimumThreshold to 1 on your World Map Part Component's Awake().

Related

Render order according to hierarchy in Unity

I am trying to understand how Unity decides what to draw first in a 2D game. I could just give everything an order in layer, but I have so many objects that it would be so much easier if it was just drawing in the order of the hierarchy. I could write a script that gives every object its index, but I also want to see it in editor.
So the question is, is there an option that I can check so that it uses the order in the hierarchy window as the default sorting order?
From your last screenshot I could see you are using SpriteRenderer.
The short answer to the question "is there an option that I can check so that it uses the order in the hierarchy window as the default sorting order?" would be no, there isn't by default*.
Sprite renderers calculates which object is in front of others in one of two ways:
By using the distance to the camera, this will draw the objects closest to the camera on top of the rest of the objects in that same order in layer, as per the docs:
Sprite Sort Point
This property is only available when the Sprite Renderer’s Draw Mode is set to Simple.
In a 2D project, the Main Camera is set to Orthographic Projection mode by default. In this mode, Unity renders Sprites in the order of their their distance to the camera, along the direction of the Camera’s view.
If you want to keep everything on the same sorting layer/order in layer you can change the order in which the objects appear by moving one of the two objects further away from the camera (this is probably further down the z axis). For example if your Cashew is on z = 0, and you place the walnut on z = 1 then the cashew will be drawn on top of the walnut. If Cashew is on z=0 and the walnut is on z = -1 then the walnut will be draw on top (Since negative is closer to the camera). If both of the objects are on z - 0 they are both equally as far away from the camera, so it becomes a coin toss for which object gets drawn in front, as it does not take into account the hierarchy.
The second way the order can be changed is by creating different sorting layers, and adjusting the order in layer in the sprite renderer component. But you already figured that out.
*However, that doesn't mean it cannot be done, technically...
If you feel adventurous there is nothing stopping you from making an editor script that automates setting the order in layer for you based on the position in the hierarchy. This script would loop through all the objects in your hierarchy, grab the index of the object in the hierarchy, and assign the index to the Order in Layer.
I don't think Unity has such feature (https://docs.unity3d.com/Manual/2DSorting.html).
Usually you shall define some Sorting Layers:
far background
background
foreground
and assign Sprite Renderer of each sprite to one of Sorting Layers

Random spawn avoiding colliders

I am working on a 2D game with colleagues that is basically a fighting game on a vertical map with platforms. Right now I am spawning items on the map for player to gather, but I was wondering how could I do that while checking that I'm not in one of the platform's collider to avoid spawning the item in?
You should take a look at the overlap functions from Physics2D (for example here). Just generate a drop position and pass it to this function, if it returns null then the position is free. If you get unexpected results make sure to set the layer mask to the same as the platforms or whatever objects you don't want to intersect with.

Unity 2D game, big tiles

I am making a big open world game where the map is hand drawn.
The dimensions of the map are 12800x3840.
Thus I have made world-tiles 1280x768 ( having 20x12 64x64 tiles )
My initial approach is to draw 9 world-tiles which are around the player. I also want my collision system to work on a tile based approach where i have a 20x12 2 dimension array. Is this the right way?
How do I do this in Unity? What objects do I use?
I used X-UniTMX to very good results on a tile based strategy game I'm working on. First you create the map using Tiled then you can import it using this software.
Tiled:
http://www.mapeditor.org
X-UniTMX:
https://bitbucket.org/Chaoseiro/x-unitmx/wiki/Home

Tile Grid Data storage for 3D Space in Unity

This question is (mostly) game engine independent but I have been unable to find a good answer.
I'm creating a turn-based tile game in 3D space using Unity. The levels will have slopes, occasional non-planar geometry, depressions, tunnels, stairs etc. Each level is static/handcrafted so tiles should never move. I need a good way to keep track of tile-specific variables for static levels and i'd like to verify if my approaches make sense.
My ideas are:
Create 2 Meshes - 1 is the complex game world, the second is a reference mesh overlay that will have minimal geometry; it will not be rendered and will only be used for the tiles. I would then Overlay the two and use the 2nd mesh as a grid reference.
Hard-code the tiles for each level. While tedious it will work as a brute force approach. I would, however, like to avoid this since it's not very easy to deal with visually.
Workaround approach - Convert the 3d to 2D textures and only use 1 mesh.
"Project" a plane down onto the level and record height/slope to minimize complexity. Also not ideal.
Create individual tile objects for each tile manually (non-rendered). Easiest solution i could think of.
Now for the Unity3D specific question:
Does unity allow selecting and assigning individual Verts/Triangles/Squares of a mesh and adding componenets, scripts, or variables to those selections; for example, selecting 1 square in the 10x10 unity plane and telling unity the square of that plane now has a new boolean attached to it? This question mostly refers to idea #1 above, where i would use a reference mesh for positional and variable information that were directly assigned to the mesh. I have a feeling that if i do choose to have a reference mesh, i'd need to have the tiles be individual objects, snap them in place using the reference, then attach relevant scripts to those tiles.
I have found a ton of excellent resources (like http://www-cs-students.stanford.edu/~amitp/gameprog.html) on tile generation (mostly procedural), i'm a bit stuck on the basics due to being new to unity and im not looking for procedural design.

TiledMaps and real objects

I'm creating a Super Mario Bros. remake for the iPhone using Cocos2d.
I'm having a hard time understanding how add real objects to a tiled map.
I know that you can add an object-layer to the tiled map and set the position of it,
but I'm not sure how to define WHAT object if should be.
For example, I'm creating a simple Super Mario Bros.
I want to add different blocks to the map, one should hold a star, one should hold a coin, and so on.
So my question, how do I correctly create class-objects in a tiled map?
I have used object layers in the past. For you needs you could create an object layer and drop an object on that layer. Type of box. It could have variables in the object of powerup then the value could be whatever you want. Coin, 1Up, Star etc..!