RectTransform
RectTransform controls where a UI element sits inside its parent rectangle.
Every UIElement has a rectTransform property. You normally use it to set:
- where the element is anchored
- which point inside the element is used as its pivot
- the element's position relative to its anchor
- the element's size, scale, and rotation
Namespace:
namespace Lenga\Engine\UI;
final class RectTransform
Basic Usage
This example places a button near the top-right corner of its parent. The code belongs in a Behaviour class attached to a GameObject in the scene.
use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\Vector2;
use Lenga\Engine\SceneManagement\Scene;
final class HudController extends Behaviour
{
public function start(): void
{
$canvas = Scene::getActive()?->findCanvas('Canvas');
$button = $canvas?->findButtonByName('Pause Button');
if ($button === null) {
return;
}
$button->rectTransform->anchorMin = new Vector2(1.0, 0.0);
$button->rectTransform->anchorMax = new Vector2(1.0, 0.0);
$button->rectTransform->pivot = new Vector2(1.0, 0.0);
$button->rectTransform->anchoredPosition = new Vector2(-24.0, 24.0);
$button->rectTransform->sizeDelta = new Vector2(160.0, 44.0);
}
}
Coordinate Values
anchorMin, anchorMax, and pivot use normalized coordinates:
x = 0.0means leftx = 0.5means centerx = 1.0means righty = 0.0means topy = 0.5means middley = 1.0means bottom
For example, (1.0, 0.0) means top right.
Properties
| Property | Type | Description |
|---|---|---|
$anchorMin |
Vector2 |
The lower normalized anchor value. When it matches $anchorMax, the element is anchored to a single point. |
$anchorMax |
Vector2 |
The upper normalized anchor value. Use a different value from $anchorMin when the element should stretch with its parent. |
$pivot |
Vector2 |
The point inside the element that is positioned, rotated, and scaled. |
$anchoredPosition |
Vector2 |
The offset from the anchor reference point to the element's pivot. |
$sizeDelta |
Vector2 |
The element's size when it is anchored to a single point, or the size adjustment when it is stretched. |
$scale |
Vector2 |
The local UI scale. |
$rotation |
float |
The element rotation in degrees. |
Usage Notes
Use anchoredPosition, not offsetMin or offsetMax, for the standard Lenga UI workflow.
The Inspector includes a 3x3 anchor preset grid for common anchor points such as top-left, middle-center, and bottom-right. Those presets update the anchors and pivot together while preserving the element's current visual position.
For a beginner-friendly walkthrough, see Lay Out UI with Anchors, Buttons, and Parenting.