- Home /
Graphics DrawTexture C# Does not appear
I am trying to draw a texture via code like this:
void Update () {
Vector2 clockSizeP = new Vector2 (clockSize.x * Screen.width, clockSize.y * Screen.height);
Graphics.DrawTexture(new Rect((Screen.width / 2) - (clockSizeP.x / 2), clockPos.y * Screen.height, clockSizeP.x, clockSizeP.y), clockTexture);
}
but nothing appears, what am I doing incorrect? TIA
Drawing needs to be called on OnGUI()
Check the docs : http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnGUI.html
Answer by Q-nn · Jul 15, 2014 at 05:56 PM
You can only do this during the Repaint event in OnGUI(). From memory you can also do it in OnPostRender but I have not tested that for this answer.
void OnGUI() {
if (Event.current.type.Equals(EventType.Repaint))
Graphics.DrawTexture(new Rect(10, 10, 100, 100), aTexture);
}
http://docs.unity3d.com/ScriptReference/Graphics.DrawTexture.html
It's a good idea to read the docs before you use a function you don't know about. At the very least, it's a good idea to read them after it all goes wrong when you try using something without knowing what it's for.
The docs say: "If you want to draw a texture from inside of OnGUI code, you should only do that from EventType.Repaint events. It's probably better to use GUI.DrawTexture for GUI code." If it is called during Update
, then presumably the above doesn't apply.
Your answer
Follow this Question
Related Questions
Graphics.DrawTexture not working after changing quality settings 1 Answer
GUI.DrawTexture versus Graphics.DrawTexture 4 Answers
RenderTexture and DrawTexture using material is not working 0 Answers
Graphics.DrawTexture vs GUI.DrawTexture vs Graphics.Blit 0 Answers
Graphics.DrawTexture + GUI.color 0 Answers