is using OnPostRender to draw textures Pro only?

Im trying to draw a texture on screen using Graphics.DrawTexture, but I am getting no results. but if i were to use it in OnGUI it suddenly decides to draw the texture.

      void OnPostRender() 
       IEnumaertaor DrawGUI() {
       yeild return new WaitForEndOfFrame;
       Graphics.DrawTexture(new Rect(0,0,128,128),MyTexture);



this does nothing...its currently unknown to me why but...

         void OnGUI() 
         if (Event.current.type == EventType.repaint)
                  Graphics.DrawTexture(new Rect(0,0,128,128),MyTexture);

this does...works great infact, however it called many times a frame making it a waste to use. I tried Google only finding unrelated answers, tried adding the script to a camera, also tried removing the "if" block. can someone explain why is this happening? (or why unity needed to remove this from basic)

EDIT I've just realized my GUI was drawing but in the stupidest possible manner, it wasn't drawing on screen but based on the coordinated of the Game_manager(which manages GUI and scene transition and other things). lol, i watched my scene view to see the giant 128x128(level units) texture floating behind the player lol. I have no clue on fixing this.... would camera.WorldtoVeiwportPoint be a starter? ![![alt text][2]][1]

alt text

[2]: /storage/temp/10475-unity+2013-04-27+23-42-48-020.jpg

more ▼

asked Apr 26, 2013 at 06:21 PM

avatar image

3.7k 9 911 31

Have you tried OnRenderObject() instead?

Apr 26, 2013 at 11:32 PM mhtraylor

tried that it wont work, I don't what Unity is drawing over but its happening in OnRenderImage (clearing image effects for non-pro users?).

Apr 27, 2013 at 06:02 AM Fornoreason1000

great question / answer so pls leave open

May 01, 2013 at 06:44 PM Fattie
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

i stumbled upon a solution to use Graphics.DrawTexture in PostRender randomly through testing. simply put you need to setup Matrices to stop it from drawing in World space which only happens outside OnGUI, I'm not sure why that happens. Anyway here is how to fix it, some sample code:

         Graphics.DrawTexture ( new Rect (0,0,300,300), MyTexture, 2, 2, 2, 2, null);

this only works if its attached to the main camera or the camera with the upper depth. it also need to be redrawn after every frame. turns out it had nothing to do with co routines or using the wrong type of coordinates. whether this method is safe or not performance wise is to be determined. hope it helps other users

more ▼

answered May 13, 2013 at 06:04 PM

avatar image

3.7k 9 911 31

(comments are locked)
10|3000 characters needed characters left

"OnPostRender is called after the camera renders all its objects. If you want to do something after all cameras and GUI is rendered, use WaitForEndOfFrame coroutine."


Basically what could be happening is that something is getting drawn ontop of your texture for not using the WaitForEndOfFrame coroutine.


more ▼

answered Apr 26, 2013 at 11:56 PM

avatar image

950 10 11 20

thanks... I would up vote, but this petty moderation thing just tells me im not allowed to. lol, not to mention it takes forever to help others. Unfortunately that doesn't seem to help...since the texture was there but in 3d space not screen space hang out behind the player

Apr 27, 2013 at 05:50 AM Fornoreason1000

for future readers simply TICK if you are a new user and the answer is correct

May 01, 2013 at 06:44 PM Fattie
(comments are locked)
10|3000 characters needed characters left

Putting event code in OnPostRender isn't possible - it only works in OnGUI and functions it calls. OnPostRender is called to let you do stuff with a render texture - so isn't much use on Free as you can't get one.

Graphics.DrawTexture and GUI.DrawTexture both work in screen coordinates. So if you want the screen coordinates of a world object you use camera.WorldToScreenPoint.

more ▼

answered Apr 27, 2013 at 07:08 AM

avatar image

54.2k 60 98 230

BTW if you only draw your texture in OnGUI when Event.current.type == EventType.repaint then the texture draw will only happen once per frame.

Apr 27, 2013 at 07:09 AM whydoidoit

can i show a pic of whats happening? cause Graphics.DrawTexture is not screen space (not in this case anyway). it draws it in the world at the coordinate (0,0) and a scale of 128(don't know where the z goes) I know its not supposed to happen, but it is...

Apr 27, 2013 at 02:10 PM Fornoreason1000

I think you might be wrong about how Graphics.DrawTexture works.

"Draw a texture in screen coordinates.

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."

As for why there is no z.

I believe you are using world coordinates to screen coordinates to position the texture making you think it would "enter" the world space yet it is still in screen space, could you provide an image of the issue?


Apr 27, 2013 at 02:15 PM xortrox

see the weird thing is if i put the same function in ONGUI the texture appears (0,0) on the screen AKA top left corner, but OnPostRender it appears a giant plane.

Apr 27, 2013 at 02:22 PM Fornoreason1000
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Apr 26, 2013 at 06:21 PM

Seen: 3403 times

Last Updated: Nov 03, 2013 at 05:26 AM