• 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 Bongmo · Feb 15, 2016 at 02:50 PM · gameobjectcanvasrecttransformrect

UI rect touch is not working?

  1. I create a new project

  2. I create a canvas

  3. I create a panel

  4. I change the panel rect transform in the inspector to stretch

So, it looks like image 1.

Image

Then I add t$$anonymous$$s code to the UI panel.

 private bool checkFirstTouch = true;

 void OnEnable()
 {
     checkFirstTouch = true;

 }

 void Update () {
     
     if (checkFirstTouch) {
     
         if (Input.GetMouseButton (0)) {    
             
             foreach (Touch touch in Input.touches) {
                 
                 if (gameObject.GetComponent<RectTransform>().rect.Contains(touch.position)) {
                     print ("Touched.");
                     
                     checkFirstTouch = false;
                 }
             }
             
         }            
         
     }

 }



My problem here is, that when I touch the screen on my mobile device, it's only working when I touch the left bottom corner. Please look image 2.

Why?

Why the whole screen is not working? Because the panel is stretched?

Must I multiply it with canvas scaler or what?

My canvas looks like t$$anonymous$$s:

[Canvas]: Screen Space Overlay

[CanvasScaler]:

UI Scale Mode: Scale with Screen Size

Reference Resolution: X: 800, Y: 480

Screen Match Mode: Match Width Or Height

Match: 0.5

I'm using Unity 4.7.0.

EDIT:

If I print "gameObject.GetComponent ().rect", I get t$$anonymous$$s:

(x:-400.00, y:-240.00, width:800.00, height:480.00)

u-10.png (4.5 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by valyard · Feb 15, 2016 at 03:24 PM

Well, you answered yourself (8
You have a (x:-400.00, y:-240.00, width:800.00, height:480.00) rect and you are trying to test a point w$$anonymous$$ch is inside a screen rect (x:0, y:0, width:800.00, height:480.00).
These 2 rects intersect exactly where you drew green rectangle on image 2.

I t$$anonymous$$nk you need either change your code to
gameObject.GetComponent<RectTransform>().rect.Contains(touch.position - new Vector2(Screen.width/2, Screen.height/2))
or move pivot on your panel so it would be in the corner.

Of course you also want to cache gameObject.GetComponent<RectTransform>() and new Vector2(Screen.width/2, Screen.height/2) in a variable.

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 Bongmo · Feb 15, 2016 at 03:52 PM 0
Share

It's a good help, but it's not working. I must check now, where the problem is.

It's now like this:

alt text

u-20.png (3.4 kB)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

39 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity 2D GameObject Movement.(C#) 0 Answers

Rect to RectTransform on overlay Canvas? 1 Answer

Canvas shrinks over time 0 Answers

GetComponent() Problem with getting value. 1 Answer

UI How to configure Left And Right. 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