- Home /
useGUILayout preventing calls to Graphics.DrawTexture?
Hey, I've been looking for ways to increase the performance of my game. I've been doing some pretty expansive work with the GUI, making it resolution-idependant and so on, but it's started to become pretty intense.
It seems that OnGUI is being called a ludicrous number of times a second, so I read up on how to reduce the number of calls. Apparently, setting useGUILayout to false cuts the calls by half (and, indeed, my own tests shows this to be the case). However, I have an effect at one point of the GUI, which is the pause menu, where this causes problems.
My pause menu structure is quite simple. Since I don't have Unity Pro, I sample the screen to a texture using SetPixel. I then draw it at a quarter of the resolution, and blur it, using Graphics.DrawTexture. I then sample this smaller image again, and draw it across the whole screen, scaled up (again blurred) using another call to Graphics.DrawTexture. This gives a nice, blurred screen effect in the pause menu, and it all works find if useGUILayout is enabled.
If useGUILayout is disabled, though, neither of these called to Graphics.DrawTexture actually works, and I end up with an effect that is certainly not what I was looking for.
This is extremely odd to me, because this doesn't seem like it should happen. As far as I understand it, useGUILayout should have no effect on the Graphics class, and is solely designed to deal with built-in GUI functions (the only GUI methods I use are DrawTexture and DrawTextureWithTexCoords, both are working fine either way). I'm quite concerned by this, because it doesn't seem to make sense. Could anyone perhaps shed some light on what is actually going on?
Actually, I'm wrong here. Only the FIRST call to Graphics.DrawTexture does not work. The second one gets through fine. That is, if anything, even MORE confusing.
Narrowed it down, I think, to when it takes the second screenshot. I have that running in a coroutine, using WaitForEndOfFrame to ensure everything is ready before continuing. This doesn't seem to work with useGUILayout disabled, so I'll try it another way.
Also, I didn't post it twice, I posted it once, then noticed a typo and edited it. It seems Unity Answers duplicated it when I edited it, keeping a version with the typo and the new edited version.
Answer by Hoeloe · Apr 21, 2014 at 10:46 PM
Fixed it! It seems that, with useGUILayout enabled, waiting for the end of a frame to draw a texture is valid. My guess would be because it allows the draw call to happen at the end of a layout phase, so it can be drawn during the repaint phase, or vice versa. I fixed this by moving a draw call out of a coroutine, and instead implemented it as a two-step draw, so that the preparation happens on one frame, and the execution on the next, rather that using WaitForEndOfFrame.
So, in short, don't use coroutines for drawing stuff, kids!
Your answer
Follow this Question
Related Questions
Drawing onto a render texture in OnGUI 0 Answers
Hiding OnGUI? 1 Answer
Android OnGUI Font size problem. 0 Answers
OnGUI button changes size for resolution, but text doesnt? 1 Answer
How to make a GUI window appear on clicking a game object 0 Answers