[Unity3D]Touch&Event

关于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;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值