关于Touch 和 Event 的使用记录
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TouchEvent : MonoBehaviour
{
// Use this for initialization
void Start()
{
if(Input.touchSupported){
Debug.Log("支持触摸");
}
if(Input.touchPressureSupported){
Debug.Log("支持压感");
}
if(Input.stylusTouchSupported){
Debug.Log("支持手写笔");
}
//Input.touches 返回touch列表 分配临时变量
//Input.touchCount 几点触摸
//Input.multiTouchEnabled 开启/关闭多点触摸
//Input.simulateMouseWithTouches 开启/关闭触摸模拟鼠标
//Input.GetTouch(0) 不分配临时变量
}
// Update is called once per frame
void Update()
{
if(Input.touchCount==1)Debug.Log("单击3");
if(Input.touchCount>1)Debug.Log("多点触摸");
//Input.GetTouch(0).phase==TouchPhase.Moved;
if(Input.touchCount>0||Input.GetMouseButtonDown(0)){
Debug.Log("-------左键按下------");
if(Input.GetTouch(0).phase==TouchPhase.Began){
}else if(Input.GetTouch(0).phase==TouchPhase.Moved){
Vector2 pos= Input.GetTouch(0).position;
Vector2 deltaPos= Input.GetTouch(0).deltaPosition;
float deltaTime=Input.GetTouch(0).deltaTime;
// Input.GetTouch(0).deltaPosition.x;//y magnitude sqrtMagnitude
Touch touch=Input.GetTouch(0);
TouchPhase phase=touch.phase;
TouchType type=touch.type;
}else if(Input.GetTouch(0).phase==TouchPhase.Stationary){
}
else if(Input.GetTouch(0).phase==TouchPhase.Ended){
}
}
}
Vector2 lastPos;
void OnGUI(){
if(Event.current.isKey){
Debug.Log("<color=yellow>Event.current.KeyCode:"+Event.current.keyCode.ToString()+"</color>");
Debug.Log("<color=cyan>Event.current.character:"+Event.current.character+"</color>");
if(Event.current.keyCode==KeyCode.A){
//判断按下哪个键 按下的是不是鼠标
Debug.Log("aaaaaaaaaaaa");
}
}
if(Event.current.isMouse){
// Event.current.clickCount;鼠标点击次数
Debug.Log("<color=green>Event.current.button:"+Event.current.button.ToString()+"</color>");
if(Event.current.type==EventType.MouseDown){
lastPos= Event.current.mousePosition;
}
if(Event.current.type==EventType.MouseDrag){
Vector2 curPos=Event.current.mousePosition;
Vector2 distance= Event.current.delta;
if(curPos.x<lastPos.x){
}else{
}
lastPos=curPos;
}
if(Event.current.type==EventType.MouseUp){
if(Event.current.button==0){
Debug.Log("======左键抬起========");
}
if(Event.current.button==1){
Debug.Log("======右键抬起========");
}
if(Event.current.button==2){
Debug.Log("======中键抬起========");
}
if(Event.current.button==3){
Debug.Log("======其他键抬起========");
}
}
}
if(Event.current.alt){
Debug.Log("按下alt");
}
if(Event.current.capsLock){
Debug.Log("打开大写锁定键");
}
if(Event.current.command){
Debug.Log("按下命令键");
}
if(Event.current.control){
Debug.Log("按下control键");
}
if(Event.current.functionKey){
Debug.Log("是功能键");
}
if(Event.current.shift){
Debug.Log("按下shift键");
}
if(Event.current.numeric){
Debug.Log("是小键盘键");
}
//Event.current.pressure
}
/**
* 判断是否为单点触摸
**/
public static bool singleTouch()
{
if(Input.touchCount==1)
return true;
return false;
}
/**
*判断是否为多点触摸
**/
public static bool multipointTouch()
{
if (Input.touchCount > 1)
return true;
return false;
}
}