Unity 获取手机触摸方法
程序员文章站
2022-04-03 09:13:09
...
转自:https://blog.csdn.net/Czhenya/article/details/78237265
触屏函数写在Update()中,用于实时监测,,方法很好用我做2048的时候用过了,,,下面是示例代码,分享给大家,,,
//有触摸点,且滑动
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
int dieX = 0;
int dieY = 0;
//获取滑动的距离 Input.GetTouch(0).deltaPosition
Vector2 touchDelPos = Input.GetTouch(0).deltaPosition;
//比较两个方向滑动的绝对值的大小,,那个大,认为在那个方向在滑动
if(Mathf.Abs(touchDelPos.x)> Mathf.Abs(touchDelPos.y))
{
//滑动距离,,这个值很灵敏,注意不要设置的太小
if (touchDelPos.x > 10)
{
dieX = 1;
}//X方向的作用滑动
else if(touchDelPos.x < -10)
{
dieX = -1;
}
}
else
{
if (touchDelPos.y > 10)
{
dieY = 1;
}
else if (touchDelPos.y < -10)
{
dieY = -1;
}
}
}
上面的代码等同于在Update中写,按下A,W,S,D,,键是一样的,,,,
//PC 端测试
int dieX = 0;
int dieY = 0;
if (Input.GetKeyDown(KeyCode.A))
{
dieX = -1;
}
else
if (Input.GetKeyDown(KeyCode.D))
{
dieX = 1;
}
else
if (Input.GetKeyDown(KeyCode.W))
{
dieY = 1;
}
else
if (Input.GetKeyDown(KeyCode.S))
{
dieY = -1;
}
放大缩小的处理
public class Gesture : MonoBehaviour {
private Touch oldTouch1; //上次触摸点1(手指1)
private Touch oldTouch2; //上次触摸点2(手指2)
void Update()
{
//没有触摸,就是触摸点为0
if (Input.touchCount <= 0){return;}
if(Input.touchCount >= 2){
//多点触摸, 放大缩小
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);
//在什么情况下进行缩放
if (scale.x >= 0.05f && scale.y >=0.05f && scale.z >= 0.05f)
{
transform.localScale = scale;
}
//记住最新的触摸点,下次使用
oldTouch1 = newTouch1;
oldTouch2 = newTouch2;
}
}
}
单击,双击,长按,旋转
//=============单击,双击==========
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
if (hitInfo.collider.gameObject.tag == "Target")
{
Debug.Log("射线检测是目标标签");
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began)
{
Debug.Log("单击");
if (Input.GetTouch(0).tapCount == 2)
{
Debug.Log("双击操作");
}
}
}
}
}
//===========长按===========
if (Input.GetMouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
if (hitInfo.collider.gameObject.tag == "Target")
{
Debug.Log("射线检测是目标标签");
if (Input.touchCount == 1)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
touchTime = Time.time;
touchDown = true;
}else if (Input.GetTouch(0).phase == TouchPhase.Stationary)
{
if (touchDown && touchTime - Time.time > 1f)
{
touchDown = false;
Debug.Log("长按");
}
}
else
{
touchDown = false;
}
}
}
}
}
//===========旋转==============
if (Input.GetMouseButton(0))
{
if(Input.touchCount == 1)
{
if(Input.GetTouch(0).phase == TouchPhase.Moved)
{
transform.Rotate(Vector3.up*Input.GetAxis("Mouse X")*Time.deltaTime*rotateSpeed,Space.World);
Debug.Log("模型旋转");
}
}
}
拖拽物体的脚本:
public class Drag : MonoBehaviour {
private Vector3 vec3TargetScreenSpace;// 目标物体的屏幕空间坐标
private Vector3 vec3TargetWorldSpace;// 目标物体的世界空间坐标
private Transform trans;// 目标物体的空间变换组件
private Vector3 vec3MouseScreenSpace;// 鼠标的屏幕空间坐标
private Vector3 vec3Offset;// 偏移
void Awake() { trans = transform; }
IEnumerator OnMouseDown()
{
// 把目标物体的世界空间坐标转换到它自身的屏幕空间坐标
vec3TargetScreenSpace = Camera.main.WorldToScreenPoint(trans.position);
// 存储鼠标的屏幕空间坐标(Z值使用目标物体的屏幕空间坐标)
vec3MouseScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, _vec3TargetScreenSpace.z);
// 计算目标物体与鼠标物体在世界空间中的偏移量
vec3Offset = _trans.position - Camera.main.ScreenToWorldPoint(vec3MouseScreenSpace);
// 鼠标左键按下
while (Input.GetMouseButton(0))
{
// 存储鼠标的屏幕空间坐标(Z值使用目标物体的屏幕空间坐标)
vec3MouseScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, vec3TargetScreenSpace.z);
// 把鼠标的屏幕空间坐标转换到世界空间坐标(Z值使用目标物体的屏幕空间坐标),加上偏移量,以此作为目标物体的世界空间坐标
vec3TargetWorldSpace = Camera.main.ScreenToWorldPoint(_vec3MouseScreenSpace) + _vec3Offset;
// 更新目标物体的世界空间坐标
trans.position = _vec3TargetWorldSpace;
// 等待固定更新
yield return new WaitForFixedUpdate();
}
}
}