GraphicRaycaster with Camera Space UI
I am trying to use the graphics raycaster, which operates on window space if I'm not mistaken, with a camera space UI so I can use the UI with a render texture. I already tried using 2D colliders on the UI but that doesn't handle overlapping very well. I am now asking for help, because my small brain just can't figure out how to make a GraphicRaycaster work with camera space UI. My code (actually just unity's example code modified) works fine with screen space UI if anyone is wondering.
Here are the important parts of the code:
m_PointerEventData = new PointerEventData(m_EventSystem);
m_PointerEventData.position = new Vector2(mouse.simCursorPos.x, mouse.simCursorPos.y);
List<RaycastResult> results = new List<RaycastResult>();
m_Raycaster.Raycast(m_PointerEventData, results);
Vector3 scPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, mouseCanvasDepth));
simCursorPos = new Vector2(scPos.x, scPos.y);
Thanks in advance
Graphic raycaster should be working in a Screen Space canvas without any extra code, are you trying to achieve an special behaviour?
Yes, but not at the moment. I am still trying to get it to work normally.
Answer by fabi090806 · Dec 18, 2020 at 02:14 PM
Here's the solution:
m_PointerEventData = new PointerEventData(m_EventSystem);
Vector2 cursorRaycasterPoint = new Vector2(
(mouse.simCursorPos.x / mouse.cursorCanvas.transform.localScale.x) + mouse.currentRes.x / 2 ,
(mouse.simCursorPos.y / mouse.cursorCanvas.transform.localScale.y) + mouse.currentRes.y / 2 );
m_PointerEventData.position = cursorRaycasterPoint;
List<RaycastResult> results = new List<RaycastResult>();
m_Raycaster.Raycast(m_PointerEventData, results);