Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Raycast UI raw image that has a render texture

Discussion in 'Scripting' started by jma00049, Jun 8, 2021.

  1. jma00049

    jma00049

    Joined:
    Feb 21, 2019
    Posts:
    89
    Hello, I have a Screen Space - Overlay canvas with a Raw Image UI object that has a render texture. That render texture image comes from a top camera that is pointing to a box grid. What I want to do is to click on a box to change it's color. How can I do it? I leave you here a screenshot of the canvas in game.

    Thanks!

     
    Last edited: Jun 8, 2021
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    I've done this before. I don't have the code handy but the gist is:

    • Use RecttransformUtility and the mouse position to figure out the normalized (0-1) coordinates of the mouse within the Raw image's Recttransform.
    • Use Camera.ViewportPointToRay with those coordinates to do a normal be raycast against your grid.
     
    jma00049 likes this.
  3. Nefisto

    Nefisto

    Joined:
    Sep 17, 2014
    Posts:
    324
    Why you can't simply add a button component to each box?
     
  4. jma00049

    jma00049

    Joined:
    Feb 21, 2019
    Posts:
    89
    If I add a button component, Will I be able to interact with it through the render texture?
     
  5. jma00049

    jma00049

    Joined:
    Feb 21, 2019
    Posts:
    89
    I will try to code what you said. Thanks!
     
  6. jma00049

    jma00049

    Joined:
    Feb 21, 2019
    Posts:
    89
    I found a solution. I post it here:
    Code (CSharp):
    1. public class RenderTextureToWorld : MonoBehaviour, IPointerClickHandler
    2. {
    3.  
    4.     public Camera gridCamera; //Camera that renders to the texture
    5.     private RectTransform textureRectTransform; //RawImage RectTransform that shows the RenderTexture on the UI
    6.  
    7.     private void Awake()
    8.     {
    9.         textureRectTransform = GetComponent<RectTransform>(); //Get the RectTransform
    10.     }
    11.  
    12.     public void OnPointerClick(PointerEventData eventData)
    13.     {
    14.         //I get the point of the RawImage where I click
    15.         RectTransformUtility.ScreenPointToLocalPointInRectangle(textureRectTransform, eventData.position, null, out Vector2 localClick);
    16.         //My RawImage is 700x700 and the click coordinates are in range (-350,350) so I transform it to (0,700) to then normalize
    17.         localClick.x = (textureRectTransform.rect.xMin * -1) - (localClick.x * -1);
    18.         localClick.y = (textureRectTransform.rect.yMin * -1) - (localClick.y * -1);
    19.  
    20.         //I normalize the click coordinates so I get the viewport point to cast a Ray
    21.         Vector2 viewportClick = new Vector2(localClick.x / textureRectTransform.rect.size.x, localClick.y / textureRectTransform.rect.size.y);
    22.  
    23.         //I have a special layer for the objects I want to detect with my ray
    24.         LayerMask layer = LayerMask.GetMask("Region");
    25.  
    26.         //I cast the ray from the camera which rends the texture
    27.         Ray ray = gridCamera.ViewportPointToRay(new Vector3(viewportClick.x, viewportClick.y, 0));
    28.  
    29.         if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, layer))
    30.         {
    31.  
    32.             Debug.Log(hit.collider.gameObject.name);
    33.         }
    34.     }
    35. }
    I found part of the solution here: https://answers.unity.com/questions/1426388/get-mouse-click-world-coordinates-through-ui-raw-i.html but I had to do some changes to make it work on my project.

    Thanks you all for the help!
     
  7. Nefisto

    Nefisto

    Joined:
    Sep 17, 2014
    Posts:
    324
    If you mean this, so yes

    capture2.gif

    To future reference only:
    Code (CSharp):
    1. public class UIBox : MonoBehaviour
    2. {
    3.     public Color colorToChange = Color.blue;
    4.  
    5.     public void Start()
    6.     {
    7.         // You can make this by inspector too, but from here you can add methods with any kind of parameter
    8.         GetComponent<Button>().onClick.AddListener(() => ChangeColor(colorToChange));
    9.     }
    10.  
    11.     private void ChangeColor (Color color)
    12.         => GetComponent<RawImage>().color = color;
    13. }
     
    jma00049 likes this.
  8. jma00049

    jma00049

    Joined:
    Feb 21, 2019
    Posts:
    89
    That's a really good solution too. Thank you!
     
    Nefisto likes this.
  9. vanceagrig

    vanceagrig

    Joined:
    Jul 13, 2018
    Posts:
    4