Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by Distant-Object · Dec 22, 2015 at 06:26 PM · canvasonguirecttransformefficiency

Efficiency & More: OnGUI() versus RectTransforms on a Canvas?

So from my understanding there are two ways to do the GUI:

1) You can use the OnGUI() method and make a bunch of calls to GUI.Label() and GUI.DrawTexture() and so on to create everything.

2) Or you can have an object with a Canvas, and have a bunch of children with RectTransform (s), each being a separate part of the GUI with the text ones using the Text component or Image components.

My question is, is there any reason to prefer one method to the other?

  • My friend has told me that OnGUI() is terrible for efficiency and should be avoided at all cost. He didn't explain why this is. Is this true? Why is it true? Are there times when onGUI() would be more efficient?

  • As far as I understand, there is nothing one can do that the other cannot. True? For example let's say we want a Halo style motion sensor in a corner, I would think maybe a Canvas would be better for this with each dot being another RectTransform? Are there things that one can do that the other cannot do, at least well?

  • Canvas seems to be easier to use if you want to do stuff like have text expand and shrink during events, but maybe not.

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
1

Answer by callen · Dec 22, 2015 at 06:53 PM

OnGUI() is the way of the past. Canvas has only been around since 4.6, and for almost all use-cases will be what people recommend using. Even the documentation for OnGUI (I guess now called "Immediate Mode GUI" or IMGUI) states:

"IMGUI is a code-driven GUI system, and is mainly intended as a tool for programmers."

and

"The IMGUI system is not generally intended to be used for normal in-game user interfaces that players might use and interact with. For that you should use Unity’s main GameObject-based UI system, which offers a GameObject-based approach for editing and positioning UI elements, and has far better tools to work with the visual design and layout of the UI."

So from this I'd infer that IMGUI is supposed to only be for debugging (but really it's stuck in there for backwards-compatibility), while the new UUI stuff is supposed to be used for true end-User Interfaces.

Comment
Add comment · Show 3 · 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 Distant-Object · Dec 22, 2015 at 07:28 PM 0
Share

Thanks, good to know. It does seem improved. I have a question for you, so I would like to have a black outline around my some of my text. I have written my own method for this, like this http://answers.unity3d.com/questions/160285/text-with-outline.html except it doesn't have the problem he mentions. But it's for OnGUI(). How do I do the same thing using the Canvas approach?

avatar image callen Distant-Object · Dec 22, 2015 at 07:38 PM 0
Share

Actually there's a UUI component called Outline you can add to a Text object to get the same effect. There is also one for Shadow that I use a lot.

avatar image Distant-Object callen · Dec 22, 2015 at 08:01 PM 0
Share

Wow, it really is so much better. Thank you!

avatar image
1

Answer by tanoshimi · Dec 22, 2015 at 06:55 PM

Simply put, OnGUI() is the "old" way of doing GUI, while canvas is the "new" replacement. I can't remember exactly when it was introduced, but OnGUI been in since at least Unity 3.x, probably earlier. Canvas was only introduced in Unity 4.5.

Canvas is to be preferred in all cases of new development - it is more efficient and more feature-rich. AFAIK, one of the only reasons why OnGUI has not been deprecated is because it's the same codebase that is used to draw GUI functions in the editor. However, in that case, it's fine to use.

Your friend is correct - OnGUI has appalling performance and many other quirks (OnGUI is called multiple times per frame, since it is called to both lay out the GUI elements on the screen and then again to render them).

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

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

33 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 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 avatar image

Related Questions

how to change the recttransform to clicktomouse ? 0 Answers

Swapping UI image element positions 1 Answer

4.6 UI Canvas moves on Play, but not really? 0 Answers

ScrollRect content position is wrong in build 2 Answers

Canvas shrinks over time 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