UGTK cat mascotUGTKengine within an engine Request access

UGTK / Toolkit / Libraries Extensions / AnchorsLibrary Completed

Libraries Extensions — UGTK

Anchor Library

Summary

The Anchor Library sets a RectTransform's anchors from a preset, in one call — the thing Unity only lets you do by hand in the inspector's anchor popup. It is what you need when the layout is decided at runtime: an element that must stick to the top-left on one screen and to the bottom-center on another.

The important part: it preserves the on-screen position and size. Changing anchors by hand normally makes the element jump; here the previous anchoredPosition, sizeDelta and pivot are read first and restored after, so only the anchoring behaviour changes, not the look.


Content


Modules Dependencies

None. A single static extension class, no UGTK reference.


Setup

Nothing to set up: no component, no prefab. Add UGTK.LibrariesExtensions.AnchorsLibrary to your assembly definition references and call the extension method on any RectTransform.


How To Use

targetRectTransform.SetAnchor(E_AnchorPosition.TopLeft);
targetRectTransform.SetAnchor(E_AnchorPosition.MiddleCenter);
targetRectTransform.SetAnchor(E_AnchorPosition.MatchRectTransform);

The presets — the nine classic positions plus one special value:

Left Center Right
Top TopLeft TopCenter TopRight
Middle MiddleLeft MiddleCenter MiddleRight
Bottom BottomLeft BottomCenter BottomRight

MatchRectTransform is the "stretch" case: the anchors are collapsed onto the element's current rectangle, so from then on the element scales with its parent instead of keeping a fixed size. It is the programmatic version of Unity's alt+click stretch preset.

Good to know:


Testing Scene

Open Debug/AnchorsLibraryTest.unity. A white square sits in the middle of the canvas, a panel of ten buttons sits bottom right, and AnchorTest carries Mn_AnchorLibraryTest, which is wired to both. Select AnchorTest and right-click the component header: the same presets the buttons apply in play mode are there as context menu entries, so the whole set can be walked with the editor stopped.

What to do, in order:

  1. Run Apply preset once. The Output box reports the preset, the two anchors, the size, and how far the element moved.
  2. Run Next preset nine times to walk the whole enum.
  3. Watch the last line of the report at every step: moved 0 px, resized 0 px.
  4. Press Play and use the on-screen buttons instead. They call exactly the same method.
  5. Resize the Game view. Now the presets differ: an element anchored to a corner keeps its size and stays glued to that corner, while MatchRectTransform grows and shrinks with the parent.

Step 3 is the whole point. Setting the anchors on their own moves the element, because anchoredPosition and the offsets are measured from the anchor - and the anchor has just moved. The utility measures the rect against the parent first and writes it back afterwards, which is what Unity's own anchor preset widget does.

MatchRectTransform is the odd one out: it turns the element's current rect into anchors and drops the offsets, so it stays exactly where it is and starts scaling with its parent. In the report you can see it as a pair of fractional anchors, (0.47, 0.45) and (0.53, 0.55), instead of a corner.


Technical Info

Path Content
Scripts/E_AnchorPosition.cs The preset enum: the nine positions plus MatchRectTransform
Scripts/AnchorUtility.cs The static class: SetAnchor(this RectTransform, E_AnchorPosition), which saves position/size/pivot, writes anchorMin/anchorMax and restores them
Debug/Mn_AnchorLibraryTest.cs Example component used by the test scene: wires the ten buttons and exposes the same presets as context menu entries

© 2026 Marcello De Bonis. All rights reserved

Real dependenciesOpen in the map →

Depends on 0

  • Standalone: no other module required.

Used by 1

Measured from the repository: code references (asmdef) plus prefab and asset GUIDs. Importing this module alone brings in 0 modules in total.