Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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 this 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 this:

[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 this:

(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 which 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 think 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.

Follow this Question

Answers Answers and Comments

40 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 avatar image

Related Questions

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

Rect to RectTransform on overlay Canvas? 1 Answer

GetComponent() Problem with getting value. 1 Answer

Canvas shrinks over time 0 Answers

Is it possible to change Rect Transform Scale throw script 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