For performance reasons, I want to take out my OnGUI function and instead of using GUI.DrawTexture I was thinking in replacing it by Graphics.DrawTexture and just using it in the Update function.
However, just replacing functions is not working.
My code on the OnGUI is the following :
var ac : Vector2;
ac = Camera.main.WorldToScreenPoint(shipICON.transform.position);
GUI.DrawTexture (Rect (ac.x-16,Screen.height - ac.y - 16,32,32), shipIcon);
Replacing GUI.DrawTexture by Graphics.DrawTexture in the Update function, nothing happens, nothing is being rendered.
Any clues on why this is not working ?
It’s possible that DrawTexture is designed to be called within a Current cameras rendering process.
Try using OnPostRender() instead of Update. Because there is no specific camera being drawn to in Update and so the DrawTexture gets dumped (think of the difference between DrawMesh and DrawMeshNow).
Well OnGui does weird stuff. It can update many many times in one frame, or it can not update for minutes. It basically calls a redraw on a camera that doesn’t get cleared except on events. So that it’s showing a picture instead of something being updated 60 fps or so.
It seems like the reason is that whatever you are drawing is getting cleared before the render, and so is not showing up on the actual render. Update will certainly not work, however it’s a matter of finding the appropriate time after the camera has been cleared for that frame, and before it’s finished rendering for the frame. Whether or not you have access at that point or how you specify which camera to render to at what times is a mystery though. It works for OnGUI because that is after the Gui Camera clear and before the Gui Camera completion.
You are drawing it. It’s just being overwritten by the scene when the scene gets written over the top. Update is way too early in the day to be drawing things to the screen.
I’m surprised it doesn’t work in OnRenderObject though. OnPostRender doesn’t work either, you say? What about OnRenderImage? That also seems to happen after all scene rendering has been done.
Note that I’ve tried it, and it does work if you put it in OnPostRender; except Graphics.DrawTexture uses world coordinates rather than screen coordinates, when not in the OnGUI function. That’s probably why the OP didn’t think it worked at all. Also, another requirement is that the script be attached to a camera, and that the camera be “top-level”. (so that the code runs after all the camera’s have finished drawing onto the screen)
I was attempting to use Graphics.DrawTexture for the same reason as him, (to replace the slow OnGUI system), but I gave up, since I ran into some issues that I didn’t want to fix. (I couldn’t seem to get the textures to be drawn at just any world-to-screen mapped location, but only at locations that were multiples of the texture size, which wouldn’t work for what I needed it for, a cursor; there’s probably a fix, but it wasn’t that important anyway, so I just gave up.)