- Home /
GUI.DrawTexture shows in Unity but not in Build
Hey folks, this issue is so strange that I'm convinced I've done something stupid.
I'm building a game with a GUI image using GUI.DrawTexture. When I test the game in Unity, the texture shows. When I build the game for Windows or Mac, it doesn't. I know it's not an issue with positioning since I'm setting it to be centered on the screen to be sure it shows up. I've also tried other GUI methods like DrawButton and DrawLabel, and they work as expected. I saw someone suggest that I remove my Scene from the build and then re-add it. This had no noticeable effect.
It might be worth noting that I'm porting my code from Javascript to C#, and this exact code and image worked as expected in Javascript.
Anyway, here's my code:
void OnGUI(){
GUI.DrawTexture(new Rect(Screen.width * 0.5f, Screen.height * 0.5f, 112.0f, 124.0f), timerBar);
}
Any ideas? Thanks!
Are you getting any errors? Check the output_log. Try placing Debug.Log to gain more info. I suspect the timerBar may be null for some reason. How are you getting that?
Hi Gizmoi, it's true, timerBar is ending up as null, but I'm still not sure why. I'm setting it in the inspector, as I've done for several other Textures that work just fine. I'll keep sifting through the logs.
Perhaps its not getting imported correctly?
Is the object this script is on instantiated at runtime or a prefab, or both?
The object is is a prefab. I can add another prefab object that uses the same Texture as I'm trying to use for the GUI, and it appear with no problem. When I swap in a different Texture for use in the GUI, and it also won't load.
After trying everything I could think of, I finally just tried a workaround-- to load the Texture at runtime using Resource.Load, which worked fine. I'm a little concerned though, since this I was just trying to test out a GUI element to see how it looked, and I've got a bunch more elements to incorporate. It wouldn't be much of a pain to load them all at runtime, but I'd rather know what caused the problem.
Answer by Elringus · Jul 13, 2013 at 12:52 PM
I'm having very similar issue right now. The only difference is that I’m using DrawTexture method only on repaint and with custom material (particle-additive):
void OnGUI () {
if (Event.current.type.Equals(EventType.Repaint))
Graphics.DrawTexture(new Rect(), texture, material);
}
Also, I’m using some post effects (image effects) on the main camera.
I found, that turning all the image effects off lets the texture show up in the build. Then you can enable them again, and texture will continue rendering normally.
I just sent bug report with repro project and hope that Unity will fix it someday…
Answer by fabio1955 · Mar 04, 2014 at 01:07 PM
I have already met this problem on a AMD card. I found a solution here: http://varuagdiary.blogspot.it/2012_01_01_archive.html.
The trick is to take off the alpha layer of the render texture during postrender. It works!
Unfortunately this project went live long ago-- I'm convinced that in my case it was a problem with the project itself, since to 'solve' it I created a new project, copied over my assets and code, and everything just worked.