Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: breakmachine on October 07, 2013, 09:28:31 AM

Title: Minimizing heap allocation
Post by: breakmachine on October 07, 2013, 09:28:31 AM
Hi!

Do you have any tips and tricks to avoid allocation on the heap when using NGUI?

I saw somewhere that you wrote that "ever-moving" sprites should have their own panel to avoid redraws. Would that help for example? And does that apply to a label displaying "ever-updating" time aswell?

I'm currently using NGUI 3.0.1 (just realized 3.0.2 has been released).
Title: Re: Minimizing heap allocation
Post by: nameles01 on October 07, 2013, 02:12:15 PM
Hi Breakmachine,

NGUI attempts to combine all widgets and textures into as few draw calls as possible by combining all the geometry into a single mesh and all the different textures into a minimal amount of materials.

The creation of the combined geometry is a script intensive step and needs to be repeated each time any of the geometry within a panel changes, i.e.: you rotate, scale or move a widget inside a panel. A UIPanel has a static flag that you can use to indicate that the geometry inside the UIPanel is not expected to change which lets the panel use cached values.
This, of course, does not work when the geometry inside the panel DOES change - if the panel is static you will see no change as the moved geometry is not integrated into the combined mesh. Therefore the advice is to split up your static and non-static geometry and assign them to either a static or non-static panel. Another thing you can do is to toggle the static flag off only while you're changing the geometry inside the panel. We have noticed massive performance changes by using the static flag properly.

Do note that you are free to move / scale / rotate the panel itself while it is static - just not the contents.

Hope that helps!
Title: Re: Minimizing heap allocation
Post by: breakmachine on October 07, 2013, 03:11:06 PM
Thanks for that reply. Just to clarify, are we talking change in performance as in less allocated memory or just less cpu?

Also, what about panels within panels? If a moving panel (a) is parented under a panel (b) that don't have widgets changing or is marked as static, will panel (b) still be redrawn?

And, if a gameobject has both a panel script  marked as static and widget script on it and moves, does that result in any redraw?
Title: Re: Minimizing heap allocation
Post by: ArenMook on October 08, 2013, 01:36:13 AM
Moving panels is cheap. Moving widgets inside panels is expensive.
Title: Re: Minimizing heap allocation
Post by: breakmachine on October 08, 2013, 07:41:47 AM
And what about panels in panels and gameobjects with both widget and panel?
Title: Re: Minimizing heap allocation
Post by: breakmachine on October 08, 2013, 07:46:31 AM
For example:

1. If I have a panel (a) within a panel (b) in the hierarchy and move panel (a). Will that cause cause panel (b) to redraw?

2. If I have sprites representing enemies on a minimap (spawned when enemies spawn), could they be one gameobject per sprite with both sprite script and panel script on them with panel set to static?

I'm currently suffering from around 20-30kb heap allocation every now and then...
Title: Re: Minimizing heap allocation
Post by: ArenMook on October 08, 2013, 12:58:48 PM
1. No. Still cheap.
2. Sprite parented to a panel, sure.
Title: Re: Minimizing heap allocation
Post by: nwsx on October 08, 2013, 03:32:22 PM
what if i have a lot of healthbars on screen? should i spawn healthbars with UIPanels for each of them or should i keep them inside one UIPanel.  ::)
Title: Re: Minimizing heap allocation
Post by: ArenMook on October 09, 2013, 08:47:05 AM
Depends on how many you have, which devices you're targeting, and how frequently they move around. Try it one way. If you have issues, try it another. Experiment and find what works for you best.