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 trs9556 · Apr 30, 2013 at 02:56 AM · performanceonguicachecalculation

Are OnGUI calculations cached?

Currently in my UI scripts I do a lot of calculations for determining the x and y coordinates and the width and length of my component. For example stuff like this:

 GUI.Box(new Rect(screenWidth/4,screenHeight/4, screenWidth/2,screenHeight/2), "You must finish your current wave before you can save your game.");
 if(GUI.Button(new Rect(screenWidth/2 - (screenWidth/5)/2, screenHeight/2 + screenHeight/16, screenWidth/5,screenHeight/7), "Ok")){
     showSaveGameMustFinishWave = false;    
 }

I'm referring to the new Rect(screenWidth/4,screenHeight/4, screenWidth/2,screenHeight/2 part.

My question is are those calculations calculated twice every frame or are they calculated once and stored in a cache.

The reason why I ask is because if they are calculated every time then it would be smarter to make a bunch of rect variables, define them in the awake or onstart method and then just reference the rects such as:

 OnAwake(){
    box1 = new Rect(blah,blah,blah,blah);
    box2 = new Rect(blah,blahl,blah,blah);
 }
 
 OnGUI(){
    GUI.Box(box1, "Hi");
    if(GUI.Button(box2, "ok")){
        //code
    }
 }


Now I understand I might not see a performance difference in a couple but I have around 300+ of these if you add them all up between my scripts. Some of them as simple as screenWidth/2 and others with a lot of calculations per parameter.

Would it be worth it to define them at the start, or are they cached? If it matters my main platform is Android. I'd also like to port to iPhone eventually. Thanks.

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

1 Reply

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

Answer by Eric5h5 · Apr 30, 2013 at 03:29 AM

They are not cached; however it's often the case with modern CPUs that calculating stuff is faster than fetching values from relatively slow RAM.

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 trs9556 · Apr 30, 2013 at 03:36 AM 0
Share

What would you suggest then? In an example of having a lot of these (over 100 at one time easily) would you suggest I calculate them or store them?

If it makes a difference I already store a lot in memory as it is, in the neighborhood of 100mb+ per level. I know text based things won't take up much but it is already being access frequently is what I'm getting at.

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

14 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

Related Questions

InspectorWindow.DrawEditors high CPU in Editor. 1 Answer

does interpolation full up some kind of cache over time 1 Answer

Performance load of OnGUI 0 Answers

Better performance? GUITextures or Texture2D drawn in function OnGUI? 0 Answers

Which one is better for mobile games many OnGUI function or one complex OnGUI function? 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