I’ve been having this issue for a while - OnGui seemingly supports only one touch at once, so I cant do something like make player controls.
My other options would be writing a script for a GuiTexture or 3d Object, but I can’t use these as I’m developing for Android and the controls must be responsive. How can I still use OnGui and support multiple touches?
Sometimes I just wish I could use HTML and CSS
Updated code:
void Update ()
{
if (Input.touchCount > 0)
{
for (int i=0; i < Input.touchCount; i++)
{
Touch touch = Input.GetTouch(i);
Vector2 vec = touch.position;
if(rightArrowRect.Contains(vec))
{
rightArrow = true;
}
else
{
rightArrow = false;
}
if(leftArrowRect.Contains(vec))
{
leftArrow = true;
}
else
{
leftArrow = false;
}
}
}
else //what to set on no touch
{
rightArrow = false;
leftArrow = false;
}
}
@piedoom, you can use TouchPhase to change behaviors based on what phase the detected touch is in. Here is an example using the code in the answer as a starting point:
if (Input.touchCount > 0){
for (int i=0; i < Input.touchCount; i++) {
Touch touch = Input.GetTouch(i);
Vector2 vec = touch.position;
if(rect.Contains(vec)
{
if(touch.TouchPhase == TouchPhase.Began)
{
// Do something when the touch
// starts
}
else if (touch.TouchPhase == TouchPhase.Ended)
{
// Do something when the finger
// is lifted off the screen
}
}
}
}
Here is the docs link for more info on touch phases: TouchPhase
Also, if you want to test touch from your mobile device you should check this out and see if it works for you: