Restricting OnGUI to test for touches inside of a RECT???(Unity iPhone)

Is this even possible??? All I want to do is have the OnGUI() test for touches inside a RECT or only part of the screen... How can I do this if at all possible? Using Unity iPhone Thanks

  1. Get the iPhoneTouch from the array: iPhoneInput.touches. (For your purposes, you can probably take the first one unless you want to handle multi-touch.
  2. You probably are only interested in touches whose phase = iPhoneTouchPhase.Began or iPhoneTouchPhase.Ended.
  3. Compare the touch's position to the Rect.

However, touch coordinates are inverted when compared to GUI coordinates. So you'll need to subtract the y-position from Screen.height.

If the rectangle you are testing is inside a GUI Window or ScrollView, you will also need to adjust by the origin of those elements. For example, a rect with coordinates 50,50 inside a window at position 50,50 is going to appear on the screen at 100,100.

I recently worked through an example on my blog that you might find helpful. It's doing more than you need, but the core problem was the same: mixing touches with UnityGUI elements. Section two talks about adjusting the tap coordinates. I compute which row of a scrolling list was clicked on, but you would just need to check the (adjusted) tap coordinates against the rectangle.

(Minor note: I'm basing this on Unity iPhone 1.7. in Unity 3, iPhoneInput and iPhoneTouchPhase have been renamed. You will get warnings if you use them. The warnings are harmless, and they also tell you exactly what to rename for future reference.)

Hello, If any one requires this, a simple solution is

For web player


Rect rct=new Rect(60, 140, 200, 200);//rect to negate
Vector3 v=Input.mousePosition;
v.y=480-v.y;//this is done as GUI origin is top left and clicks are based on bottom-left
if(!rct.Contains(v)){//if the mouse click is outside your rect
   //do your stuff
}

for iphone


Rect rct=new Rect(60, 140, 200, 200);
Vector2 touchDeltaPosition = Input.GetTouch(0).position;
Vector3 v=new Vector3(touchDeltaPosition.x, touchDeltaPosition.y, 0.0f);
v.y=480-v.y;
if(!rct.Contains(v)){//if the touch is outside your rect
   //do your stuff
}