Unity 模拟百度地图,使用鼠标控制图片在固定区域内放大、缩小、鼠标左键拖拽移动图片

效果展示:

步骤流程:

1.使用的是UGUI,将下面的脚本拖拽到图片上即可。

cs 复制代码
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class CheckImage : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler, IPointerEnterHandler, IPointerExitHandler
{
    public float MaxRange = 5f;
    public float MinRange = 0.5f;
    private Vector3 offPos;
    private Vector3 arragedPos;
    private bool isEnter = false;
    private bool isDrag = false;

    void Update()
    {
        if (!isEnter) return;
        if (!isDrag)
        {
            SetPivotAndPos();
            if (Input.GetAxis("Mouse ScrollWheel") > 0)
            {
                transform.localScale += (transform.localScale.x >= MaxRange ? Vector3.zero : Vector3.one * 0.1f);
            }
            else if (Input.GetAxis("Mouse ScrollWheel") < 0)
            {
                transform.localScale -= (transform.localScale.x <= MinRange ? Vector3.zero : Vector3.one * 0.1f);
            }
        }
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        isDrag = true;
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(transform.GetComponent<RectTransform>(), Input.mousePosition, null, out arragedPos))
        {
            offPos = transform.position - arragedPos;
        }
    }

    public void OnDrag(PointerEventData eventData)
    {
        transform.position = offPos + Input.mousePosition;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        transform.position = offPos + Input.mousePosition;
        isDrag = false;
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        isEnter = true;
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        isEnter = false;
    }

    private void SetPivotAndPos()
    {
        float OffsetX = Input.mousePosition.x - transform.position.x;
        float OffsetY = Input.mousePosition.y - transform.position.y;

        float PivotX = OffsetX / transform.GetComponent<RectTransform>().rect.width / transform.localScale.x;
        float PivotY = OffsetY / transform.GetComponent<RectTransform>().rect.height / transform.localScale.y;

        transform.GetComponent<RectTransform>().pivot += new Vector2(PivotX, PivotY);
        transform.localPosition += new Vector3(OffsetX, OffsetY, 0);
    }
}

2.再给图片的父物体添加个Mask遮罩就ok了。

相关推荐
土了个豆子的4 小时前
unity中的动画混合树
unity·游戏引擎
学游戏开发的5 小时前
UE学习日志#19 C++笔记#5 基础复习5 引用1
c++·笔记·学习·游戏引擎·unreal engine
奔跑的犀牛先生7 小时前
unity学习26:用Input接口去监测: 鼠标,键盘,虚拟轴,虚拟按键
unity
Dr.勿忘17 小时前
C#面试常考随笔8:using关键字有哪些用法?
开发语言·unity·面试·c#·游戏引擎
存储服务专家StorageExpert17 小时前
答疑解惑:如何监控EMC unity存储系统磁盘重构rebuild进度
运维·unity·存储维护·emc存储
Petrichorzncu20 小时前
Games104——游戏引擎Gameplay玩法系统:基础AI
游戏引擎
追逐梦想永不停1 天前
Unity实现按键设置功能代码
unity
我命由我123452 天前
游戏引擎 Unity - Unity 下载与安装
c语言·开发语言·c++·后端·unity·c#·游戏引擎
车载诊断技术2 天前
车载软件架构 --- 基于AUTOSAR软件架构的ECU开发流程小白篇
网络·unity·架构·汽车·电子电器框架·车载充电器(obc)
我命由我123452 天前
游戏引擎 Unity - Unity 启动(下载 Unity Editor、生成 Unity Personal Edition 许可证)
c语言·c++·后端·unity·c#·游戏引擎·ue4