Unity中Touch类的简单介绍

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class TouchDemo : MonoBehaviour
{
    public Text infoText;
    private string info;
    private void Update()
    {
        //每当一个手指触摸屏幕时,unity都会围棋生成一个touch对象
        //Input.touchCount 是可以获取当前Touch对象的个数
        if (Input.touchCount > 0)
        {
            info = string.Empty;
            info += "touchCount" + Input.touchCount + "\n";
            //unity会将当前存在的所有Touch对象放在Input.touches数组里面
            //另一种方式获取指定index的Touch对象:Input.GetTouch(index)

            Touch myTouch = Input.touches[0];

            //myTouch.fingerId是用来识别当前手指的唯一表示

            info += "fingerId:" + myTouch.fingerId + "\n";

            //deltaPosition当前位置与上次位置之间的差

            info += "deltaPosition:" + myTouch.deltaPosition + "\n";

            //deltaTime本次记录Touch对象状态与上次记录Touch状态之间的时间差

            info += "deltaTime:" + myTouch.deltaTime + "\n";

            //Touch对象的生命周期的结束并不是手指离开屏幕后立刻销毁
            //如果一根手指在同一位置快速点击,侧视作同一Touch对象
            //tapCount为Touch对象所对应的手指点击屏幕的次数

            info += "tapCount:" + myTouch.tapCount + "\n";

            //phase表示当前手指锁对应的Touch对象阶段(状态)
            //Began 开始状态
            //Moved 移动状态
            //Stationary  不动状态
            //Ended 结束状态
            //Canceled 取消状态
            info += "phase:" + myTouch.phase + "\n";
        }
        infoText.text = info;
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值