ue3d在Unity中使用鼠标滚动或触摸进行缩放

在Unity中使用鼠标滚动或触摸进行缩放

分类:
ue3d - 在Unity中使用鼠标滚动或触摸进行缩放

本文3D天堂将介绍在Unity中如何使用鼠标滚动或触摸进行缩放。

现在将使用下面的场景作为示例。

ue3d - 在Unity中使用鼠标滚动或触摸进行缩放

使用鼠标滚动或触摸缩放

将下面给定的脚本附加到Main Camera GameObject,为了获得缩放效果,下面的脚本根据鼠标或触摸输入更改相机的正交视野。

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

public class ZoomBehaviour : MonoBehaviour
{
    float MouseZoomSpeed = 15.0f;
    float TouchZoomSpeed = 0.1f;
	float ZoomMinBound = 0.1f;
	float ZoomMaxBound = 179.9f;
    Camera cam;

    // Use this for initialization
    void Start()
    {
        cam = GetComponent<Camera>();
    }

    void Update()
    {
        if (Input.touchSupported)
        {
            // Pinch to zoom
            if (Input.touchCount == 2)
            {
				// get current touch positions
                Touch tZero = Input.GetTouch(0);
                Touch tOne = Input.GetTouch(1);
				// get touch position from the previous frame
				Vector2 tZeroPrevious = tZero.position - tZero.deltaPosition;
				Vector2 tOnePrevious = tOne.position - tOne.deltaPosition;

				float oldTouchDistance = Vector2.Distance (tZeroPrevious, tOnePrevious);
				float currentTouchDistance = Vector2.Distance (tZero.position, tOne.position);

				// get offset value
				float deltaDistance = oldTouchDistance - currentTouchDistance;
				Zoom (deltaDistance, TouchZoomSpeed);
            }
        }
        else
        {
            float scroll = Input.GetAxis("Mouse ScrollWheel");
            Zoom(scroll, MouseZoomSpeed);
        }
    }

    void Zoom(float deltaMagnitudeDiff, float speed)
    {
        cam.fieldOfView += deltaMagnitudeDiff * speed;
        // set min and max value of Clamp function upon your requirement
		cam.fieldOfView = Mathf.Clamp(cam.fieldOfView, ZoomMinBound, ZoomMaxBound);
    }
}

现在Unity运行应用程序并使用鼠标滚动按钮进行缩放。

ue3d - 在Unity中使用鼠标滚动或触摸进行缩放

构建适用于android平台的项目并将其安装在android设备中,目前已经用android Marshmallow对其进行了测试,并且缩放效果很好。

ue3d - 在Unity中使用鼠标滚动或触摸进行缩放

以上是3D天堂关于使用鼠标滚动或触摸缩放的全部内容,如果你有任何反馈,请随时在本页面下方留言。

相关信息

  • 类型:知识
  • 字数:210
  • 字符:2144
  • 适用软件:Unity
  • 说明:无
  • 编号:156011

热门内容

提示:3D天堂作为服务提供者,尊重网络版权及知识产权,对某些行为的发生不具备充分的监控能力,若无意间侵犯到您的权利,请 联系我们,我们会在收到信息后尽快给予处理。

本站文章版权归本站自创作者所有,未经允许不得转载!