Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 TurboHermit · Jul 27, 2017 at 04:20 PM · raycastinterfaceuv coordinatestexturecoord

Getting UV coordinate with GraphicRaycaster

Hey everyone,

I'm trying (and failing) to use the GraphicRaycaster to get the UV coordinate of an UI Image at my cursor position in a similar fashion as RaycastHit.textureCoord. I can't seem to find anything resembling such information in RaycastResult.

I want to use the UV coordinates found by clicking on a render texture as input for ViewportPointToRay and I used to do this with a Physics.Raycast, however I've recently switched out that mesh with a UI element.

Anyone ever had a similar problem?

Thanks in advance

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
1

Answer by nawash · Jul 24, 2018 at 01:24 PM

Same problem here... :(

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

Answer by udunadmin · Feb 18, 2021 at 03:09 PM

Hi @TurboHermit!

This is was posted long time ago, but this is a solution I've found using the RectTransformUtility:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 
 public class Test : MonoBehaviour
 {
     private GraphicRaycaster graphicRaycaster;
     private EventSystem eventSystem;
     private PointerEventData eventData;
 
     void Start() {
         //The GraphicRaycaster component in the Canvas
         graphicRaycaster = GetComponent<GraphicRaycaster>();
         //The EventSystem in the Scene
         eventSystem = GameObject.Find("EventSystem").GetComponent<EventSystem>();
     }
     
     void Update()
     {
         if (Input.GetMouseButtonDown(0)) {
             //Create a PointerEventData to pass in the Raycast
             eventData = new PointerEventData(eventSystem);
             eventData.position = Input.mousePosition;
             //Create a RaycasResult list to pass in the Raycast
             List<RaycastResult> results = new List<RaycastResult>();
 
             graphicRaycaster.Raycast(eventData, results);
             
             foreach (RaycastResult result in results)
             {
                 //Check if you hitted the image
                 if (result.gameObject.name == "your image name") {
                     Vector2 localPosition; //the local position in the image Rect coordinates
                     RectTransform rectTransform = result.gameObject.GetComponent<RectTransform>();
                     
                     //This obtains the position of the pointer in the image rect coordinates
                     RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, 
                     eventData.position, Camera.main, out localPosition);
 
                     Debug.Log(localPosition);
                 }
             }
 
             
         }
     }
 }
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 mylastggeast · Oct 12, 2021 at 11:49 AM 0
Share

Works like a charm! Thanks for sharing.

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

96 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 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 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

RaycastHit.textureCoord ranges from (0,0) to (1,1) for each face. 2 Answers

Multi Coloured Buttons 0 Answers

Find TextureCoordinates from plane with out raycast only using mouse position on plan 0 Answers

Canvas UI - Get Pixel Colour from Image 0 Answers

Why does my Mesh Collider RayCastHit only return a texturecoord on Unity Remote? 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