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 xaddict · Dec 20, 2009 at 08:55 PM · guioptimization

How do I change GUI elements from outside of the OnGUI function?

I have an array with 200 elements that have to be placed on the GUI layer once. After that, sometimes I will have to delete one or two of these.

Right now I'm using

for(var i=0;i<itemArray.length;i++){

To loop through it all, but it then does this EVERY onGUI call... making it extremely slow. Is there a way to place the elements once and when a change is made in another function only update this single element?

This is the script I'm using inside OnGUI():

for(var i=0;i<AgendaArray.length;i++){ var taskBegin = AgendaArray[i][1]; var taskEnd = AgendaArray[i][2]; var taskName = AgendaArray[i][0]; var taskStatus = AgendaArray[i][5]; var thisStyle : GUIStyle;

  if(timer &gt;= taskBegin &amp;&amp; timer &lt;= taskEnd &amp;&amp; taskStatus != 2){AgendaArray[i][5] = 1;}
  else if(timer&gt;taskEnd &amp;&amp; taskStatus != 2){AgendaArray[i][5] = 3;}

  if(taskStatus == 1){thisStyle = activeStyle;}
  else if(taskStatus == 2){thisStyle = passedStyle;}
  else if(taskStatus == 3){thisStyle = failedStyle;}
  else{thisStyle = inactiveStyle;}

  var taskPosition = Screen.width*(taskBegin-beginOfTheDay)/lengthOfDay;
  var taskWidth = Screen.width*((taskEnd-taskBegin)/lengthOfDay);

  GUI.Box(Rect(taskPosition,Screen.height-35,taskWidth,30),taskName,thisStyle);
 }

Comment
Add comment · Show 1
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 Stelimar · Dec 20, 2009 at 09:15 PM 0
Share

All of the GUI objects have to be drawn every frame, but you may be able to optimize the script in other ways. Can you give us your entire script?

3 Replies

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

Answer by Lucas Meijer 1 · Dec 20, 2009 at 09:58 PM

In most cases, you just have to draw your elements every frame. If this is giving you realworld performance problems (measure, not guess!), you could create a texture on the fly, render them into that, and then only render the combined texture at runtime.

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

Answer by dkoontz · Dec 21, 2009 at 12:56 AM

The Unity GUI system is an "immediate mode" rendering system, which means there's no GUI objects that are created and maintain their own state (although you could create such a system if you wanted to). So each time OnGUI is called you will need to redraw all of your boxes. Now, as others have suggested, you may be able to solve your performance problems in other ways. The most obvious would seem to be moving your logic for determining a style and box width into some sort of Agenda object that can maintain the needed state so you're only recalculating when something changes.

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

Answer by Peter Alexander · Dec 21, 2009 at 12:54 AM

OnGUI was not designed to be something that you'd actually ship with a product. It's only there so that you can create UI quickly for debugging purposes, or just for placeholders. For a real, shippable UI, you need to roll your own, probably using GuiTexture and GuiText.

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 Eric5h5 · Mar 18, 2010 at 09:36 PM 1
Share

I don't know where you got that idea, but it's not correct. Of course OnGUI was designed to be used in actual products. Otherwise it would not have been created. For complex GUI systems, GUITexture/GUIText is a complete nightmare.

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

HUD with a lot of elements, efficient way? 3 Answers

Changing texture coordinates in GUI 1 Answer

Which is better? UnityEngine.GUI (drawcalls) or UnityEngine.GL? 0 Answers

How to implement an ammo counter using textured quads? [Scripting doubts] 2 Answers

How to create a CPU-friendly dynamic interface? 2 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