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 Rasmus Schlnsen · Nov 09, 2010 at 08:48 AM · raycastmouseguitexture

Get texture coordinates of mouse when clicking on GUITexture

Can anyone help me with getting the texture coordinates when clicking a GUITexture ?

if tried the following raycast method with no succes on a GUITexture

void OnMouseUp() { // Only when interactive is enabled if (!interactive) return;

 RaycastHit hit;
 if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
 {
     int x = /*width -*/ (int)(hit.textureCoord.x * width);
     int y = height - (int)(hit.textureCoord.y * height);          
 }

}

I'm currently working on in-game browser rendered on a GUITexture. I got some working (See github repo) but I need some mouse support!

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
1

Answer by Bampf · Nov 09, 2010 at 12:39 PM

Raycast only hits when there is a Collider component on the object.

According to the documentation, it has to be a mesh collider as well.

Typically this isn't used with GUITextures, but with 3d models. GUITextures aren't in the scene but rather, they float above it facing the camera. So I don't know of a good way to size a mesh collider for it properly without getting into some serious math.

Happily there is an easier way. Since a GUITexture is rectangular, all you need to know are the on-screen dimensions. Then you can compute what part of the image the mouse is over. Call GetScreenRect to get the screen rectangle of the GUITexture. Use it to convert the mouse's screen coordinates into the texture coordinates.

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

[Solved]Cursor emulation with canvas? 0 Answers

(C#) 2D Raycast wrong direction 0 Answers

How to prevent GameObject from spawning on top of each other? 1 Answer

Drag Object Via Axis Handles 0 Answers

Raycast with strange Behaviour 0 Answers


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