- Home /
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
Answer by Bampf · Sep 27, 2010 at 10:30 PM
- 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.
- You probably are only interested in touches whose phase = iPhoneTouchPhase.Began or iPhoneTouchPhase.Ended.
- 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.)
Thanks for the reply! I just made my own slider with GUI textures and scripted the functionality. $$anonymous$$y issue was when using a GUI Skin vertical slider it didn't support multi-touches and since I can't see the code that the GUI Skins use to detect when it's being touched...when I would try to move my character with touchpads and then use the slider at the same time, the slider wouldn't work! However, I really appreciate your reply! XD
Answer by ash1405 · Feb 01, 2011 at 07:21 AM
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
}