Best practices for high performance when using FlexBuilder via script

The main thing to bear in mind when aiming for performance is that FlexBuilder will always default to "doing the right thing" -- which may be a lot slower than necessary. For instance: if you write a script that adds 100 FlexItems to a FlexContainer, FlexBuilder will automatically re-run the layout algorithm ... 100 times. It wants to make sure that the layout is always correct by default.

...but if you know you're going to add 100 items, all in the same frame, there's no point in running 100 layouts: you want FlexBuilder to wait and only run it once.

Batching updates - automatically

There's a built-in system in FlexBuilder for doing this automatically for you. When you want to make a lot of changes in a single frame, wrap the methods in a block with the "ExperimentalDelayUpdates2" class. FlexBuilder will intelligently detect all changes inside that block, re-order them for performance where possible, and execute the smallest amount of changes needed.

using( new ExperimentalDelayUpdates2() )
{
   /// normally these three lines would cause 3 relayouts
   /// ... but the 'using' causes only 1 relayout to be executed
   myFlexItem.flexGrow = 1;
   myFlexItem.flexShrink = 0;
   myFlexItem.cssWidth = 50f;
}

You can wrap multiple method calls - or just wrap your own main method in a 1-line block - FlexBuilder will auto-detect the context and handle it correctly. e.g.


public class MyScript : MonoBehaviour
{
  public FlexContainer containerToChange;
  public void Update()
  {
    using( new ExperimentalDelayUpdates2() )
    {
       MyUIChangesMethod();
    }
  }

  public void MyUIChangesMethod()
  {
    ... do your FlexBuilder scripting calls here
  }
}