Touch

3158 ワード

Input.touchCount
static 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クラスに戻り、indexに対応するタッチ状態情報を含む.
Touch.fingerId
タッチのユニークなマーク.すべてのタッチはInput.touchs配列ではInput.GetTouch関数と有効な配列インデックスを取得します.しかしながら、配列インデックスが2つのフレーム間で同じであることは保証されない.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. The fingerID 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 the position 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. The deltaTime 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.