Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by JoeStrout · Feb 27, 2011 at 05:05 PM · guibuggraphicsdrawtexture

Graphics.DrawTexture drawing in both 2d and 3d?!?

I'm trying to use Graphics.DrawTexture to draw some 2D HUD-style overlays on my scene. (I'm not using GUI.DrawTexture for this because that can only draw the entire texture -- I need to draw a lot of little pieces from a larger texture map.) I'm calling this from OnGUI().

It all works great, except for one thing: it appears that Graphics.DrawTexture is drawing twice. Once, as a 2D object in screen space, exactly as I would expect; and again, as a textured 3D plane that floats out in the environment. If I look in the right direction, there it is, hanging out at some funny angle and upside-down behind the 2D version I intended.

I've gone carefully over my code to see if there is some way I could be rendering the same texture in two different ways, but I don't think so. If I comment out the one Graphics.DrawTexture call, both version disappear. When I put it back in, and turn the camera in the right way to see it, there they both are.

I've searched the other questions here related to Graphics.DrawTexture, but can't find any other references to this problem. I'm using the latest version of Unity (3.2rf4) on a Mac. Any ideas what's going on here?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by JoeStrout · Feb 27, 2011 at 05:30 PM

OK, further experimentation, plus a real gem of a tip noticed in an example from this question, has led to the answer.

It appears that OnGUI is called multiple times (as the docs do mention), and the current transform is only set up for overlay drawing when the event type is EventType.Repaint. At other times (for example, when it's EventType.Layout), the transform is something else, and your attempt at GUI drawing will appear out in the world instead.

So for me, simply inserting this line before the drawing function:

if (Event.current.type != EventType.Repaint) return;

solved the problem completely. I would speculate that the various drawing methods in the GUI module (such as GUI.DrawTexture) each contain such a line already, which is why you only notice this issue if you use the lower-level Graphics drawing.

Sorry for answering my own question -- something about displaying my ignorance publicly seems to immediately add 10 points to my IQ. :) Hopefully having this in the archives will help the next person to run into the same issue!

Comment
Add comment · Show 1 · 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 Bunny83 · Feb 27, 2011 at 05:42 PM 0
Share

Don't be sorry for answering your own question! It's good to see at least a few people doing some own research ;)

avatar image
1
Best Answer

Answer by Bunny83 · Feb 27, 2011 at 05:25 PM

Graphics.DrawTexture is not a GUI function, Why do you call it from OnGUI? if you do non GUI stuff in OnGUI you have to make sure to do it the right way. OnGUI is called multiple times in one frame. You have to check the current GUIevent

//C# void OnGUI() { if (Event.current.type == EventType.Repaint) { // Do your drawing stuff here } }

//JS function OnGUI() { if (Event.current.type == EventType.Repaint) { // Do your drawing stuff here } }

Or maybe move it to OnRenderObject

Take a look at this forum thread:
http://forum.unity3d.com/threads/43277-Graphics.DrawTexture-side-effect

Comment
Add comment · 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 JoeStrout · Mar 01, 2011 at 02:34 PM 0
Share

I call it from OnGUI because I'm using it as a GUI function -- it's just one of many other GUI operations going on all together (including GUI.DrawLabel, etc.).

And yes, see my answer below; I had already found that I need to check the event type. But I hadn't seen that thread you point out; it does indeed refer to the exact same issue. Perhaps this will make it into the documentation, when the Graphics.DrawTexture docs get written. :)

If I were to call it from OnRenderObject or any other callback, I would have to go through gyrations to transform it properly to screen space, right?

avatar image Bunny83 · Mar 01, 2011 at 03:26 PM 0
Share

Well, i guess. Since the information on DrawTexture is really very thin you have to try-and-error. I'm not sure what transformation matrix is used by DrawTexture maybe just the worldToCamera$$anonymous$$atrix of the current camera. I haven't used DrawTexture but maybe i'll give it a try someday. The sourceRect seems to be very useful ;) All that can be done with a proper shader but it will be a bit more complicated.

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

GUI.DrawTexture versus Graphics.DrawTexture 4 Answers

GUI.DrawTexture not working? (nothing shows up) 1 Answer

Graphics DrawTexture C# Does not appear 1 Answer

GUI Texture Resolution 0 Answers

GUI.DrawTexture shows in Unity but not in Build 1 Answer


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