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:因为是新手的关系,所以代码不是很完善,后面等功能完成,会和大家一起分享代码的

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值