【Unity】【Code】Unity+触摸屏 单点旋转,双点放大缩小
2017-08-14 18:19
337 查看
通用代码库
基于Unity5.6.0f
基于Unity5.6.0f
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Collections; using UnityEngine.UI; public class Touch : MonoBehaviour { public Text txt; public EventDispatcher evt; float distance = 0;//触控缩放的距离 private float lastDist = 0;//用于计算触控缩放 private float curDist = 0;//用于计算触控缩放 int t;//判断缩放触控 private Vector3 pos1; private Quaternion pos2; private void Start() { pos1 = this.gameObject.transform.localScale; pos2 = this.gameObject.transform.rotation; } // Update is called once per frame void Update() { //单点触摸, 水平上下旋转 if (1 == Input.touchCount) { var touch = Input.GetTouch(0); ShowTxt("touch: " + touch.position); if (touch.position.y > Screen.height * 0.25f && touch.position.y < Screen.height * 0.93f) { Vector2 deltaPos = touch.deltaPosition; transform.Rotate(Vector3.down * deltaPos.x * 0.05f, Space.World); transform.Rotate(Vector3.right * deltaPos.y * 0.05f, Space.World); } } //两点以上触控,且触控点发生移动 if ((Input.touchCount > 1) && (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)) { var touch1 = Input.GetTouch(0); //第一根手指 var touch2 = Input.GetTouch(1); //第二根手指 ShowTxt("touch1: " + touch1.position); ShowTxt("touch2: " + touch2.position); if (touch1.position.y > Screen.height * 0.25f && touch1.position.y < Screen.height * 0.93f && touch2.position.y > Screen.height * 0.25f && touch2.position.y < Screen.height * 0.93f) { curDist = Vector2.Distance(touch1.position, touch2.position);//两指间距 //当手指移动时,重置起始距离为当前距离 if (t == 0) { lastDist = curDist; t = 1; } distance = curDist - lastDist; if (this.gameObject.transform.localScale.x >= 1 && this.gameObject.transform.localScale.x < this.gameObject.transform.localScale.x*20) { this.gameObject.transform.localScale += Vector3.one * distance * 0.05f * Time.deltaTime; } else { this.gameObject.transform.localScale = new Vector3(1, 1, 1); } } lastDist = curDist; } //没有触控事件 if (Input.touchCount == 0) t = 0; } private void ShowTxt(string info) { txt.text = info + "\r\n" + txt.text; } }
相关文章推荐
- unity 鼠标拖拽物体旋转,滚轮放大缩小(一)
- unity中通过收拾实现物体旋转和放大缩小
- unity触摸放大缩小旋转
- Unity 控制摄像机旋转、放大、缩小
- Unity 模型在移动端进行移动、旋转和放大(缩小)
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- 用QMatrix实现帧的旋转、翻转、放大、缩小等简单操作示例
- Android实现旋转,放大,缩小图片的方法
- IOS代码添加控件,控件移动,放大,缩小,旋转
- cocos2d各种动作的使用(变色、跳动、旋转、闪烁、悬挂、放大缩小、渐变、animation)
- 初识matlab及图像处理之图片平移、旋转、水平镜像、垂直镜像、放大、缩小 (1)
- 关于手势问题的一些问题,图片旋转或者放大缩小,,需要指定锚点进行旋转或者缩放
- 实现一个简单的按钮实例-上下左右,左旋转,右旋转,放大缩小。
- 放大、缩小、旋转、开始动画
- iOS CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- #iOS开发笔记#UIImageView实现拖动,放大/缩小,旋转
- 简单的旋转,放大,缩小 (HTML5)
- 【CODE】DOM、Jquery、D3.js字体放大缩小效果
- Android opengl 正方形 手势三维旋转、缩小、放大
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)