开启辅助访问
 找回密码
 注册帐号

扫一扫,访问微社区

首页   >   博客   >   ID丶sunshine

关于移动端的手势控制,脚本打写

个人分类: 移动端控制 | 2018-1-11 10:12
如下:
using UnityEngine;
using System.Collections;

public class Onclickspeed : MonoBehaviour {
private Vector2 oldPosition;

private Touch oldTouch1;  

/// 上次触摸点2(手指2)
private Touch oldTouch2;  

      float oldDis = 0;

     float  newDis = 0;

     float scaler = 0;
// Update is called once per frame
void Update ()
    { 
if (Input.touchCount == 1)
{   //单指移动
if (Input.GetTouch (0).phase == TouchPhase.Began) 
{  
oldPosition = Event.current.mousePosition;  
}  
if (Input.GetTouch (0).phase == TouchPhase.Moved)
{  
Vector2 tempPosition = Event.current.mousePosition;  
Vector3 body = transform.localScale;  
                  
float x = tempPosition.x - oldPosition.x;  
float y = tempPosition.y - oldPosition.y;  
  

transform.localPosition  += (new Vector3 (x / 200, -y / 200, 0)); 
 
oldPosition = tempPosition;  
  
}
}
if (Input.touchCount > 1)
{

//多点触摸, 放大缩小

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);

oldDis = oldDistance;

newDis = newDistance;
//两个距离之差,为正表示放大手势, 为负表示缩小手势

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);

scaler = scaleFactor;




//允许模型最小缩放到 0.5 倍最大放大2倍

if (scale.x > 0.5f && scale.y > 0.5f && scale.z > 0.5f)
{
//实用差值运算,模型平滑缩放
transform.localScale = Vector3.Lerp(transform.localScale,new Vector3(Mathf.Clamp(localScale.x + scaleFactor, 0.5f,2f), 

Mathf.Clamp(localScale.y + scaleFactor, 0.5f, 2f),

Mathf.Clamp(localScale.z + scaleFactor, 0.5f, 2f)),1f);
}

if ( newTouch1.phase == TouchPhase.Moved || newTouch2.phase == TouchPhase.Moved)  //双指平移进行旋转
{  
float MM = Mathf.Abs(newTouch1.position.x - oldTouch1.position.x);  
float NN = Mathf.Abs (newTouch1.position.y - oldTouch1.position.y);
if(MM-NN > 0)
{
transform.Rotate (new Vector3 (0,(newTouch1.position.x - oldTouch1.position.x)/1.5f,0));

}

}

//记住最新的触摸点,下次使用

oldTouch1 = newTouch1;

oldTouch2 = newTouch2;
}

}
}
0 0

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册帐号

个人分类

标签

阅读排行

评论排行

推荐博客

最新博客

返回顶部