8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

タッチパネルでタップ検知 マルチタッチ対応

Posted at
TouchPad.cs


public class TouchPad: MonoBehaviour {
	
	public float touchRectX;
	public float touchRectY;
	public float touchRectW;
	public float touchRectH;
	

	private Rect touchArea;
	private bool touching;
	private int touchId = -1;
	
	void Update() {
		
		int count = Input.touchCount;
	
		if ( count == 0 ) {
			touching = false;
			touchId = -1;
		} else {
			for(int i = 0;i < count; i++) {
				Touch touch = Input.GetTouch(i);
				
				if (touchArea.Contains(touch.position)
					&& (touchId == -1 || touchId != touch.fingerId)) {
	
					touchId = touch.fingerId;
					
				}
		
				if ( touchId == touch.fingerId ) {
					
					if ( touch.phase == TouchPhase.Ended 
						|| touch.phase == TouchPhase.Canceled ) {
						
						touching = false;				
						touchId = -1;		
					} else {
						touching = true;
					}
				}			
			}
		}
	}
	
	public bool GetTouch() {
		return touching;
	}

}


touchRectX,touchRectY,touchRectW,touchRectHは0から1までのfloat型で指定する。
画面左下が(0,0)となる。

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?