Unity之触屏控制_实现模型旋转和缩放—Touch类的使用
2016-08-12 00:09
555 查看
如果我们在Unity中实现物体的放大和旋转等需要触屏控制的功能,可以使用类Touch,可以理解为屏幕触点。
首先让我们看一看API中的定义:Structure describing the status of a finger touching the screen,大体意思是,描述手指接触屏幕状态的结构体。
它的主要变量有:
Touch.phase:Describes the phase of the touch.,即描述触摸的阶段(阶段主要分分为三种:Began(手指刚接触屏幕),move(手指在屏幕上移动),end(手指离开屏幕))。
Touch.position:当前触点的位置,返回的是一个二维向量。
注1:了解当前屏幕触点数目可以雕用:Input.TouchCount.
注2:获得屏幕的触点可以调用:Input.getTouch(int index);index为接触屏幕的触点编号(从0开始计)
现在让我们实现物体的放大和旋转功能:
using UnityEngine;
using System.Collections;
public class rotateand_scale : MonoBehaviour {
private Vector2 oldPo1;
private Vector2 oldPo2;
private Vector2 newPo1;
private Vector2 newPo2;
private Touch touch1;
private Touch touch2;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//单点触碰旋转
if(Input.touchCount==1)
{
touch1 = Input.GetTouch(0);
Vector2 slip = touch1.deltaPosition;
this.transform.Rotate(new Vector3(slip.y, slip.x),Space.World);
}else if(Input.touchCount==2) //多点触碰放大或缩小
{
touch1 = Input.GetTouch(0);
touch2 = Input.GetTouch(1);
//记录两个手指都触屏时的位置
if(touch1.phase == TouchPhase.Began||touch2.phase==TouchPhase.Began)
{
oldPo1 = touch1.position;
oldPo2 = touch2.position;
}
//随手指的移动变换物体的大小
if(touch1.phase==TouchPhase.Moved&&touch2.phase==TouchPhase.Moved)
{
newPo1 = touch1.position;
newPo2 = touch2.position;
float distance1 = Vector2.Distance(oldPo1, oldPo2);
float distance2 = Vector2.Distance(newPo1, newPo2);
float scale = distance2 / distance1;
if(scale<0.3)
{
scale = 0.3f;
} else if(scale>2)
{
scale = 2.0f;
}
this.transform.localScale *= scale;
}
}
}
}
注:space类 操作的空间,官方:The coordinate space in which to operate.
space.Word : Applies transformation relative to the world coordinate system.指游戏里面的空间系统
space.self: : Applies transformation relative to the local coordinate system. 本地的空间系统。
首先让我们看一看API中的定义:Structure describing the status of a finger touching the screen,大体意思是,描述手指接触屏幕状态的结构体。
它的主要变量有:
Touch.phase:Describes the phase of the touch.,即描述触摸的阶段(阶段主要分分为三种:Began(手指刚接触屏幕),move(手指在屏幕上移动),end(手指离开屏幕))。
Touch.position:当前触点的位置,返回的是一个二维向量。
注1:了解当前屏幕触点数目可以雕用:Input.TouchCount.
注2:获得屏幕的触点可以调用:Input.getTouch(int index);index为接触屏幕的触点编号(从0开始计)
现在让我们实现物体的放大和旋转功能:
using UnityEngine;
using System.Collections;
public class rotateand_scale : MonoBehaviour {
private Vector2 oldPo1;
private Vector2 oldPo2;
private Vector2 newPo1;
private Vector2 newPo2;
private Touch touch1;
private Touch touch2;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//单点触碰旋转
if(Input.touchCount==1)
{
touch1 = Input.GetTouch(0);
Vector2 slip = touch1.deltaPosition;
this.transform.Rotate(new Vector3(slip.y, slip.x),Space.World);
}else if(Input.touchCount==2) //多点触碰放大或缩小
{
touch1 = Input.GetTouch(0);
touch2 = Input.GetTouch(1);
//记录两个手指都触屏时的位置
if(touch1.phase == TouchPhase.Began||touch2.phase==TouchPhase.Began)
{
oldPo1 = touch1.position;
oldPo2 = touch2.position;
}
//随手指的移动变换物体的大小
if(touch1.phase==TouchPhase.Moved&&touch2.phase==TouchPhase.Moved)
{
newPo1 = touch1.position;
newPo2 = touch2.position;
float distance1 = Vector2.Distance(oldPo1, oldPo2);
float distance2 = Vector2.Distance(newPo1, newPo2);
float scale = distance2 / distance1;
if(scale<0.3)
{
scale = 0.3f;
} else if(scale>2)
{
scale = 2.0f;
}
this.transform.localScale *= scale;
}
}
}
}
注:space类 操作的空间,官方:The coordinate space in which to operate.
space.Word : Applies transformation relative to the world coordinate system.指游戏里面的空间系统
space.self: : Applies transformation relative to the local coordinate system. 本地的空间系统。
相关文章推荐
- 008-实现Unity3d中使用LeanTouch插件进行触屏控制(拖拽、移动、缩放旋转...)
- 使用向量点积来实现将模型绕着中心点旋转
- 【练习4.7】使用键盘控制透视变换和仿射变换的变换矩阵:实现拉伸、收缩、扭曲、旋转
- 使用PCL配合cvFindExtrinsicCameraParams2实现棋盘格控制模型的三维运动
- Unity通过射线碰撞选择模型,然后控制模型的移动、旋转、缩放等动作
- 在Unity中通过触摸屏幕实现模型的旋转和缩放
- Unity 3D 中实现对物体 位置(position) 旋转(rotation) 大小(scale) 的全面控制
- Unity3D For iPhone游戏引擎之使用脚本实现模型平移与模型旋转(六)
- Unity 移动端实现pad 端acceleration 控制移动、旋转
- 使用HoloToolKit实现导航手势旋转3D模型
- 手势检测实现相册的左右滑动(并加上移动与旋转的特效) 首先,activity_main.xml里,使用ViewFlipper组件(可使用动画控制多个组件之间的切换效果) <?xml version="
- 使用FormView Unity和LINQ实现的3层架构----MasterDataManage模型
- unity使用鼠标控制移动、缩放、旋转
- unity3d鼠标滑动UIImage从而实现3D模型在UIImage上的左右旋转(RawImage映射)
- 使用向量点积来实现将模型绕着中心点旋转
- 【Unity3D API的学习与使用】Unity实现GUI组件的位移、缩放和旋转
- unity使用vuforia实现模型AR效果
- 在Unity中通过触摸屏幕实现模型的旋转和缩放
- unity中用鼠标控制物体实现缩放、平移、旋转
- 基于角色-功能-资源的权限控制模型的设计与实现-引子