Unity2d:根据手指拖动和拉伸移动位置或改变大小

60 篇文章 2 订阅
9 篇文章 1 订阅

适用于2d相机的拖动移动和缩放拉伸,其中有位置和大小的最大最小限制

文中用到的GameManager在这篇文章:https://blog.csdn.net/qq_39332021/article/details/106625199

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

public class MapMove : MonoBehaviour
{
    private Camera camera;
    private Vector3 lastclickpos, lastmappos;
    private Vector3 nextpos, v;
    private Vector2 lastLeftHandPos, lastRightHandPos;
    private float handDistancke,lastHandDistance;
    private bool mapmove;
    // Start is called before the first frame update
    void Start()
    {
        camera = GetComponent<Camera>();
    }

    // Update is called once per frame
    void Update()
    {
        if (GameManager.Main.GetDoubleClick())
        {
            lastLeftHandPos = Input.GetTouch(0).position;
            lastRightHandPos = Input.GetTouch(1).position;
            lastHandDistance = Vector2.Distance(lastLeftHandPos, lastRightHandPos);
        }

        if (GameManager.Main.GetDoubleClickUp())
        {
            if (Input.GetTouch(0).phase == TouchPhase.Ended && Input.GetTouch(1).phase == TouchPhase.Ended)
            {
                mapmove = false;
                GameManager.Main.canchange = true;
            }

            if (Input.GetTouch(0).phase == TouchPhase.Ended)
            {
                lastclickpos = Input.GetTouch(1).position;
            }
            else
            {
                lastclickpos = Input.GetTouch(0).position;
            }

            lastmappos = transform.position;
        }

        if (GameManager.Main.GetDoubleClickAlways())
        {
            if (Input.GetTouch(0).phase == TouchPhase.Ended || Input.GetTouch(1).phase == TouchPhase.Ended)
            {
                goto one;
            }

            if (Input.GetTouch(0).phase == TouchPhase.Began || Input.GetTouch(1).phase == TouchPhase.Began)
            {
                goto one;
            }

            handDistancke = Vector2.Distance(Input.GetTouch(0).position, Input.GetTouch(1).position);
            if (handDistancke < lastHandDistance && camera.orthographicSize < 10)
            {
                camera.orthographicSize -= (handDistancke - lastHandDistance) / 500.0f;
            }
            else if (handDistancke > lastHandDistance && camera.orthographicSize > 2)
            {
                camera.orthographicSize -= (handDistancke - lastHandDistance) / 500.0f;
            }

            lastLeftHandPos = Input.GetTouch(0).position;
            lastRightHandPos = Input.GetTouch(1).position;
            lastHandDistance = Vector2.Distance(lastLeftHandPos, lastRightHandPos);
            return;
        }

        one:
        if (GameManager.Main.GetClick())
        {
            mapmove = true;
            lastclickpos = GameManager.Main.GetNumPos();
            lastmappos = transform.position;
        }

        if (GameManager.Main.GetClickUp())
        {
            mapmove = false;
            GameManager.Main.canchange = true;
        }


        if (mapmove)
        {
            if (Application.platform != RuntimePlatform.WindowsEditor)
            {
                if (Input.GetTouch(0).phase == TouchPhase.Ended)
                {
                    return;
                }

                if (Input.GetTouch(0).phase == TouchPhase.Began)
                {
                    return;
                }
            }

            nextpos = lastmappos + ((lastclickpos - GameManager.Main.GetNumPos()) / 1080.0f * camera.orthographicSize);
            v = nextpos - transform.position;
            if (Vector3.Distance(GameManager.Main.GetNumPos(), lastclickpos) > 1.0f)
            {
                GameManager.Main.canchange = false;
            }

            if (transform.position.x - camera.orthographicSize/2 < -10.0f && v.x < 0)
            {
                v.x = 0;
            }

            if (transform.position.x + camera.orthographicSize/2 > 10.0f && v.x > 0)
            {
                v.x = 0;
            }

            if (transform.position.y - camera.orthographicSize/2 < -10.0f && v.y < 0)
            {
                v.y = 0;
            }

            if (transform.position.y + camera.orthographicSize/2 > 10.0f && v.y > 0)
            {
                v.y = 0;
            }

            transform.position += v;
        }
    }
}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值