IOS&Android通用屏幕控制(移动旋转缩放)
2016-07-18 12:50
519 查看
using UnityEngine;
using System.Collections;
public class SimpleRotateAndMove : MonoBehaviour {
private float time;
private bool isRotating=false ;
private Vector3 m_endpos;
//记录上一次手机触摸位置判断用户是在左放大还是缩小手势
private Vector2 oldPosition1 ;
private Vector2 oldPosition2 ;
private float distance;
//缩放限制
// float yMinLimit = -20;
// float yMaxLimit = 80;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
if (Input.touchCount == 1)
{
time += Time.deltaTime;//当只有一个手指按屏幕时,开始计时
//print ("只有一个手指按屏幕时,开始计时");
if (Input.touches[0].phase == TouchPhase.Ended && Input.touches[0].phase != TouchPhase.Canceled)
{
//canceled的意思是系统取消跟踪触摸,如果用户把屏幕放到他的脸上或超过五个触摸点时的状态
m_endpos = Input.touches[0].position;
time = 0;//将时间重置为0
}
}
if (Input.touches != null && Input.touches.Length > 0)
{//排除手指为空的情况
if (time > 2f)
{//移动
transform.Translate(Vector3.right * 0.01f * Input.touches[0].deltaPosition.x, Space.World);
transform.Translate(Vector3.forward * 0.01f * Input.touches[0].deltaPosition.y, Space.World);
}
else
{//否则手指点击屏幕拖动时是旋转该物体
transform.Rotate(Vector3.up, -1 * Input.touches[0].deltaPosition.x);
}
}
if (Input.touchCount > 1)
{
//前两只手指触摸类型都为移动触摸
if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
{
//计算出当前两点触摸点的位置
var tempPosition1 = Input.GetTouch(0).position;
var tempPosition2 = Input.GetTouch(1).position;
//函数返回真为放大,返回假为缩小
if (isEnlarge(oldPosition1, oldPosition2, tempPosition1, tempPosition2))
{
//放大系数超过3以后不允许继续放大
//这里的数据是根据我项目中的模型而调节的,大家可以自己任意修改
//if (distance > 3)
//{
// distance -= 0.5f;
//}
this.transform.localScale *= 1.1f;
}
else
{
//缩小系数返回18.5后不允许继续缩小
//这里的数据是根据我项目中的模型而调节的,大家可以自己任意修改
//if (distance < 18.5)
//{
// distance += 0.5f;
//}
this.transform.localScale *= 0.9f;
}
//备份上一次触摸点的位置,用于对比
oldPosition1 = tempPosition1;
oldPosition2 = tempPosition2;
}
}
}
//函数返回真为放大,返回假为缩小
bool isEnlarge(Vector2 oP1 , Vector2 oP2 , V
a492
ector2 nP1 , Vector2 nP2 )
{
//函数传入上一次触摸两点的位置与本次触摸两点的位置计算出用户的手势
var leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
var leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
if(leng1<leng2)
{
//放大手势
return true;
}else
{
//缩小手势
return false;
}
}
//Update方法一旦调用结束以后进入这里算出重置摄像机的位置
void LateUpdate()
{
////target为我们绑定的箱子变量,缩放旋转的参照物
//if (this.gameObject)
//{
// //重置摄像机的位置
// y = ClampAngle(y, yMinLimit, yMaxLimit);
// var rotation = Quaternion.Euler(y, x, 0);
// var position = rotation * Vector3(0.0, 0.0, -distance) + this.gameObject.position;
// transform.rotation = rotation;
// transform.position = position;
//}
}
}
//这个脚本实现的是左右滑动屏幕旋转物体,但手机触摸屏幕超过两秒时是移动该物体,两个手指可实现缩放,具体数值可自行调节
using System.Collections;
public class SimpleRotateAndMove : MonoBehaviour {
private float time;
private bool isRotating=false ;
private Vector3 m_endpos;
//记录上一次手机触摸位置判断用户是在左放大还是缩小手势
private Vector2 oldPosition1 ;
private Vector2 oldPosition2 ;
private float distance;
//缩放限制
// float yMinLimit = -20;
// float yMaxLimit = 80;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
if (Input.touchCount == 1)
{
time += Time.deltaTime;//当只有一个手指按屏幕时,开始计时
//print ("只有一个手指按屏幕时,开始计时");
if (Input.touches[0].phase == TouchPhase.Ended && Input.touches[0].phase != TouchPhase.Canceled)
{
//canceled的意思是系统取消跟踪触摸,如果用户把屏幕放到他的脸上或超过五个触摸点时的状态
m_endpos = Input.touches[0].position;
time = 0;//将时间重置为0
}
}
if (Input.touches != null && Input.touches.Length > 0)
{//排除手指为空的情况
if (time > 2f)
{//移动
transform.Translate(Vector3.right * 0.01f * Input.touches[0].deltaPosition.x, Space.World);
transform.Translate(Vector3.forward * 0.01f * Input.touches[0].deltaPosition.y, Space.World);
}
else
{//否则手指点击屏幕拖动时是旋转该物体
transform.Rotate(Vector3.up, -1 * Input.touches[0].deltaPosition.x);
}
}
if (Input.touchCount > 1)
{
//前两只手指触摸类型都为移动触摸
if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
{
//计算出当前两点触摸点的位置
var tempPosition1 = Input.GetTouch(0).position;
var tempPosition2 = Input.GetTouch(1).position;
//函数返回真为放大,返回假为缩小
if (isEnlarge(oldPosition1, oldPosition2, tempPosition1, tempPosition2))
{
//放大系数超过3以后不允许继续放大
//这里的数据是根据我项目中的模型而调节的,大家可以自己任意修改
//if (distance > 3)
//{
// distance -= 0.5f;
//}
this.transform.localScale *= 1.1f;
}
else
{
//缩小系数返回18.5后不允许继续缩小
//这里的数据是根据我项目中的模型而调节的,大家可以自己任意修改
//if (distance < 18.5)
//{
// distance += 0.5f;
//}
this.transform.localScale *= 0.9f;
}
//备份上一次触摸点的位置,用于对比
oldPosition1 = tempPosition1;
oldPosition2 = tempPosition2;
}
}
}
//函数返回真为放大,返回假为缩小
bool isEnlarge(Vector2 oP1 , Vector2 oP2 , V
a492
ector2 nP1 , Vector2 nP2 )
{
//函数传入上一次触摸两点的位置与本次触摸两点的位置计算出用户的手势
var leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
var leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
if(leng1<leng2)
{
//放大手势
return true;
}else
{
//缩小手势
return false;
}
}
//Update方法一旦调用结束以后进入这里算出重置摄像机的位置
void LateUpdate()
{
////target为我们绑定的箱子变量,缩放旋转的参照物
//if (this.gameObject)
//{
// //重置摄像机的位置
// y = ClampAngle(y, yMinLimit, yMaxLimit);
// var rotation = Quaternion.Euler(y, x, 0);
// var position = rotation * Vector3(0.0, 0.0, -distance) + this.gameObject.position;
// transform.rotation = rotation;
// transform.position = position;
//}
}
}
//这个脚本实现的是左右滑动屏幕旋转物体,但手机触摸屏幕超过两秒时是移动该物体,两个手指可实现缩放,具体数值可自行调节
相关文章推荐
- Android APK安装与卸载机制
- Android 中轴时光轴
- Android动画的分类和简单的使用方法
- Android中 WebView与Js的交互1
- android中TextView分段显示不同颜色,字体,时间
- android中TextView分段显示不同颜色,字体,时间
- android中TextView分段显示不同颜色,字体,时间
- android中TextView分段显示不同颜色,字体,时间
- android中TextView分段显示不同颜色,字体,时间
- Android — Communicating with Other Fragments
- TextView属性大全
- TextView属性大全
- TextView属性大全
- TextView属性大全
- TextView属性大全
- android的消息处理机制(图+源码分析)——Looper,Handler,Message
- Android 自定义Toast
- AndroidStudio安装与Genymotion模拟器
- android蓝牙-isMultipleAdvertisementSupported(API21)判断支持不支持BLE Peripheral
- Android中的基础----android:gravity和android:layout_gravity的使用原则和区别