- Home /
4.6 UI sort order in fornt of IMGUI/OnGUI possible?
Is there a way to display a new UI (post Unity 4.6) canvas in front of old OnGUI elements? I've tried setting the OnGUI elements to sortOrder = 1 and the new ones to 1000, but OnGUI still draws on top of everything else.
I'm using a canvas for debugging and want it on top of everything else, but some other tools still draw old OnGUI debug info etc, that's why I have both currently.
Answer by Bunny83 · Feb 21, 2017 at 06:02 PM
Yes you can draw the new UI on top of the old IMGUI. You just need to setup your new UI properly and render it manually from inside OnGUI.
The repaint event of the OnGUI callback is pretty much the last thing that is rendered. It will fire when all cameras have finished rendering. That's why you have to do the following:
Setup your canvas to be "screenspace - Camera" and create a dedicated UI camera for this canvas. You might want to use a seperate layer for that UI stuff and set the camera cullingmask accordingly.
Make sure you assign that camera in your UI canvas.
Make sure you disable the camera and set it's clearflags to "Depth only"
Inside OnGUI you can now manually render your UI camera after your other OnGUI stuff::
public Camera UICamera;
void Start()
{
UICamera.enabled = false;
}
void OnGUI ()
{
if (GUI.Button(new Rect(10,10,300,200),"Test Button"))
{
}
if (Event.current.type == EventType.Repaint)
{
UICamera.Render(); // this will render the new UI
}
}
Note that if you need interactive elements in both UIs you have to take care yourself of which should get the input. The new UI uses the new Eventsystem while OnGUI directly processes input usually around the time when Update is called.
Though pure visual UI should work just fine that way.
If you have multiple scripts that use OnGUI and you want the new UI on top of all of them you simply would render the camera from a seperate script with OnGUI callback that is ensured to run last / is on top.
Yes, thanks. I've seen this approach used by another dev while hacking a old project and mixing with the new UI system. Seems to work fine for those special cases.
Your answer
![](https://koobas.hobune.stream/wayback/20220612055344im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I obtain children on UI canvas text 0 Answers
Trying to place an UI Canvas Image next to another UI Image 1 Answer
Is there a way to make a Texture2D screenshot that ignores the canvas? 3 Answers
CanvasGroup.alpha hides the UI in Scene view but not in game view. 1 Answer
UI fade with CanvasGroup vs of Image.color.alpha or Text.color.alpha performance 1 Answer