- Home /
How to call Graphics.DrawTexture() in Update
How would you call Graphics.DrawTexture() from Update or any other function than GUI. I wanted to use Graphics.DrawTexture() instead of GUI.DrawTexture() just so I wouldn't have to call it from OnGUI but it only draws a texture if its in OnGUI().
var texture : Texture2D;
var rect = Rect(0,0,0,0);
var color : Color;
var mat : Material;
function Update () {
//I would like to call Graphics.DrawTexture() here
}
function FixedUpdate () {
//Or maybe even here
}
function OnGUI () {
//But it only works here
if (Event.current.type != EventType.Repaint){ //draws texture only in 3d space
if(texture != null){
rect.center = transform.position;
Graphics.DrawTexture(rect, texture, Rect(0,0,1,1), 0, 0, 0, 0, color, mat);
}
}
}
I don't get any errors but it just doesn't draw the texture when I call the function outside of OnGUI.
Thanks for any type of help.
Have you looked into the $$anonymous$$onobehaviour function OnRenderObject?
Oh, Awesome. I had no idea that OnPostRender was repeatedly called. I had the dumb idea that it was only called once like Start or Awake. Post your reply so I can mark it as a right answer. Thanks
Answer by lktfl · Apr 08, 2013 at 04:05 AM
Ok, since that other guy wont post his answer as an answer;
since I care that unity answers is a database for all users, not just my personal questionnaire;
AND since I know that most users who have the same question will click on this page, look at it for 2 seconds and see that there's no official answer, then click off without really reading my question or the responses...
Answer Part:
OnRenderObject, if you want to call Graphics.DrawTexture() every frame you must call it from within OnPostRender().
Reason:
This is because (I'm assuming) Graphics.DrawTexture() draws the texture immediately. Update() runs before the next frame is rendered.
Therefore if you call Graphics.DrawTexture() in the Update() function or any other function that is called before the frame is rendered, it will not show up.
That is why you have to call it in OnPostRender() (Called after frame is rendered) or OnGUI() (called multiple times a frame) or just after the frame has been rendered.
correct me if I'm wrong
Your answer
Follow this Question
Related Questions
Getting Error "DrawGUITexture: Texture is Null" when using Graphics.DrawTexture in OnGUI() 2 Answers
Problem with Graphics.DrawTexture 0 Answers
Creating an outline around the opaque pixels on a plane. Outline should extend outside of plane. 0 Answers
water graphic problem 0 Answers
Graphics/RenderTexture Alpha Brush 0 Answers