• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Fornoreason1000 · Apr 26, 2013 at 06:21 PM · c#guitexturegraphicsrender

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() 
             {
                 
          DrawGUI();
                  
                   
                
            }
       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

unity 2013-04-27 23-42-48-020.jpg (492.8 kB)
unity 2013-04-27 23-42-50-376.jpg (642.6 kB)
Comment
whydoidoit

People who like this

1 Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mhtraylor · Apr 26, 2013 at 11:32 PM 0
Share

Have you tried OnRenderObject() instead?

avatar image Fornoreason1000 · Apr 27, 2013 at 06:02 AM 0
Share

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?).

avatar image Fattie · May 01, 2013 at 06:44 PM 0
Share

great question / answer so pls leave open

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Fornoreason1000 · May 13, 2013 at 06:04 PM

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:

         GL.PushMatrix();
         GL.LoadPixelMatrix();
  
         Graphics.DrawTexture ( new Rect (0,0,300,300), MyTexture, 2, 2, 2, 2, null);
  
         GL.PopMatrix();


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

Comment
jeThomas

People who like this

1 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by xortrox · Apr 26, 2013 at 11:56 PM

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

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnPostRender.html

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

http://docs.unity3d.com/Documentation/ScriptReference/WaitForEndOfFrame.html

Comment
whydoidoit
Loius
Fornoreason1000
Fattie

People who like this

4 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fornoreason1000 · Apr 27, 2013 at 05:50 AM 1
Share

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

avatar image Fattie · May 01, 2013 at 06:44 PM 1
Share

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

avatar image

Answer by whydoidoit · Apr 27, 2013 at 07:08 AM

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.

Comment
Fornoreason1000
Fattie

People who like this

2 Show 13 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Apr 27, 2013 at 07:09 AM 0
Share

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.

avatar image Fornoreason1000 · Apr 27, 2013 at 02:10 PM 0
Share

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

avatar image xortrox · Apr 27, 2013 at 02:15 PM 0
Share

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?

http://docs.unity3d.com/Documentation/ScriptReference/Graphics.DrawTexture.html

avatar image Fornoreason1000 · Apr 27, 2013 at 02:22 PM 0
Share

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.

avatar image xortrox · Apr 27, 2013 at 02:26 PM 0
Share

Could this be a solution?

http://docs.unity3d.com/Documentation/ScriptReference/GUI.DrawTexture.html

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to use Graphics.Draw draw a texture behind other GameObject? 2 Answers

Can't create a button when clicked? 1 Answer

Multiple Cars not working 1 Answer

How to gradient-wise fill a bitmap programmatically 1 Answer

Equivelant of GUI.DrawSprite() ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges