如何在 Unity 2D 和 3D 中放大或缩小以及点击屏幕

目标 下面的示例代码的主要目的是为了解释如何在 Unity 中实现缩放即以点击屏幕的功能。

1. 介绍

在示例代码中,实现了放大或缩小和点击功能。在手机的图库中,缩放和平移/拖动图像时,它具有相同的行为。此示例代码对 unity2d 和 unity3d 对象都起作用。它独立于摄像机;无论主摄像机是 tk2d 摄像机简单的摄像机以及它的投影类型是正交或是透视

2. 例子Example

下面的 c# 代码实现的是,你可以通过两个手指来放大和缩小屏幕以及在图片上点击移动。

注意 在你想要实现缩放功能的游戏物体上添加下面的脚本。

2.1 PinchZoomScript

using UnityEngine;
using System.Collections;
 
public class PinchZoom : MonoBehaviour
{
    private float scale_factor= 0.07f;  
    private float MAXSCALE = 6.0f, MIN_SCALE = 0.6f; // zoom-in and zoom-out limits
    private bool isMousePressed;
    private Vector2 prevDist = new Vector2(0,0);
    private Vector2 curDist = new Vector2(0,0);
    private Vector2 midPoint = new Vector2(0,0);
    private Vector2 ScreenSize;
    private Vector3 originalPos;
    private GameObject parentObject;
    
    void Start ()
    {
        // Game Object will be created and make current object as its child (only because we can set virtual anchor point of gameobject and can zoom in and zoom out from particular position)
        parentObject = new GameObject("ParentObject");
        parentObject.transform.parent = transform.parent;
        parentObject.transform.position = new Vector3(transform.position.x*-1, transform.position.y*-1, transform.position.z);
        transform.parent = parentObject.transform;
 
        ScreenSize = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width,Screen.height));
        originalPos = transform.position;
        isMousePressed = false;
    }
    void Update ()
    {
        if(Input.GetMouseButtonDown(0))
            isMousePressed = true;
        else if(Input.GetMouseButtonUp(0))
            isMousePressed = false;
        // These lines of code will pan/drag the object around untill the edge of the image
        if(isMousePressed && Input.touchCount==1 && Input.GetTouch(0).phase == TouchPhase.Moved && (parentObject.transform.localScale.x > MIN_SCALE || parentObject.transform.localScale.y > MIN_SCALE))
        {
            Touch touch = Input.GetTouch(0);    
            Vector3 diff = touch.deltaPosition*0.1f;    
            Vector3 pos = transform.position + diff;
            if(pos.x > ScreenSize.x * (parentObject.transform.localScale.x-1))
                pos.x = ScreenSize.x * (parentObject.transform.localScale.x-1);
            if(pos.x < ScreenSize.x * (parentObject.transform.localScale.x-1)*-1)
                pos.x = ScreenSize.x * (parentObject.transform.localScale.x-1)*-1;
            if(pos.y > ScreenSize.y * (parentObject.transform.localScale.y-1))
                pos.y = ScreenSize.y * (parentObject.transform.localScale.y-1);
            if(pos.y < ScreenSize.y * (parentObject.transform.localScale.y-1)*-1)
                pos.y = ScreenSize.y * (parentObject.transform.localScale.y-1)*-1;
            transform.position = pos;
        }
        // On double tap image will be set at original position and scale
        else if(Input.touchCount==1 && Input.GetTouch(0).phase == TouchPhase.Began && Input.GetTouch(0).tapCount==2)
        {
            parentObject.transform.localScale = Vector3.one;
            parentObject.transform.position = new Vector3(originalPos.x*-1, originalPos.y*-1, originalPos.z);
            transform.position = originalPos;
        }    
        checkForMultiTouch();
    }
    // Following method check multi touch
    private void checkForMultiTouch()
    {
        // These lines of code will take the distance between two touches and zoom in - zoom out at middle point between them
        if (Input.touchCount == 2 && Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved)
        {
            midPoint = new Vector2(((Input.GetTouch(0).position.x + Input.GetTouch(1).position.x)/2), ((Input.GetTouch(0).position.y + Input.GetTouch(1).position.y)/2));
            midPoint = Camera.main.ScreenToWorldPoint(midPoint);
            
            curDist = Input.GetTouch(0).position - Input.GetTouch(1).position; //current distance between finger touches
            prevDist = ((Input.GetTouch(0).position - Input.GetTouch(0).deltaPosition) - (Input.GetTouch(1).position - Input.GetTouch(1).deltaPosition)); //difference in previous locations using delta positions
            float touchDelta = curDist.magnitude - prevDist.magnitude;
            // Zoom out
            if(touchDelta>0)
            {
                if(parentObject.transform.localScale.x < MAXSCALE && parentObject.transform.localScale.y < MAXSCALE)
                {
                    Vector3 scale = new Vector3(parentObject.transform.localScale.x + scale_factor, parentObject.transform.localScale.y + scale_factor, 1);
                    scale.x = (scale.x > MAXSCALE) ? MAXSCALE : scale.x;
                    scale.y = (scale.y > MAXSCALE) ? MAXSCALE : scale.y;
                    scaleFromPosition(scale,midPoint);
                }
            }
            //Zoom in
            else if(touchDelta<0)
            {
                if(parentObject.transform.localScale.x > MIN_SCALE && parentObject.transform.localScale.y > MIN_SCALE)
                {
                    Vector3 scale = new Vector3(parentObject.transform.localScale.x + scale_factor*-1, parentObject.transform.localScale.y + scale_factor*-1, 1);
                    scale.x = (scale.x < MIN_SCALE) ? MIN_SCALE : scale.x;
                    scale.y = (scale.y < MIN_SCALE) ? MIN_SCALE : scale.y;
                    scaleFromPosition(scale,midPoint);
                }
            }
        }
        // On touch end just check whether image is within screen or not
        else if (Input.touchCount == 2 && (Input.GetTouch(0).phase == TouchPhase.Ended || Input.GetTouch(0).phase == TouchPhase.Canceled || Input.GetTouch(1).phase == TouchPhase.Ended || Input.GetTouch(1).phase == TouchPhase.Canceled))
        {
            if(parentObject.transform.localScale.x < 1 || parentObject.transform.localScale.y < 1)
            {
                parentObject.transform.localScale = Vector3.one;
                parentObject.transform.position = new Vector3(originalPos.x*-1, originalPos.y*-1, originalPos.z);
                transform.position = originalPos;
            }
            else
            {
                Vector3 pos = transform.position;
                if(pos.x > ScreenSize.x * (parentObject.transform.localScale.x-1))
                    pos.x = ScreenSize.x * (parentObject.transform.localScale.x-1);
                if(pos.x < ScreenSize.x * (parentObject.transform.localScale.x-1)*-1)
                    pos.x = ScreenSize.x * (parentObject.transform.localScale.x-1)*-1;
                if(pos.y > ScreenSize.y * (parentObject.transform.localScale.y-1))
                    pos.y = ScreenSize.y * (parentObject.transform.localScale.y-1);
                if(pos.y < ScreenSize.y * (parentObject.transform.localScale.y-1)*-1)
                    pos.y = ScreenSize.y * (parentObject.transform.localScale.y-1)*-1;
                transform.position = pos;
            }
        }
    }
    //Following method scales the gameobject from particular position
    static Vector3 prevPos = Vector3.zero;
    private void scaleFromPosition(Vector3 scale, Vector3 fromPos)
    {
        if(!fromPos.Equals(prevPos))
        {
            Vector3 prevParentPos = parentObject.transform.position;
            parentObject.transform.position = fromPos;    
            Vector3 diff = parentObject.transform.position - prevParentPos;
            Vector3 pos = new Vector3(diff.x/parentObject.transform.localScale.x*-1, diff.y/parentObject.transform.localScale.y*-1, transform.position.z);
            transform.localPosition = new Vector3(transform.localPosition.x + pos.x, transform.localPosition.y+pos.y, pos.z);
        }
        parentObject.transform.localScale = scale;
        prevPos = fromPos;
    }
}

如果你查到了任何关于如何在 Unity 2D 和 3D 中缩放以及点击屏幕?,请提交你的问题,我们会尽力解决它。

有了一个游戏开发的主意?你还在等什么?现在就联系我们吧!你会看到这个想法很快就实现了。我们的公司被看作是在印度最好的游戏开发公司之一

原帖地址

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏阿凯的Excel

或关系求均值(函数虐心版)

最近醉心于Python的学习和分享,好久没有分享Excel相关的文章了。 熟悉我文章的朋友,都知道我特喜欢分享数组函数,也特喜欢分享那种很长的函数。 前几天有朋...

3836
来自专栏数据结构与算法

P1325 雷达安装

题目描述 描述: 假设海岸线是一条无限延伸的直线。它的一侧是陆地,另一侧是海洋。每一座小岛是在海面上的一个点。雷达必须安装在陆地上(包括海岸线),并且每个雷达都...

3366
来自专栏新工科课程建设探讨——以能源与动力工程专业为例

5.2.1 二维导热算例-热导的概念

材料类,描述材料的参数,如密度、比热和初始温度等,这里特别给出了凝固潜热;这里要注意Math.pow(2,0)的意义,读者自己琢磨,用于判断相邻控制体的...

1010
来自专栏数据结构与算法

BZOJ2216: [Poi2011]Lightning Conductor(DP 决策单调性)

首先把给出的式子移项,我们要求的$P_i = max(a_j + \sqrt{|i - j|}) - a_i$。

1272
来自专栏安恒网络空间安全讲武堂

由suctf一道题学到的中国余子式定理

1143
来自专栏AzMark

贝斯狸的 Python 之旅 -- 深入切片操作及原理

我首先通过 input() 函数,接收了外部输入字符串,然后通过 list 函数的切片,实现了回文数,代码真的好简洁,我自己都佩服我自己,我也不知道小组长会问...

1023
来自专栏AI科技大本营的专栏

你一定能看懂的算法基础书(代码示例基于Python)

本文引自图灵教育《算法图解》 你一定能看懂的算法基础书;代码示例基于Python;400多个示意图,生动介绍算法执行过程;展示不同算法在性能方面的优缺点;教会...

5227
来自专栏有趣的Python

c++实战-(一)-走出迷宫

走出迷宫 走出规则: 左手规则 & 右手规则 原则:保证手始终触墙 结果:走出迷宫 情况1:有入有出。(特殊情况,出入口是一个) ? 架构描述: 迷宫类(Maz...

2984
来自专栏互联网开发者交流社区

SQL基础日期函数

1475
来自专栏大数据挖掘DT机器学习

【手把手教你做项目】自然语言处理:单词抽取/统计

作者 白宁超 成都信息工程大学硕士。 近期关注数据分析统计学、机器学习。 原文:http://www.cnblogs.com/baiboy/p/zryy1.h...

32913

扫码关注云+社区

领取腾讯云代金券