关闭

Touch

1096人阅读 评论(0) 收藏 举报

Input.touchCountstatic int touchCount;
Description

Number of touches. Guaranteed not to change throughout the frame. (Read Only)

PC上不能触摸,所以这个值总是为0。在小米手机上,经本人测试,为触摸在屏幕上的手指数量。所以我们一般要在检测触摸事件前加上一句判断

if(Input.touchCount>0)
{
    if(Input.GetTouch(0).phase==TouchPhase.Began)
    {
	oldloc=Input.GetTouch(0).position;
	touched=true;
    }
}

在PC上若没有if(Input.touchCount>0)这句判断,由于没有触摸事件,所以Input.touchCount始终为0,Input.GetTouch(0)将出错。

Input.touches
static Touch[] touches;

Description

Returns list of objects representing status of all touches during last frame. (Read Only) (Allocates temporary variables).

Each entry represents a status of a finger touching the screen.

返回所有触摸点信息的列表。返回值是一个数组,该数组的长度与Input.touchCount相同。

 

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour {
    void Update() {
        int fingerCount = 0;
        foreach (Touch touch in Input.touches) {
            if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
                fingerCount++;
            
        }
        if (fingerCount > 0)
            print("User has " + fingerCount + " finger(s) touching the screen");
        
    }
}

Input.GetTouch

static Touch GetTouch(int index);
Description

Returns object representing status of a specific touch. (Does not allocate temporary variables).

返回Touch类,Touch类包含了对应index所对应的触摸状态信息。

Touch.fingerId

触摸的唯一标识。所有的触摸都在Input.touchs数组中,也可以使用Input.GetTouch函数和有效的数组索引来获取。然而,不能保证数组索引在两帧之间是相同的。fingerID却不同,它在不同帧之间总是指向同一个触摸。

All current touches are reported in theInput.touches array or by using theInput.GetTouch function with the equivalent array index. However, the array index is not guaranteed to be the same from one frame to the next. ThefingerID value, however, consistently refers to the same touch across frames. This ID value is very useful when analysing gestures and is more reliable than identifying fingers by their proximity to previous position, etc.

Touch.deltaPosition

自从上一次Update以来的触摸移动,可以用来实现游戏物体随触摸移动而一定。Touch.deltaTime则代表与delatPosition对应的时间段。触摸的绝对位置为Touch.position


	public GameObject panel;
	void Update()
	{
		if(Input.touchCount>0)
		{
			if(Input.GetTouch(0).phase==TouchPhase.Moved)
			{
				Vector2 deltaTouchDis=Input.GetTouch(0).deltaPosition;
				panel.transform.Translate(deltaTouchDis.x*0.01f,0,0);

The position delta since last change.

The absolute position of the touch is recorded periodically and available in theposition property. The deltaPosition value is a Vector2 that represents the difference between the touch position recorded on the most recent update and that recorded on the previous update. ThedeltaTime value gives the time that elapsed between the previous and current updates; you can calculate the touch's speed of motion by dividing deltaPosition.magnitude by deltaTime.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:144651次
    • 积分:2180
    • 等级:
    • 排名:第18069名
    • 原创:76篇
    • 转载:4篇
    • 译文:2篇
    • 评论:6条
    文章分类
    最新评论