Unity 大地图滑动与缩放

Posted 格拉格拉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity 大地图滑动与缩放相关的知识,希望对你有一定的参考价值。

目录

1.UGUI实现大地图滚动与缩放

2.Sprite Renderer实现大地图滑动与缩放


1.UGUI实现大地图滚动与缩放

 

 如图,大地图尺寸是超出屏幕1920*1080的,我们使用ScrollView实现大地图滑动

 ⚠️注意:camera使用 Perspective

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UiScale : MonoBehaviour

    public ScrollRect bgScroll;

    private float lastDis = 0;
    private float cameraDis = 0;

    void Start()
    

    

    private void OnEnable()
    
    

    void Update()
    
        if (Input.touchCount > 1)
        
            bgScroll.horizontal = false;
            bgScroll.vertical = false;
            Touch t1 = Input.GetTouch(0);
            Touch t2 = Input.GetTouch(1);
            if (t2.phase == TouchPhase.Began)
            
                lastDis = Vector2.Distance(t1.position, t2.position);
            
            else
            if (t1.phase == TouchPhase.Moved && t2.phase == TouchPhase.Moved)
            
                float dis = Vector2.Distance(t1.position, t2.position);
                if (Mathf.Abs(dis - lastDis) > 1)
                
                    if (dis != lastDis)
                    
                        if (dis < lastDis)
                        
                            if (cameraDis != 0)
                            
                                cameraDis += 10;
                                cameraDis = Mathf.Clamp(cameraDis, -50, 0);
                                lastDis = dis;
                            
                        
                        else
                        
                            if (cameraDis != -50)
                            
                                cameraDis -= 10;
                                cameraDis = Mathf.Clamp(cameraDis, -50, 0);
                                lastDis = dis;
                            
                        
                    
                
            
        
        else
        
            bgScroll.horizontal = true;
            bgScroll.vertical = true;
        

        if (Input.mouseScrollDelta.y != 0)
        
            if (Input.mouseScrollDelta.y > 0)
            
                cameraDis -= 10;
                cameraDis = Mathf.Clamp(cameraDis, -50, 0);
            
            else
            
                cameraDis += 10;
                cameraDis = Mathf.Clamp(cameraDis, -50, 0);
            
        

        if (Input.GetKeyDown(KeyCode.Escape))
        
            Application.Quit();
        
    

    private void LateUpdate()
    
        if (this.transform.position.z == cameraDis)  return; 
        this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, cameraDis);
    


2.Sprite Renderer实现大地图滑动与缩放

 

 ⚠️注意:Camera使用Orthographic

using UnityEngine;

public class SpriteBgContr : MonoBehaviour

    public Camera _camera;

    private Vector3 oriMousePos;
    private Vector3 oriPos;

    private int curDis = 3;
    private float lastDis = 0;

    void Start()
    

    

    void Update()
    

    

    private void OnMouseDown()
    
        oriMousePos = Input.mousePosition;
        oriPos = _camera.transform.position;
    

    private void OnMouseDrag()
    
        if (Input.touchCount > 1)
        
            Touch t1 = Input.GetTouch(0);
            Touch t2 = Input.GetTouch(1);
            if (t2.phase == TouchPhase.Began)
            
                lastDis = Vector2.Distance(t1.position, t2.position);
            
            else
            if (t1.phase == TouchPhase.Moved && t2.phase == TouchPhase.Moved)
            
                float dis = Vector2.Distance(t1.position, t2.position);
                if (Mathf.Abs(dis - lastDis) > 1)
                
                    if (dis != lastDis)
                    
                        if (dis < lastDis)
                        
                            if (curDis < 3)
                            
                                curDis += 1;
                                curDis = Mathf.Clamp(curDis, 1, 3);
                                lastDis = dis;
                            
                        
                        else
                        
                            if (curDis > 1)
                            
                                curDis -= 1;
                                curDis = Mathf.Clamp(curDis, 1, 3);
                                lastDis = dis;
                            
                        
                    
                
            
        
        else
        
            var offsetX = _camera.ScreenToWorldPoint(Input.mousePosition).x - _camera.ScreenToWorldPoint(oriMousePos).x;
            var offsetY = _camera.ScreenToWorldPoint(Input.mousePosition).y - _camera.ScreenToWorldPoint(oriMousePos).y;
            _camera.transform.position = new Vector3(Mathf.Clamp(oriPos.x - offsetX, -4, 4), Mathf.Clamp(_camera.transform.position.y - offsetY, -2, 2), -10);
        
    

    private void LateUpdate()
    
        if (_camera.fieldOfView == curDis)  return; 
        _camera.orthographicSize = curDis;
    

以上是关于Unity 大地图滑动与缩放的主要内容,如果未能解决你的问题,请参考以下文章

关于Unity中UGUI图片Image实现仿视频播放窗口的四角缩放功能

unity怎么拖动ugui控件的中心点

unity UGUI的控件与UI组件组合后都有哪些妙用

unity UGUI怎么让 被拖拽的UI最后渲染

Unity UGUI 实现简单拖拽功能

unity UGUI为啥在父物体绑定拖拽脚本,子物体也能拖拽?