美文网首页unityUnity3D学习笔记征服Unity3d
如何在 Unity 2D 和 3D 中放大或缩小以及点击屏幕

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

作者: hrscy | 来源:发表于2016-06-07 08:12 被阅读535次

目标
下面的示例代码的主要目的是为了解释如何在 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 中缩放以及点击屏幕?,请提交你的问题,我们会尽力解决它。

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

原帖地址

相关文章

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

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

  • 是时候该了解下Unity3D了

    现在的 Unity 已经发展到了 Unity 5,使用 Unity 可以创建 2D 或 3D 游戏、建筑格式化、实...

  • CSS3新特性

    写出如下 CSS3效果的简单事例 demo 圆角, 圆形 div 阴影 2D 转换:放大、缩小、偏移、旋转 3D ...

  • Python中的图像调整和裁剪工具

    Python中的图像调整和裁剪工具 Python图像调整和裁剪工具 网站如何使那些图标随你的屏幕大小而放大或缩小?...

  • Unity 2D和3D混合效果

    参考 网上找到的博客 资源 本篇内容 2D和3D混合效果 Unity软件版本 Unity2017.4.15 N...

  • 放大或缩小

    缩小与放大是两个不同的概念 对于网页来说,是有一个合适的阅读视角大小 当我们觉得屏幕字体小了 我们可以放大页面来观...

  • Unity Pro如何创建和使用脚本

    Unity Pro 2018 for mac是游戏开发必备的软件之一,unity mac版主要用于创建2D和3D跨...

  • iOS 中实现图片点击全屏预览

    点击单张图片的全屏预览和双指捏合缩小放大

  • 商超登录界面设计

    一、界面效果图 二、登录界面功能介绍1、登录界面出现在屏幕正中央,并不可被放大或缩小(效果图中可看到无放大缩小框)...

  • 商超登录界面设计

    一、界面效果图 二、登录界面功能介绍 1、登录界面出现在屏幕正中央,并不可被放大或缩小(效果图中可看到无放大缩小框...

网友评论

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

    本文链接:https://www.haomeiwen.com/subject/wyufdttx.html