2D地图的拖动与缩放

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class MapControl : MonoBehaviour, IPointerEnterHandler, IPointerDownHandler, IDragHandler, IPointerUpHandler
{
    private Vector3 targetScreen;// 目标物体的屏幕空间坐标  
    private Vector3 targetWorld;// 目标物体的世界空间坐标  
    private Transform target;// 目标物体的空间变换组件  
    private Vector3 mouseScreen;// 鼠标的屏幕空间坐标  
    private Vector3 start;//初始位置

    private RectTransform canvas;//得到canvas的ugui坐标
    private RectTransform imgRect;//得到图片的ugui坐标
    Vector2 offset = new Vector3();//用来得到鼠标和图片的差值
    Vector3 startPosition;//初始位置

    float maxX, maxY, minX, minY;

    public static bool isExitMap=true;

    void Awake()
    {
        target = transform;
        start = transform.localPosition;

        canvas = GameObject.Find("Canvas").GetComponent<RectTransform>();
        imgRect = GetComponent<RectTransform>();
    }

    private void Update()
    {
        ScrollWheelControl();
    }
    void ScrollWheelControl()//鼠标滑轮滚动控制物体大小
    {
        if (isExitMap) return;
        float scaleOffset = Input.GetAxis("Mouse ScrollWheel");
        if ((transform.localScale.x + scaleOffset) <= 3 && (transform.localScale.x + scaleOffset) >= 1)
        {
            transform.localScale = new Vector3(transform.localScale.x + scaleOffset, transform.localScale.y + scaleOffset, transform.localScale.z + scaleOffset);

            maxX = 950 * (transform.localScale.x - 1);
            maxY = 500 * (transform.localScale.x - 1);
            minX = -950 * (transform.localScale.x - 1);
            minY = -500 * (transform.localScale.x - 1);

            if (scaleOffset < 0)//缩放的时候逐渐返回原点(防止缩放后视界停留在最大视界外)
            {
                transform.localPosition = Vector3.MoveTowards(transform.localPosition, start, 100f);
            }
        }
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        isExitMap = false;
    }
    void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
    {
        startPosition = gameObject.transform.position;
        Vector2 mouseDown = eventData.position;    //记录鼠标按下时的屏幕坐标
        Vector2 mouseUguiPos = new Vector2();   //定义一个接收返回的ugui坐标
        bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, mouseDown, eventData.enterEventCamera, out mouseUguiPos);
        if (isRect)   //如果在
        {
            //计算图片中心和鼠标点的差值
            offset = imgRect.anchoredPosition - mouseUguiPos;
        }
    }

    void IDragHandler.OnDrag(PointerEventData eventData)
    {
        Vector2 mouseDrag = eventData.position;   //当鼠标拖动时的屏幕坐标
        Vector2 uguiPos = new Vector2();   //用来接收转换后的拖动坐标
                                           //和上面类似
        bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, mouseDrag, eventData.enterEventCamera, out uguiPos);
        if (isRect)
        {
            //设置图片的ugui坐标与鼠标的ugui坐标保持不变
            imgRect.anchoredPosition = offset + uguiPos;
            LimitArea();
        }
    }

    void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
    {
        offset = Vector2.zero;
    }

    void LimitArea()
    {
        if(imgRect.localPosition.x>maxX)
            imgRect.localPosition = new Vector3(maxX,imgRect.localPosition.y, imgRect.localPosition.z);
        if(imgRect.localPosition.y>maxY)
            imgRect.localPosition = new Vector3(imgRect.localPosition.x, maxY, imgRect.localPosition.z);
        if (imgRect.localPosition.x<minX)
            imgRect.localPosition = new Vector3(minX, imgRect.localPosition.y, imgRect.localPosition.z);
        if (imgRect.localPosition.y<minY)
            imgRect.localPosition = new Vector3(imgRect.localPosition.x, minY, imgRect.localPosition.z);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值