Unity UI and Render Texture (Pixel Perfect)
I have a problem achieving pixel perfection while using Unity UI:
I have a setup where I render my game to a texture and then render the texture to a quad that is scaled appropriately, so I achieve a sharp, pixel perfect game. (Same approach as https://www.youtube.com/watch?v=1czpscg9gC0)
Now this works great for the game itself, but I'm having major problems when it comes to using Unity's UI functions.
If I place the UI on the game world camera, the UI is rendered pixel perfectly, but I can't interact with the controls (which makes sense I guess).
If I place the UI on top of the pixel perfect camera, I loose the pixel perfection.
How are you dealing with UI in pixel perfect games? Should I just disregard Unity UI and build my own custom implementation or is there a way?
I'm going to add a comment here, because it seems I've solved it the day I asked this question after wrestling with it for weeks. It seems that the key here is providing a custom Raycaster derived from GraphicRaycaster for the canvas, that: 1) Raycasts against the Quad containing the render texture. 2) From the Raycast find the uv coordinates. 3) Using ViewportToScreenPoint with the uv coordinates to generate a new screenpoint on the "world camera" 4) Pass the transformed screen position to the base GraphicRaycaster:
public override void Raycast (PointerEventData eventData, System.Collections.Generic.List<RaycastResult> resultAppendList)
{
RaycastHit hit;
if (!Physics.Raycast(pixelCamera.ScreenPointToRay(eventData.position), out hit))
return;
PointerEventData pointer = new PointerEventData(EventSystem.current);
pointer.position = base.eventCamera.ViewportToScreenPoint(hit.textureCoord);
base.Raycast(pointer, resultAppendList);
}