Have you ever wanted to apply a full-screen effect to your GUI without having to completely restructuring the way you set up your OnGUI structure? Or how about rendering text and GUI controls onto in-scene virtual screens and objects? Yea me too.
So a couple of years ago I did a friday project investigating the possibility of a setup like this. What I wanted was a Doom 3 – style in-game numpad, responding to mouse clicks. Unfortunately I couldn’t get there 100% and in a nice and straight-forward way, so I abandoned it with no further time spent.
The specific problem was that while I could get my GUI to render to the object just fine, the GUI system had no way of injecting events or modifying existing ones. This meant that if I wanted GUI to respond to events, I would have to bypass the GUI event system and effectively rewrite the basic GUI controls. That failed my “nice and straight-forward” requirement.
However a while later, I came across a similar need: I had a project where the screen was warped slightly fish-eye, to give an impression of an old-school CRT display. It looked great, except the GUI was just rendered on top of everything – as per usual – with no warping. Since the GUI was non-interactive, there was no down-side to using the approach I had come up with before to include the GUI in the full-screen effect rendering.
It was a snap to apply, I had no required changes to my general OnGUI structure and the result looked great. This setup also came in handy when I was working with the Oculus Rift – rendering the GUI on a plane, rendered always on top, in front of the Oculus cameras.
So what is the idea then? Well it is quite straight forward: OnGUI is invoked multiple times each frame (once per event) – at least twice – unless layouting has been disabled on the MonoBehaviour. The last event of the frame is the repaint event. In this, all resolved controls render to the active context. Which fortunately can be set by render textures.
The trick therefore becomes:
Remember that this is not immediately useful if you need interactive GUI as this would require work to handle input gathered from the regular UnityEngine.Input API. Nonetheless it will give you for instance a scrolling screen of text on an in-game monitor.
I hope you find it useful!