unity学习27:用Input接口去监测: 单点触摸和多点触摸
目录
1 用Input去监测:触摸 touch
1.1 触摸分类:单点触摸和多点触摸
1.2 用Input.touches[] 获取触摸对象
1.3 获取触摸对象后,再获取触摸对象的属性
1.4 接着获取触摸对象的具体状态
2 触摸的代码今天没有测试,以后再说
1 用Input去监测:触摸 touch
1.1 触摸分类:单点触摸和多点触摸
- 单点触摸, if(Input.touchCount==1)
- 多点触摸, if(Input.touchCount==2) 等等
- 但是需要在 void start() 里要先开启允许多点触摸:Input.multiTouchEnabled=true;
1.2 用Input.touches[] 获取触摸对象
- 获得触摸对象
- 需要先 声明对象, Input.touches[0] 的返回值是一个Touch对象
- Touch touch1=Input.touches[0];
- 然后才能去使用 touch1对象
- Input.touches[0]
- Input.touches[1]
- 后面的序号是触摸的点
1.3 获取触摸对象后,再获取触摸对象的属性
- 接着获取触摸对象的属性:触摸位置
- Debug.Log(touch1.position);
1.4 接着获取触摸对象的具体状态
- TouchPhase.Began: //触摸开始时
- TouchPhase.Moved: //触摸移动
- TouchPhase.Stationary: //触摸静止
- TouchPhase.Ended: //触摸结束
- TouchPhase.Canceled: //被其他打断了
2 触摸的代码今天没有测试,以后再说
- 主要不好测试触摸
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestTouch : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
// 允许多点触摸
Input.multiTouchEnabled=true;
}
// Update is called once per frame
void Update()
{
//判断是否单点触摸
if(Input.touchCount==1)
{
//获得触摸对象
Touch touch1=Input.touches[0];
//接着获取触摸对象的属性:触摸位置
Debug.Log(touch1.position);
//接着获取触摸对象的具体状态
switch(touch1.phase)
{
case TouchPhase.Began:
break;
case TouchPhase.Moved:
break;
case TouchPhase.Stationary:
break;
case TouchPhase.Ended:
break;
case TouchPhase.Canceled:
break;
}
}
//判断是否两点触摸
if(Input.touchCount==2)
{
//获得触摸对象
Touch touch1=Input.touches[0];
Touch touch2=Input.touches[1];
//接着获取触摸对象的属性:触摸位置
Debug.Log(touch1.position);
Debug.Log(touch2.position);
//接着获取触摸对象的具体状态
switch(touch1.phase)
{
case TouchPhase.Began:
break;
case TouchPhase.Moved:
break;
case TouchPhase.Stationary:
break;
case TouchPhase.Ended:
break;
case TouchPhase.Canceled:
break;
}
}
}
}