Unity Small Map Click location map to map entity location (like the King Glory of Small map click function)

Source: Internet
Author: User

Small map of self-made function not much to say, my small map making reference address:https://www.youtube.com/watch?v=EeyZ2y2Jpz4

It is recommended to go directly to the address to see Ugui's small map of the production, I think it is better to speak.

Below to make a reference address under the premise of making a small map click location map to map entity location to go to the function.

Preparatory work:

Build the scene:

  

  

The simple thing is to build a scene on your own, and then look down at the image image that is saved as the UI as the background image of the small map (so that you don't use another camera to do the background with rendertexture)

The directory resolution for the UI is as follows:

  

Mapcontainer: A panel component plus a Mapmanager script (provided later ) and a mask component

Map: An image component that takes a picture of the entity environment above as the source of the image, plus the mapicon script and the Mapctrl script.

The above is the premise of the current article, the following begins to explain the small map click location mapping to the map of the physical location of the mathematical principle.

Step one: Get the coordinates of the mouse click to the small map (MapX,mapy),

  

So how do we get it? In fact, in this case, we can get the width and height (screen.width,screen.height) of the screen and the coordinates of the mouse click (mousex) through the API provided by unity. Mousey) and small map width and height (recttransform.rect.width,recttransform.rect.height)

With these three known conditions, we can calculate the coordinates (mapx,mapy) of clicking on the small map itself. As follows:

MapX = MouseX-(screen.width-recttransform.rect.width)

Mapy = Mousey-(screen.height-recttransform.rect.height)

  

This allows you to get a mouse click on the small map's own coordinate system.

Step two: Map the coordinates of your own coordinate system to the real map by clicking on the small maps.

Here we can get the "real terrain" width and height (meshrenderer.bounds.size.x,meshrenderer.bounds.size.z) with the bounds.size of the "real terrain" meshrender component.

In this way, the corresponding coordinates can be obtained easily by equal scale operation, and the formula is as follows:

Realx = (mapx/recttransform.rect.width) * MESHRENDERER.BOUNDS.SIZE.X-MESHRENDERER.BOUNDS.SIZE.X/2;

realy = (mapy/recttransform.rect.height) * MESHRENDERER.BOUNDS.SIZE.Y-MESHRENDERER.BOUNDS.SIZE.Y/2;

Here because my "real terrain" is placed in the center point (0,0,0), so to subtract "real terrain" half, let him return to the original point, otherwise the map coordinates will be greater than half, oneself pondering to understand.

Here is the end, there are not understand can ask me below, will try to answer.

Entire project: Https://git.oschina.net/TMoon-Net/MapManager/tree/master

The project also has small map production Code ~ ~ ~

Unity Small Map Click location map to map entity location (like the King Glory of Small map click function)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.