Unity2D 简易2D地图 —— 地图的显示
剛剛接觸Unity,新手入門,項目為GIS類應用,包括地圖的顯示,漫游和縮放等功能,記錄如下:
1.新建Unity2D項目,準備好一張世界地圖
2.建立空物體,命名為MapView,把上面的圖片拖放到MapView下面(Scale的設置自己調整,最好高度與camera差不多,并且添加一個BoxCollider2D的組件)
顯示如下:
3.接下來我們需要控制地圖的縮放、漫游拖動效果,在MapView上掛載MapView.cs來控制,代碼部分如下:
using System.Collections.Generic;
using UnityEngine;
public class MapView : MonoBehaviour
{
? ? public Camera m_camera;
? ? public float moveSpeed = 0.001f;
? ? private Vector3 mouseFirstPos = Vector3.zero;// 鼠標第一位置點【長按拖動】
? ? private GameObject mapview;
?? ?float camara_min = 0.001f;
?? ?float camara_max = 5f;
?? ?float aspectRatio = 1f; //屏幕寬高比
?? ?float earth_width = 1f; //地圖的寬度
?? ?float earth_height = 1f;//地圖的高度
?? ?float truemapscale = 1f;//實際地圖與顯示地圖的比例
?? ?// Start is called before the first frame update
?? ?void Start()
? ? {
?? ??? ?aspectRatio = Screen.width * 1.0f / Screen.height;
? ? ? ? mapview = GameObject.Find("MapView");
?? ??? ?GameObject earth = GameObject.Find("Earth4");
?? ??? ?BoxCollider2D box2d = earth.GetComponent<BoxCollider2D>();
?? ??? ?earth_width = box2d.size.x * earth.transform.localScale.x;
?? ??? ?earth_height = box2d.size.y * earth.transform.localScale.x;
?? ??? ?truemapscale = earth_height / 180f;
?? ??? ?LoadAirport();
?? ?}
? ? // Update is called once per frame
? ? void Update()
? ? {
?? ??? ?if (!ExistPopups()) //PS:該部分代碼可忽略,是我用來控制彈出窗體后禁用的,可不判斷
?? ??? ?{
?? ??? ??? ?//長按
?? ??? ??? ?if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
?? ??? ??? ?{
?? ??? ??? ??? ?if (mouseFirstPos != Vector3.zero && mouseFirstPos != Input.mousePosition)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?var position = new Vector2(Input.mousePosition.x - mouseFirstPos.x, Input.mousePosition.y - mouseFirstPos.y);
?? ??? ??? ??? ??? ?if (Input.GetMouseButton(0))
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?DragMove(position);
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?mouseFirstPos = Input.mousePosition;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?mouseFirstPos = Input.mousePosition;
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?//鼠標抬起事件
?? ??? ??? ?if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1))
?? ??? ??? ?{
?? ??? ??? ??? ?mouseFirstPos = Vector3.zero;
?? ??? ??? ?}
?? ??? ??? ?//鼠標滑輪滾動
?? ??? ??? ?if (Input.GetAxis("Mouse ScrollWheel") != 0)
?? ??? ??? ?{
?? ??? ??? ??? ?float swheel = Input.GetAxis("Mouse ScrollWheel") > 0 ? 0.1f : -0.1f;
?? ??? ??? ??? ?ZoomInOut(m_camera.orthographicSize - swheel * m_camera.orthographicSize / 5);
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
? ? }
?? ?/// <summary>
?? ?/// 鼠標拖動【相機】
?? ?/// </summary>
?? ?/// <param name="position"></param>
?? ?void DragMove(Vector2 position)
?? ?{
?? ??? ?float bs = 5 / m_camera.orthographicSize;
?? ??? ?float max_x = earth_width / 2 - aspectRatio*m_camera.orthographicSize;
?? ??? ?float move_x = position.x * moveSpeed/bs;
?? ??? ?float newx = m_camera.transform.position.x - move_x;
?? ??? ?if (newx >= -max_x && newx <= max_x)
?? ??? ?{
?? ??? ??? ?m_camera.transform.Translate(Vector3.right * -move_x, transform);
?? ??? ?}
?? ??? ?float max_y = earth_height / 2 - m_camera.orthographicSize;
?? ??? ?float move_y = position.y * moveSpeed/bs;
?? ??? ?float newy = m_camera.transform.position.y - move_y;
?? ??? ?if (newy >= -max_y && newy <= max_y)
?? ??? ?{
?? ??? ??? ?m_camera.transform.Translate(Vector3.up * -move_y, transform);
?? ??? ?}
?? ?}
?? ?/// <summary>
?? ?/// 放大縮小【相機】
?? ?/// </summary>
?? ?/// <param name="zoom"></param>
?? ?void ZoomInOut(float zoom)
?? ?{
?? ??? ?float old_level = m_camera.orthographicSize; //原級別
?? ??? ?if (zoom > camara_max) { zoom = camara_max; }
?? ??? ?if (zoom < camara_min) { zoom = camara_min; }
?? ??? ?m_camera.orthographicSize = zoom;
?? ??? ?Vector3 nowcamera = m_camera.transform.position;
?? ??? ?float max_x = earth_width / 2 - aspectRatio * m_camera.orthographicSize;
?? ??? ?float max_y = earth_height / 2 - m_camera.orthographicSize;
?? ??? ?if (nowcamera.x < -max_x) { nowcamera.x = -max_x; }
?? ??? ?if (nowcamera.x > max_x) { nowcamera.x = max_x; }
?? ??? ?if (nowcamera.y < -max_y) { nowcamera.y = -max_y; }
?? ??? ?if (nowcamera.y > max_y) { nowcamera.y = max_y; }
?? ??? ?m_camera.transform.position = nowcamera;
?? ?}
?? ?/// <summary>
?? ?/// 監測是否存在彈出窗體,如果存在就禁用地圖拖動和縮放功能
?? ?/// </summary>
?? ?/// <returns></returns>
?? ?bool ExistPopups()
? ? {
? ? ? ? bool ispopups = false;
?? ??? ?GameObject[] objs = GameObject.FindGameObjectsWithTag("Popups");
?? ??? ?for (var i = 0; i < objs.Length; i++)
?? ??? ?{
?? ??? ??? ?BasePanel panel = objs[i].GetComponent<BasePanel>();
?? ??? ??? ?if (panel.Isshow) { ispopups = true; break; }
?? ??? ?}
?? ??? ?return ispopups;
? ? }
}
?
好了,地圖的簡易顯示功能就這樣了,后面我會繼續更新~~~
PS:因為是新手的關系,所以代碼不是很完善,后面等功能完成,會和大家一起分享代碼的
總結
以上是生活随笔為你收集整理的Unity2D 简易2D地图 —— 地图的显示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑ppt录制微课软件哪个好 电脑ppt
- 下一篇: Vim中文本全部选中