OnGUI() vs Update() + new UI

I have several UI elements that should display the length of some lists that change from time to time. It’s difficult to track all the times when these lists are changed so before I was just using OnGUI and

GUI.Label(new Rect(4,500,200,20), "Cards: "+ Player.cards.Count);

So with the new UI can I just use this in Update() ?

myText.text = "Cards: " + Player.cards.Count.ToString();

Will it be about the same in terms of performance? For simple types I’m using a property with a set{} that updates the UI text when it’s needed.

You shouldn’t use Update() at all for that. Just update the text whenever the number changes, instead of polling every frame.

1 Like

Make the number a property and update the UI element on “set”?

I know about that, that’s why I said it’s difficult to track each time I’m changing that list. Was hoping that using it in Update() for a few text elements wouldn’t be a hit on performance.

Yep, like I said I do it this way for simple types, however the question was about a List.Count()

It should work. If you’re not sure about the performance, why not cache and compare the generated string? It might do that for you already, but I’m not sure…

1 Like

Instead of caching the string, cache the previous cards count and compare that to current cards.Count, and update text if changed.

2 Likes

If the text hasn’t changed we don’t regenerate anything.

The string catenation would still be generating excess garbage on the heap, though.

Yes, caching the count and testing against it is better than caching the string.