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)となる。