Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Dr. Watson · Sep 27, 2010 at 03:37 PM · iphoneonguirecttouches

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bampf · Sep 27, 2010 at 10:30 PM

  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.)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dr. Watson · Sep 28, 2010 at 03:39 PM 0
Share

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

avatar image
0

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
}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

iphone OnGUI define and test within rect?? 0 Answers

HELP first person controller setup iphone/android 2 Answers

HUD on Android/iPhone : GUITexture or OnGUI (drawTexture) ? 2 Answers

Separate functions for separate touches 1 Answer

Unity iPhone : black rect when I turn the iPhone 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges