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了。

相关推荐
TO_ZRG3 小时前
Unity打包安卓、iOS知识点
android·unity·android studio
冰凌糕3 小时前
Unity3D Shader 顶点动画 呼吸和水波
unity
呆呆敲代码的小Y5 小时前
【Unity 实用工具篇】| UX Tool 工具 快速上手使用,提高日常开发效率
游戏·unity·游戏引擎·游戏程序·ux
我送炭你添花5 小时前
Pelco KBD300A 模拟器:06+6.键盘按键扩展、LCD 优化与指示灯集成(二次迭代)
python·自动化·计算机外设·运维开发
世洋Blog5 小时前
Unity开发微信小程序-避开新InputSystem有关坑
unity·微信小程序
我送炭你添花5 小时前
Pelco KBD300A 模拟器:TEST01.重构后键盘部分的测试方案规划
python·重构·自动化·计算机外设·运维开发
简简单单OnlineZuozuo19 小时前
提示架构:设计可靠、确定性的AI系统
人工智能·unity·架构·游戏引擎·基准测试·the stanford ai·儿童
我送炭你添花1 天前
Pelco KBD300A 模拟器:06+4.KBD300A 键盘 UI 布局的设计与响应式实现(二次迭代)
ui·计算机外设
最后一个bug1 天前
为什么linux内存要分DMA区域,常规区域和高端内存区域?
linux·服务器·开发语言·系统架构·计算机外设
最后一个bug1 天前
linux内核中的一致性DMA与流式DMA
linux·开发语言·嵌入式硬件·系统架构·计算机外设