Hello, I’m trying to implement my own GUI-System into Unity.

I don’t want to use OnGUI because of the well known performance issues.

Altough I have a license for EZGUI, i’d like to create my own system so i can learn something new.

As I see it, there are 2 Approaches:

  1. Using a second orthographic camera for the GUI and overlaying it on the main camera using a Render Texture.
  2. Keep everything in the Perspective Camera and calculating the correct Distance/Scale of the Object for it to be pixel perfect.

Most people seem to go for the 1st approach. Isn’t there quite a bit of Overhead in this, because of the render texture?

I prefer approach 2. I already made several tests and it works surprisingly well. I just calculate the correct Scale of the Object (plane) from the distance to camera (with some trigonometry).
This way the texture is pixel perfect (after moving it 0.5 pixels, took me forever to figure this one out…).
Even drag and drop works like a charm. For MouseOver- or Click-effects I just move the UV coordinates of the mesh (I don’t use Material Offset, so the Objects can still be batched).

I know this approach is quite a bit more complicated (and also more difficult to maintain) but I think performance-wise it’s better.

What are your thoughts?

Should I continue (I intend to code a full framework like EZGUI) like this or should I Switch to the Orthographic Approach with a RenderTexture and second camera. What are the Overheads of a Rendertexture?

Like I said in my comment, just use a second camera.

  1. Create a new camera, set it to be
    orthographic. (Also, remove any
    unneeded components such as
    GUILayer, FlareLayer, AudioListener)

  2. To maintain pixel-perfect set the
    orthographic size to be exactly one
    half of the screen height (ie if res
    is 1024x768, ortho size is 384)

  3. Create a new layer and call it “UI”
    or something - set the GUI Camera’s
    culling to cull only this new layer.

  4. Now on the clear flags of the
    camera, set that to “depth only”.

  5. Select the other camera, set it’s
    culling layers to not cull “UI”.

  6. Now any UI element should be set to the
    layer UI, and fall within the GUI
    Camera’s frustum.

Note - You should also ensure that the depth of your UI camera is greater than your Main Camera, else you won’t see anything.

Hope that helps.

==

Ok that works great but I have a question. I use a custom gui setup with an overlay orthographic camera the way you explained.

However I use OnmouseOver events for my buttons that are gameobjects with collider’s. This works fine with a perspective camera but orthographic doesn’t seem to trigger the onmouseover event.

However I need to use an orthographic camera to keep the gui buttons in position with different aspect ratio’s. Any ideas?