素材巴巴 > 程序开发 >

Unity手势,单指移动,双指旋转缩放

程序开发 2023-09-13 20:21:43

Unity手势,单指移动,双指旋转缩放

今天记录一份代码,unity自带的手势做一点简单的入门级的操作
在这里插入图片描述

上代码

using UnityEngine;
 using UnityEngine.UI;
 /// 
 /// 点击屏幕实现缩放与旋转,移动
 /// 
 public class EasyTouchManager5_Demo : MonoBehaviour
 {private Touch oldTouch1;  //上次触摸点1(手指1)  private Touch oldTouch2;  //上次触摸点2(手指2)  //这是场景里的文本,我打log用的,不用可以删掉public Text _text;//****************************private string text;//****************************void Update(){//没有触摸  if (Input.touchCount <= 0){//Debug.Log("Input.touchCount = 0");return;}单点触摸, 水平上下移动if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved){Debug.Log("单点触摸, 水平上下移动");text = "单点触摸, 水平上下移动";//****************************_text.text = text;//****************************var deltaposition = Input.GetTouch(0).deltaPosition;transform.Translate(new Vector3(deltaposition.x * 0.01f, deltaposition.y * 0.01f, 0f),Space.World);}//单点触摸, 水平上下旋转  if (2 == Input.touchCount){Debug.Log("单点触摸, 水平上下旋转  ");text = "单点触摸, 水平上下旋转";//****************************_text.text = text;//****************************Touch touch = Input.GetTouch(0);Vector2 deltaPos = touch.deltaPosition;transform.Rotate(Vector3.down * deltaPos.x, Space.World);transform.Rotate(Vector3.right * deltaPos.y, Space.World);}//多点触摸, 放大缩小  Touch newTouch1 = Input.GetTouch(0);Touch newTouch2 = Input.GetTouch(1);//第2点刚开始接触屏幕, 只记录,不做处理  if (newTouch2.phase == TouchPhase.Began){oldTouch2 = newTouch2;oldTouch1 = newTouch1;return;}//计算老的两点距离和新的两点间距离,变大要放大模型,变小要缩放模型  float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);//两个距离之差,为正表示放大手势, 为负表示缩小手势  float offset = newDistance - oldDistance;//放大因子, 一个像素按 0.01倍来算(100可调整)  float scaleFactor = offset / 100f;Vector3 localScale = transform.localScale;Vector3 scale = new Vector3(localScale.x + scaleFactor,localScale.y + scaleFactor,localScale.z + scaleFactor);//最小缩放到 0.3 倍  if (scale.x > 0.3f && scale.y > 0.3f && scale.z > 0.3f){transform.localScale = scale;}//记住最新的触摸点,下次使用  oldTouch1 = newTouch1;oldTouch2 = newTouch2;}
 }
 

#不过还是有些缺陷的,旋转和缩放会同时进行,后变有时间我再更新一下

提示:脚本挂在被操作物体哦

2020年9月9日:做了一版更新,解决了双指状态物体闪动的问题

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;public class TouchScreen : MonoBehaviour
 {private Touch oldTouch1;  //上次触摸点1(手指1)  private Touch oldTouch2;  //上次触摸点2(手指2)  Touch newTouch1;Touch newTouch2;// Start is called before the first frame updatevoid Start(){}// Update is called once per frame void Update(){//没有触摸  if (Input.touchCount <= 0){return;}       //单点拖动else if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved){//var deltaposition = Input.GetTouch(0).deltaPosition;transform.Translate(new Vector3(deltaposition.x * 0.01f, deltaposition.y * 0.01f, 0f), Space.World);}//双指旋转  && 缩放else  if (2 <= Input.touchCount){//Debug.Log("单点触摸, 水平上下旋转  ");Touch touch = Input.GetTouch(0);Vector2 deltaPos = touch.deltaPosition;//位置增量if (Mathf.Abs(deltaPos.x) >=3 || Mathf.Abs(deltaPos.y) >= 3){transform.Rotate(Vector3.down * deltaPos.x, Space.World);//绕y轴旋转transform.Rotate(Vector3.right * deltaPos.y, Space.World);//绕x轴}//缩放newTouch1 = Input.GetTouch(0);//newTouch2 = Input.GetTouch(1);if (newTouch2.phase == TouchPhase.Began){oldTouch2 = newTouch2;oldTouch1 = newTouch1;return;}float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);//计算两点距离float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);//计算两点距离float offset = newDistance - oldDistance;if (Mathf.Abs(offset)>=3){Debug.Log(offset);//放大因子, 一个像素按 0.01倍来算(100可调整)  float scaleFactor = offset / 100f;Vector3 localScale = transform.localScale;Vector3 scale = new Vector3(localScale.x + scaleFactor,localScale.y + scaleFactor,localScale.z + scaleFactor);//最小缩放到 0.3 倍 最大1.5倍 if (scale.x > 0.3f && scale.x < 1.5f){transform.localScale = scale;//赋新值}//记住最新的触摸点,下次使用  oldTouch1 = newTouch1;oldTouch2 = newTouch2;}}}
 }
 

标签:

素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。