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 SleepingRobot · Oct 26, 2017 at 11:11 PM · camerauiui image

Get mouse click world coordinates "through" UI Raw Image with Render Texture from second camera

Greetings! I've seen a lot of similar questions posted for this, but I'm still struggling so thought I would post my own question.

I have a Render Texture that I am using as the Target Texture for an offscreen second camera. I am using a Canvas to render a UI menu, including a game object with a Raw Image using the aforementioned Render Texture to display the second camera's output in the UI.

I would like to be able to click on the UI Raw Image and get the mouse click coordinates in world space for where that click should be in relation to what was visible through the second camera.

I've tried using the UI EventSystem to get the event data for the mouse click, but I am not quite sure what to do with it next.

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class UICameraPanel : MonoBehaviour, IPointerClickHandler
 {
 
     public void OnPointerClick(PointerEventData eventData)
     {
         // need to do something with eventData
     }
 } 


My main and secondary cameras are both orthographic, if that matters. Any and all suggestions very much welcome! I have been banging my head against the wall on this one for longer than I'd care to admit.

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
4
Best Answer

Answer by SleepingRobot · Oct 29, 2017 at 04:48 PM

I was able to come up with a solution that gets the correct point within a few pixels, which is accurate enough for what I am trying to accomplish. Maybe someone else will find this useful in the future.

I start by getting the local coordinates of my render texture's rect

 Vector2 localClick;
 RectTransformUtility.ScreenPointToLocalPointInRectangle(textureRectTransform, eventData.position, Camera.main, out localClick);

I haven't looked into the reason yet, but it seems my render texture's Y axis is reversed (yMax is 0 and yMin is negative), so I un-reverse this effect on my local click coordinates

 localClick.y = (textureRectTransform.rect.yMin * -1) - (localClick.y * -1);

Next, I manually normalize my local rect coordinates to 0-1 viewport coordinates (correcting for the reverse Y axis again)

 Vector2 viewportClick = new Vector2(localClick.x / textureRectTransform.rect.xMax, localClick.y / (textureRectTransform.rect.yMin * -1));

Finally, I use the second camera to convert the viewport coordinates to world coordinates

 Vector2 worldClick = designerCamera.ViewportToWorldPoint(viewportClick);

While testing this out, I was spawning a 1x1 pink pixel sprite prefab at the world coordinates to visualize how close it looked to the UI click. It's still not perfect, but close enough for my current requirements

 GameObject clickPoint = Instantiate(pinkPixel);
 clickPoint.transform.position = worldClick;


Comment
Add comment · Show 2 · 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 jesse12521 · Apr 23, 2018 at 02:58 AM 0
Share

Was this for a 2D game? Or did you use Vector2 because of your orthographic cameras? How could you get the Vector3 coordinates?

avatar image Kirchesch jesse12521 · Mar 21, 2019 at 09:22 PM 0
Share

Hey bro you can just use new Vector3(worldClick.x, 0, worldClick.y) if your map is flat or you want to order a Nav$$anonymous$$eshAgent

avatar image
0

Answer by Adam_Benko · Jan 09, 2020 at 10:21 AM

Hi bro. Could you share please the entire script ? Not really sure how are you getting textureRectTransform, eventData.position

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 TwiiK · Apr 11, 2020 at 02:24 PM 1
Share

I just came across this post and made my own version based on the answer that @SleepingRobot gave, so here it is:

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class CameraScreen : $$anonymous$$onoBehaviour, IPointerClickHandler {
 
     public GameObject pinkPixel;
 
     public Camera playerCamera;
     private RectTransform _screenRectTransform;
 
     private void Awake() {
         _screenRectTransform = GetComponent<RectTransform>();
     }
 
     public void OnPointerClick(PointerEventData eventData) {
         RectTransformUtility.ScreenPointToLocalPointInRectangle(_screenRectTransform, eventData.position, null, out Vector2 localClick);
         localClick.y = (_screenRectTransform.rect.y$$anonymous$$in * -1) - (localClick.y * -1);
 
         Vector2 viewportClick = new Vector2(localClick.x / _screenRectTransform.rect.x$$anonymous$$ax, localClick.y / (_screenRectTransform.rect.y$$anonymous$$in * -1));
 
         Ray ray = playerCamera.ViewportPointToRay(new Vector3(viewportClick.x, viewportClick.y, 0));
         if (Physics.Raycast(ray, out RaycastHit hit)) {
             GameObject clickPoint = Instantiate(pinkPixel);
             clickPoint.transform.position = hit.point;
         }
     }
 }

$$anonymous$$y scenario was that I have my main camera rendering to a render texture that I display in a raw image in my UI and I wanted clicks on that raw image to tranfer into the game world. This seems to work perfectly from what I can tell.

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

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

Issues rotating a UI image while the canvas is parented to the Main Camera 0 Answers

Unity 2D not rendering images in Canvas 1 Answer

Is it possible to configure raycast to hit opaque pixels without using alphaHitTestMinimumThreshold? 1 Answer

Canvas Screen Space Issue 1 Answer

Why can't I see my full canvas? 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