Upgrade to Flexalon 2

Flexalon 2 rebuilds the layout core so that changes only recompute what they affect. Most projects that only use the built-in components and the inspector upgrade without code changes. If you call the Flexalon API from scripts, or extend Flexalon with your own components, read the sections that apply to you.

Back up your project before upgrading. Flexalon 2 upgrades serialized components when they're first loaded, and you can't downgrade them afterwards.

Before you start

  • Unity 2022.3 or newer is required. Update your editor before importing Flexalon 2.
  • Delete the old Assets/Flexalon folder before importing the new package, so that removed files don't remain in your project. Then follow the installation steps.
  • Open each scene and prefab that uses Flexalon and save it, so the component upgrades are written to disk.

Quick checklist

If your project…See
Uses only built-in components from the inspectorBehavior changes for everyone
Refers to any Flexalon interface, or to LayoutBase, Math, Flex, or FlexItemRenamed types
Calls Flexalon.Get(), Flexalon.GetOrCreate(), or other members of the Flexalon classThe Flexalon singleton is now FlexalonCore
Has classes that derive from FlexalonComponentCustom components
Has classes that derive from LayoutBase or implement LayoutCustom layouts
Implements AdapterCustom adapters
Implements TransformUpdater or derives from a built-in animatorCustom animators
Implements InputProviderCustom input providers
Implements FlexalonModifier or ConstraintCustom modifiers and constraints

The rows above use the Flexalon 1 names. The rest of this guide uses the Flexalon 2 names.

Renamed types

Flexalon 2 adds an IFlexalon prefix to every Flexalon interface and a Flexalon prefix to the remaining public classes, so they don't collide with your own types or with System.Math. The old names don't exist anymore, so code that uses them won't compile until you update it:

Flexalon 1 nameFlexalon 2 name
AdapterIFlexalonAdapter
LayoutIFlexalonLayout
ConstraintIFlexalonConstraint
TransformUpdaterIFlexalonTransformUpdater
InputProviderIFlexalonInputProvider
FlexalonModifierIFlexalonModifier
FlexalonNodeIFlexalonNode
DataSourceIFlexalonDataSource
DataBindingIFlexalonDataBinding
LayoutBaseFlexalonLayoutBase
MathFlexalonMath
FlexFlexalonFlex
FlexItemFlexalonFlexItem

Only the types were renamed. Properties with the same names, such as node.Adapter, node.Constraint, and FlexalonCore.InputProvider, keep their names, so rename Adapter, Layout, and Constraint only where they're used as a type. Nested types also keep their names.

Behavior changes for everyone

Inactive GameObjects are always excluded from layouts. The Flexalon.SkipInactiveObjects setting was removed. If you had turned it off so that inactive children kept their space, add a placeholder object instead, or set Skip Layout on the Flexalon Object of children that shouldn't take up space.

Disabled Flexalon components no longer affect layout. Disabling a layout, Flexalon Object, adapter, constraint, modifier, or cell component now removes its effect, and enabling it again restores the effect. Previously, some components continued to apply their last settings while disabled. If you disabled a component to "freeze" it, keep it enabled instead.

Root UI anchors may change. If Flexalon controls the size of a root RectTransform, Flexalon now sets its anchors so that it follows its parent: Fill axes stretch, Fixed and Layout axes stay centered, and Component axes stay under your control. If your own scripts or animations also write the anchors of these objects, they will now conflict with Flexalon.

Layouts update at a different point in the frame. In Flexalon 1, layouts updated in the LateUpdate of the Flexalon GameObject. In Flexalon 2, they update after all LateUpdate scripts run. Script Execution Order settings for the Flexalon component no longer have any effect. If your script needs layout results in the same frame, call FlexalonCore.UpdateDirtyNodes() first, or subscribe to FlexalonCore.PostUpdate.

UI positioning respects pivots. Layouts now position Unity UI children by their visible bounds and preserve their RectTransform pivots. UI elements with pivots other than the center may move slightly from where they were in Flexalon 1. Recheck these elements after you upgrade.

Objects in front of an interactable now block it. Flexalon 1 picked the nearest interactable handle among everything under the pointer, so it looked through popups, panels, and walls. Flexalon 2 uses the nearest UI or physics hit, and selects an interactable only if that hit is its handle or a child of its handle, so images, text, and child colliders inside a handle still select it. A raycastable graphic or a collider in front of an interactable now blocks it, the way a popup blocks the buttons behind it. Trigger colliders block too, as they do for the Event System's Physics Raycaster, unless Queries Hit Triggers is off in the Physics settings. To let input through an object, turn off Raycast Target on its graphics (or Blocks Raycasts on its CanvasGroup), or move its collider to the Ignore Raycast layer. An interactable's Bounds collider and a 3D Flexalon Scroller's viewport collider don't block.

Lerp and Curve animations may look slower. FlexalonLerpAnimator and FlexalonCurveAnimator now advance once per frame, even when Flexalon runs several layout passes in that frame. If an animation advanced on each pass in Flexalon 1, it can appear slower with the same settings. Increase its speed to compensate.

Click distance is measured in canvas units on every Canvas. In Flexalon 1, Max Click Distance on a Flexalon Interactable was always measured in world units. In Flexalon 2, interactables under any Canvas measure it in canvas units, and the default is 40. The first time an interactable is enabled, if it or its parent has a RectTransform and its value is still the old default of 0.1, the value is updated to 40. Custom values aren't converted. On a World Space canvas, a value that was in world units is now read as canvas units, which is usually far too small for clicks to register, so recheck custom values on every Canvas, and update any script that sets MaxClickDistance in world units. Max Click Time now defaults to 1 second for new interactables.

The Flexalon singleton is now FlexalonCore

The layout engine now runs from a static FlexalonCore class, so it no longer needs a Flexalon GameObject in the scene. The Flexalon component still exists to hold scene settings (Update In Edit Mode, Update In Play Mode, and the input provider GameObject). Its static and instance methods are marked [Obsolete] and forward to FlexalonCore. The compiler warning at each call names the FlexalonCore replacement.

A Flexalon GameObject is no longer created automatically. In Flexalon 1, Flexalon.Get() always returned an object once a layout was enabled. It now returns null unless the scene contains a Flexalon component. Code like Flexalon.Get().UpdateDirtyNodes() will throw a NullReferenceException. Use FlexalonCore instead:

Flexalon 1Flexalon 2
Flexalon.Get().UpdateDirtyNodes()FlexalonCore.UpdateDirtyNodes()
Flexalon.Get().ForceUpdate()FlexalonCore.ForceUpdate()
Flexalon.GetNode(go) / Flexalon.GetOrCreateNode(go)FlexalonCore.GetNode(go) / FlexalonCore.GetOrCreateNode(go)
Flexalon.AddComponent<T>(go)FlexalonCore.AddComponent<T>(go)
Flexalon.Get().InputProvider = providerFlexalonCore.InputProvider = provider
Flexalon.GetInputProvider() (removed)FlexalonCore.InputProvider
Flexalon.Get().PreUpdate += Handler (removed)FlexalonCore.PreUpdate += Handler (now a static event)
Flexalon.Get().Nodes (removed)FlexalonCore.Nodes
Flexalon.Get().SkipInactiveObjects (removed)No replacement. Inactive objects are always skipped.
Flexalon.RecordFrameChanges = true (setter removed)No replacement. Flexalon records editor undo changes itself.

Flexalon.Get() and Flexalon.GetOrCreate() still return the scene's Flexalon component, so you can keep using them to read or set UpdateInEditMode and UpdateInPlayMode.

Custom components

These changes apply to any class that derives from FlexalonComponent, including custom layouts and adapters.

DoUpdate() was removed. Flexalon no longer calls every component each frame. Replace public override void DoUpdate() with a Unity Update() method, and only check for changes there if you need to:

// Flexalon 1
public override void DoUpdate()
{
    if (_target.hasChanged) MarkDirty();
}

// Flexalon 2
void Update()
{
    if (_target.hasChanged) MarkDirty();
}

UpdateProperties() and ResetProperties() must be idempotent. As in Flexalon 1, MarkDirty() calls UpdateProperties(), so your setters can keep relying on it to apply values to the node. What changed is when the two methods run:

  • UpdateProperties() runs every time the component is enabled, and on every MarkDirty() while the component is enabled and its GameObject is active. While the GameObject is inactive, MarkDirty() doesn't apply properties; they're applied when it's activated.
  • ResetProperties() now also runs when the component is disabled. In Flexalon 1 it only ran when the component was destroyed. A component that's disabled and then destroyed gets two calls in a row.

So calling either method more than once in a row must be safe, and UpdateProperties() should be cheap. Make sure ResetProperties() undoes everything UpdateProperties() does:

protected override void UpdateProperties()
{
    // Safe to repeat.
    Node.SetAdapter(this);

    // Unsubscribe first, so repeated
    // calls subscribe only once.
    _source.Changed -= OnChanged;
    _source.Changed += OnChanged;
}

protected override void ResetProperties()
{
    Node.SetAdapter(null);
    // Safe even when not subscribed.
    _source.Changed -= OnChanged;
}

If you registered with the node in DoOnEnable(), move that code to UpdateProperties(), and move the matching cleanup to ResetProperties().

MarkDirty() does nothing while the component is disabled. A disabled component doesn't affect layout, so it can't request a layout update. When the component is enabled again, Flexalon applies its properties and updates the layout.

Use the targeted dirty methods inside your component. FlexalonComponent now has protected MarkDirtyArrange(), MarkParentDirtyArrange(), and MarkDirtyConstrain() methods. Call MarkDirtyArrange() from a setter when a change only affects where children are placed. These methods don't call UpdateProperties(), so use MarkDirty() when a change affects measured sizes or what UpdateProperties() applies. Call them on the component, not on Node, so that Flexalon ignores changes to disabled components.

Custom layouts

See Custom Layout for the complete interface.

Call base.UpdateProperties() and base.ResetProperties(). FlexalonLayoutBase now registers your layout with its node in UpdateProperties() and unregisters it in ResetProperties(). If your layout overrides either method without calling the base method, your layout won't run.

protected override void UpdateProperties()
{
    base.UpdateProperties(); // Required in Flexalon 2.
    // Your code here.
}

protected override void ResetProperties()
{
    base.ResetProperties(); // Required in Flexalon 2.
    // Your code here.
}

node.FlexalonObject and node.Result return interfaces. They now return IFlexalonObject and IFlexalonResult, so layouts can also work with virtual nodes that don't have a GameObject. Properties like Width, Margin, Padding, TargetPosition, and LayoutBounds work as before. Code that used these values as Unity objects won't compile:

// Flexalon 1
if (child.FlexalonObject)
{
    var go = child.FlexalonObject.gameObject;
}

// Flexalon 2
if (child.HasFlexalonObject)
{
    var go = child.GameObject;
}

Members of FlexalonResult are now properties, not fields, so you can no longer pass them as ref or out arguments.

ApplyScaleAndRotation() was removed. Call MarkDirty() or MarkDirtyArrange() on your component instead.

Layouts only update when something they depend on changes. In Flexalon 1, a change anywhere in a hierarchy measured and arranged the whole hierarchy again. In Flexalon 2, only the affected nodes are updated. This affects custom layouts in two ways:

  • Mark your layout dirty when its inputs change. If your layout reads values that Flexalon doesn't track, such as other objects' positions, time, or data from your game, call MarkDirty() or MarkDirtyArrange() when those values change. Your layout won't update otherwise.
  • Arrange can run without Measure. After MarkDirtyArrange(), or when a parent only needs to reposition its children, Flexalon calls Arrange without calling Measure first. If you compute values in Measure and use them in Arrange, either compute them again in Arrange, or store them in serialized fields so they're still available after a scene loads.

Optional: ResolveChildAlign() on FlexalonLayoutBase lets your layout respect the new alignment overrides on Flexalon Object.

Custom adapters

See Adapters for the complete interface.

Account for pivots in Measure(). The returned bounds are relative to the transform's origin, so adapters that measure a RectTransform should offset Bounds.center by the RectTransform's pivot.

Optional: To refresh the layout in the editor when an asset such as a sprite or mesh changes, use FlexalonAssetTracker.Track() and dispose of the returned handle in ResetProperties(). If you start tracking in UpdateProperties(), only do it when you don't already have a handle, since that method runs on every MarkDirty().

Custom animators

See Custom Animators for the complete interface.

Apart from its new name, the IFlexalonTransformUpdater interface is unchanged, but Flexalon calls it differently:

  • PreUpdate and the Update* methods are only called while a node has a pending transform change. In Flexalon 1, Flexalon called PreUpdate on every node every frame. In Flexalon 2, a node that has reached its target is skipped until its layout result changes. Keep returning false from UpdatePosition, UpdateRotation, UpdateScale, or UpdateRectSize until the animation finishes. To start animating again for a reason Flexalon doesn't know about, call node.MarkDirtyTransformUpdater(TransformFlags.Position) with the parts of the transform that should be updated.
  • node.Result returns IFlexalonResult, and TargetRectSize is now a Vector2 instead of a Vector3.

If you derive from FlexalonLerpAnimator: its OnEnable() and OnDisable() methods are now protected virtual. If your subclass defines OnEnable() or OnDisable(), change them to protected override and call the base method. Its settings properties are now virtual, and it adds separate rotation and scale interpolation settings.

Custom input providers

Apart from its new name, the IFlexalonInputProvider interface is unchanged. To set your provider from code, use FlexalonCore.InputProvider = myProvider (see the table above). Setting it to null restores the default mouse input provider. If you assigned your provider to the Flexalon component in the inspector, that still works. See Custom Input Provider.

InvalidDropMode.ContinueDrag isn't supported with InputMode.External, and behaves like InvalidDropMode.None.

Custom modifiers and constraints

Apart from their new names, the IFlexalonModifier and IFlexalonConstraint interfaces are unchanged. Modifiers on a GameObject with a FlexalonRow component aren't called, because the parent table or grid places the row's children. Constraint components now update when their target moves, rotates, or resizes, so you don't need to mark them dirty yourself.

Using built-in components from scripts

Built-in component properties are unchanged, except:

  • FlexalonGridCell now inherits Column, Row, Layer, and Cell from the new FlexalonCell. Your existing code works, but use Flexalon Cell for new objects. It also works with Table Layout.
  • FlexalonGridLayout.GetChildAt() and GetChildrenAt() are now defined on the shared base class FlexalonCellLayout, so they work for both Grid and Table layouts.
  • FlexalonDragTarget.DragTargets now returns a HashSet<FlexalonDragTarget>.
  • FlexalonInteractable.DragTargetChanged still works. DragTargetChange is the new name for it.