Problem with rect and touch

Hi.
due to unity’s GUI system not working great with multi-touch on android devices I had to make a work-around where I create 3 rect that I draw a texture in and check if the player is touching within that rect.
but for some reason. it only works with one of the buttons :confused:

The variables:
public Texture redButtonTexture;
public Texture greenButtonTexture;
public Texture blueButtonTexture;

public Rect redButtonRect;
public Rect greenButtonRect;
public Rect blueButtonRect;

Setting the value of the rects in Start()
redButtonRect = new Rect(0, colorButtonHeight * 2, Screen.width / 5, colorButtonHeight);

greenButtonRect = new Rect(0, colorButtonHeight * 3, Screen.width / 5, colorButtonHeight);

blueButtonRect = new Rect(0, colorButtonHeight * 4, Screen.width / 5, colorButtonHeight);

Drawing the texture in OnGUI()

GUI.DrawTexture(redButtonRect, redButtonTexture);

GUI.DrawTexture(greenButtonRect, greenButtonTexture);

GUI.DrawTexture(blueButtonRect, blueButtonTexture);

and checking for touch in the Update()

foreach (Touch touch in Input.touches) 
				{					
					if(redButtonRect.Contains(touch.position))	
					{
						Debug.Log ("red");
					}
					
					if(greenButtonRect.Contains(touch.position))	
					{
						Debug.Log ("green");
					}
					
					if(blueButtonRect.Contains(touch.position))	
					{
						Debug.Log ("blue");
					}
				}

For some weird reason, that I can’t explain. Then it’s only the Red button that works and I can’t get the green and blue buttons to work. no matter what I do…

It’s possible for me to place GUITexture onto the screen and use them but then I’d have to position them correctly every time the game starts so that the size and position fits the device…

does anyone have a solution to this problem? or can you see what I did wrong in my code?

Thanks

-Frank

There’s this weird thing where the screen position for GUI and for mouse input are reversed…

So for mouse input, (0,0) is bottom left, while for GUI, (0,0) is top left. I’d guess the red one just happens to be in the center of the screen, which is the same for both coordinates.

So you need to convert your input to the same coordinate system as the GUI before checking:

  foreach (Touch touch in Input.touches) 
      {             
          Vector3 inputGuiPosition = touch.position;
          inputGuiPosition.y = Screen.height - inputGuiPosition.y;

          if(redButtonRect.Contains(inputGuiPosition)) 
          {
             Debug.Log ("red");
          }

          if(greenButtonRect.Contains(inputGuiPosition))   
          {
             Debug.Log ("green");
          }

          if(blueButtonRect.Contains(inputGuiPosition))    
          {
             Debug.Log ("blue");
          }
      }