Layouts
A layout saves the open windows, their bounds and states, and the tab groups (tab order and active tab).


Method on Workspace |
Result |
|---|---|
SaveLayout(string name) |
Saves the current layout. |
LoadLayoutAsync(string name) |
Closes the windows, then reopens the saved ones. With no saved layout of that name, it opens the preset of that name. Returns false if neither exists. |
GetSavedLayouts() |
Names of the saved layouts. |
DeleteLayout(string name) |
Deletes a layout. A preset of the same name stays. |
CaptureLayout() |
Returns the open windows as layout JSON. |
ApplyLayoutAsync(string json) |
Validates a layout, asks all affected windows to close, then applies it. Returns false for invalid data or a canceled close. |
SaveSession() |
Saves the session layout. |
RestoreSessionAsync() |
Loads the session layout. |
- Layouts are JSON in
PlayerPrefs. The keys areUIToolbox.Layout.{LayoutName}.{name}.LayoutNameis the workspace attributelayout-name. - A layout name cannot be blank,
Session,Index, or contain a line break. These names and characters belong to the storage format. The layout methods throwArgumentExceptionfor such a name. - Apply, load, switch, reset, and session restore run in request order. Saves and deletes requested during these operations wait their turn.
CaptureLayout()reads the current state immediately; await a layout operation before capturing its result. - A canceled close leaves the arrangement unchanged. Each affected window receives one close query. A saved layout that fails validation or receives a cancellation does not fall back to its preset. A text that fails validation changes no window and logs a warning that names the operation.
- A layout stores window ids, not content. Register every window before you load a layout.
- These methods do nothing until the workspace is attached to a panel.
- The session restore on attach waits one scheduler tick, so windows and dock zones declared in UXML are registered first. The session is not saved while a layout loads.
- The workspace reads the session once. A workspace that leaves its panel and comes back keeps its windows as they were. A workspace that leaves before its first restore does not save over the session, and restores it when it comes back.
- A tab group's minimized or maximized state is saved and restored.
- A maximized window saves its bounds from before the maximize, so a restore after the load returns it to them. A window that was minimized while it was maximized loads minimized and comes back maximized, as before the save.
- A window that was hidden by minimize (see Minimize modes) is saved as minimized and hides again when the layout loads, with the minimize mode that the window has then.
using System.Threading.Tasks;
using UIToolbox;
public static class LayoutMenu
{
public static void Save(Workspace workspace) =>
workspace.SaveLayout("Trading");
public static async Task LoadAsync(Workspace workspace)
{
if (!await workspace.LoadLayoutAsync("Trading"))
await workspace.ArrangeCascadeAsync();
}
}
Layout presets
A preset is a named layout that ships with the game. The user switches between the named layouts, and each layout keeps the user's changes.
Member of Workspace |
Result |
|---|---|
layout-presets (LayoutPresetsPath) |
Adds each TextAsset in this Resources folder as a preset, named after the asset, in name order. |
AddLayoutPreset(string name, string json) |
Adds a preset, or replaces the preset with that name. Throws ArgumentException for an empty name or JSON. |
GetLayoutPresets() |
Names of the presets, in the order they were added. |
SwitchLayoutAsync(string name) |
Saves the open windows under CurrentLayout, then loads the named layout: the saved one, else the preset. Returns false if neither exists. If the current layout is a preset and no window is open, the switch does not save an empty layout, so the preset opens the next time. |
CurrentLayout |
The name of the last switch, or null before the first switch. |
LayoutSwitched |
Raised after a switch, with the layout name. |
ResetLayoutAsync(string name) |
Deletes the saved changes to a preset. If the preset is the current layout, it opens the preset again. Returns false if no preset has that name. |
- To make a preset, arrange the windows, call
CaptureLayout(), and save the text as a.jsonfile in the Resources folder. - A preset can also be written by hand. It needs a
Windowsarray, which may be empty. Each window and group needs anIdand finiteBoundswith positive width and height.TabGroupsis optional. A group needs a nonemptyTabIdsarray, and its active tab must belong to that array. A tab cannot belong to two groups. Unknown window ids are skipped. Other fields are optional; an omitted state opens the window normally.TabGroupsmakes a tab group with the listed windows as tabs:
{
"Windows": [
{ "Id": "inventory",
"Bounds": { "Left": 10, "Top": 10,
"Width": 460, "Height": 370 } }
],
"TabGroups": [
{
"Id": "sideTabs",
"Bounds": { "Left": 480, "Top": 10,
"Width": 600, "Height": 500 },
"TabIds": [ "map", "quests" ],
"ActiveTabId": "map"
}
]
}
- The bounds are pixels relative to the top-left corner of the viewport, so a workspace toolbar does not cover the windows. A load clamps each window into the viewport, so a preset made on a large screen stays visible on a small screen. All saved layouts use the same bounds.
- A captured group stores its chrome template independently of its tab order. Older layouts without a group template use the first available tab's template. Dock bounds follow the current viewport and the declared dock sizes; their saved tab membership restores.
CurrentLayoutis not saved. After a restart, the first switch does not save the session arrangement under a layout name.- The WorkspaceDemo example has three presets (Desk, Stack and Tabs) in
Assets/UIToolbox/Examples/Resources/UIToolboxSamples/WorkspaceLayouts, with a switch bar above the workspace. The Tabs preset puts three windows in one tab group. - Its Receiver decodes three distress signals using clues in Notes. Group Receiver and Notes, then select Save arrangement in Glass windows. Close or rearrange the windows and select Restore arrangement. The restored arrangement keeps decoded messages and the tuned channel. This checkpoint lasts until the example closes; the scene controller owns the rescue state separately from window chrome.
workspace.LayoutSwitched += name => statusLabel.text = $"Layout: {name}";
foreach (var name in workspace.GetLayoutPresets())
bar.Add(new Button(() =>
AsyncRunner.Run(() => workspace.SwitchLayoutAsync(name),
"Switch layout")) { text = name });
Many windows
The workspace reads the order of the windows once for a focus, an arrangement and a hit test. These times are for 200 windows with the animations off:
| Operation | Time with 200 windows |
|---|---|
| Focus a window | about 0.2 ms |
ArrangeCascadeAsync, synchronous part |
about 50 ms |
| Open 200 windows | about 200 ms |
An arrangement restores and focuses each window. With animations on, each window also runs its restore animation, so turn off the animations (AnimationsEnabled) before you arrange many windows at once.