x


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 >= taskBegin && timer <= taskEnd && taskStatus != 2){AgendaArray[i][5] = 1;}
     else if(timer>taskEnd && 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);
    }
more ▼

asked Dec 20 '09 at 08:55 PM

xaddict gravatar image

xaddict
142 7 8 19

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?

Dec 20 '09 at 09:15 PM Stelimar
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Dec 20 '09 at 09:58 PM

Lucas Meijer 1 gravatar image

Lucas Meijer 1 ♦♦
7.9k 19 43 85

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Dec 21 '09 at 12:56 AM

dkoontz gravatar image

dkoontz
170 6 8 18

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Dec 21 '09 at 12:54 AM

Peter Alexander gravatar image

Peter Alexander
931 5 6 23

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.

Mar 18 '10 at 09:36 PM Eric5h5
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3669
x470

asked: Dec 20 '09 at 08:55 PM

Seen: 1891 times

Last Updated: Dec 20 '09 at 11:54 PM