NinePatchRect
Resizable textured panel with preserved border regions.
NinePatchRect splits a texture into nine regions using four PatchMargin values: corners retain local dimensions while edges and center stretch or repeat according to AxisStretchHorizontal/Vertical. Use it for decorative frames when ordinary TextureRect stretching distorts corners.
Assign Texture, set nonnegative left, right, top and bottom border thicknesses, then size the Control. Margins use source-region pixels; their sums must fit inside that region. Leave positive center width/height for repeated areas. The setter validates Side but does not constrain the margin value itself: validate external data.
Intrinsic minimum size is (Left + Right, Top + Bottom), even without Texture. For example, horizontal borders 8/12 and vertical borders 6/10 produce minimum 20 × 16. CustomMinimumSize can increase it. PatchMargin defines texture slices rather than content padding: NinePatchRect is not a Container and does not lay out children. Add an appropriate container for layout; consider PanelContainer for a theme-based panel.
RegionRect selects a source area in pixels rather than 0…1 UVs. A zero Rect2 uses the whole texture. Stretch scales a region; Tile repeats with a potentially partial last tile; TileFit adjusts repetition scale to fit a whole number. Each axis is configured separately. DrawCenter = false only suppresses center drawing, without changing minimum size or creating a child-content area.
TextureChanged is emitted when the Texture reference changes, including clearing to null; assigning the same resource again does not emit it. Changes to the resource itself refresh drawing without emitting this NinePatchRect signal. Texture remains shared. MouseFilter defaults to Ignore so the decorative frame does not intercept mouse input.
Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Constructors1
NinePatchRectConstructor
public NinePatchRect();Creates a decorative frame without a texture: margins 0, DrawCenter = true, both axes Stretch, zero RegionRect, MouseFilter = Ignore.
Properties9
AxisStretchHorizontalProperty
public NinePatchRect.AxisStretchMode AxisStretchHorizontal { get; set; }Horizontal interior-region mode: Stretch scales, Tile repeats, TileFit adjusts to a whole repeat count. Default Stretch. Does not change the PatchMargin-derived minimum.
AxisStretchVerticalProperty
public NinePatchRect.AxisStretchMode AxisStretchVertical { get; set; }Vertical interior-region mode: Stretch, Tile or TileFit. Independent of the horizontal setting; PatchMargin defines top/bottom border thickness.
DrawCenterProperty
public bool DrawCenter { get; set; }Draw the center region, default true. false retains the frame and existing minimum size. Does not control children, layout or mouse hit testing.
PatchMarginBottomProperty
public int PatchMarginBottom { get; set; }Bottom border thickness in source-region pixels, default 0. Contributes to minimum-size sums rather than content padding. Use a nonnegative value that fits the source area.
PatchMarginLeftProperty
public int PatchMarginLeft { get; set; }Left border thickness in source-region pixels, default 0. Contributes to minimum-size sums rather than content padding. Use a nonnegative value that fits the source area.
PatchMarginRightProperty
public int PatchMarginRight { get; set; }Right border thickness in source-region pixels, default 0. Contributes to minimum-size sums rather than content padding. Use a nonnegative value that fits the source area.
PatchMarginTopProperty
public int PatchMarginTop { get; set; }Top border thickness in source-region pixels, default 0. Contributes to minimum-size sums rather than content padding. Use a nonnegative value that fits the source area.
RegionRectProperty
public Rect2 RegionRect { get; set; }Source area in texture pixels rather than normalized UVs. Zero Rect2 means the whole texture. Does not change PatchMargin or Control minimum; choose an area that fits all borders.
TextureProperty
public Texture2D Texture { get; set; }Shared Texture2D to slice. Reference replacement, including null, emits TextureChanged; assigning the same reference does not. Margins rather than texture dimensions define minimum size.
Methods12
GetHAxisStretchModeNinePatchRect.AxisStretchMode
public NinePatchRect.AxisStretchMode GetHAxisStretchMode();Horizontal interior-region mode: Stretch scales, Tile repeats, TileFit adjusts to a whole repeat count. Default Stretch. Does not change the PatchMargin-derived minimum.
NinePatchRect.AxisStretchModeCurrent stored setting.
GetPatchMarginint
public int GetPatchMargin(Side margin);Returns selected border thickness in source-region pixels. A slicing boundary rather than child-content padding.
| Name | Type | Default |
|---|---|---|
margin | Side | — |
marginSelected Side.Left, Top, Right or Bottom.
intCurrent stored setting.
GetRegionRectRect2
public Rect2 GetRegionRect();Source area in texture pixels rather than normalized UVs. Zero Rect2 means the whole texture. Does not change PatchMargin or Control minimum; choose an area that fits all borders.
Rect2Current stored setting.
GetTextureTexture2D
public Texture2D GetTexture();Shared Texture2D to slice. Reference replacement, including null, emits TextureChanged; assigning the same reference does not. Margins rather than texture dimensions define minimum size.
Texture2DCurrent shared Texture2D reference, or null.
GetVAxisStretchModeNinePatchRect.AxisStretchMode
public NinePatchRect.AxisStretchMode GetVAxisStretchMode();Vertical interior-region mode: Stretch, Tile or TileFit. Independent of the horizontal setting; PatchMargin defines top/bottom border thickness.
NinePatchRect.AxisStretchModeCurrent stored setting.
IsDrawCenterEnabledbool
public bool IsDrawCenterEnabled();Draw the center region, default true. false retains the frame and existing minimum size. Does not control children, layout or mouse hit testing.
boolCurrent stored setting.
SetDrawCentervoid
public void SetDrawCenter(bool drawCenter);Draw the center region, default true. false retains the frame and existing minimum size. Does not control children, layout or mouse hit testing.
| Name | Type | Default |
|---|---|---|
drawCenter | bool | — |
drawCenterDraw the center region, default true. false retains the frame and existing minimum size. Does not control children, layout or mouse hit testing.
SetHAxisStretchModevoid
public void SetHAxisStretchMode(NinePatchRect.AxisStretchMode mode);Horizontal interior-region mode: Stretch scales, Tile repeats, TileFit adjusts to a whole repeat count. Default Stretch. Does not change the PatchMargin-derived minimum.
| Name | Type | Default |
|---|---|---|
mode | NinePatchRect.AxisStretchMode | — |
modeHorizontal interior-region mode: Stretch scales, Tile repeats, TileFit adjusts to a whole repeat count. Default Stretch. Does not change the PatchMargin-derived minimum.
SetPatchMarginvoid
public void SetPatchMargin(Side margin, int value);Sets selected border thickness in pixels and refreshes minimum size. Side must be valid; negative/excessive sizes are not automatically constrained. Keep border sums within the source region.
| Name | Type | Default |
|---|---|---|
margin | Side | — |
value | int | — |
marginSelected Side.Left, Top, Right or Bottom.
valueNonnegative border thickness in source-region pixels; validate opposite-border sums.
SetRegionRectvoid
public void SetRegionRect(Rect2 rect);Source area in texture pixels rather than normalized UVs. Zero Rect2 means the whole texture. Does not change PatchMargin or Control minimum; choose an area that fits all borders.
| Name | Type | Default |
|---|---|---|
rect | Rect2 | — |
rectSource area in texture pixels rather than normalized UVs. Zero Rect2 means the whole texture. Does not change PatchMargin or Control minimum; choose an area that fits all borders.
SetTexturevoid
public void SetTexture(Texture2D texture);Shared Texture2D to slice. Reference replacement, including null, emits TextureChanged; assigning the same reference does not. Margins rather than texture dimensions define minimum size.
| Name | Type | Default |
|---|---|---|
texture | Texture2D | — |
textureShared Texture2D, or null to clear drawing.
SetVAxisStretchModevoid
public void SetVAxisStretchMode(NinePatchRect.AxisStretchMode mode);Vertical interior-region mode: Stretch, Tile or TileFit. Independent of the horizontal setting; PatchMargin defines top/bottom border thickness.
| Name | Type | Default |
|---|---|---|
mode | NinePatchRect.AxisStretchMode | — |
modeVertical interior-region mode: Stretch, Tile or TileFit. Independent of the horizontal setting; PatchMargin defines top/bottom border thickness.
Signals and events1
TextureChangedEvent
public event Action TextureChanged;Texture reference changed, including null. Not emitted for same-resource reassignment or the resource’s own Changed. Observe the resource’s signal for internal changes.
NinePatchRect.AxisStretchMode
| Value | Description |
|---|---|
Stretch = 0 | Stretches the center texture across the NinePatchRect. This may cause the texture to be distorted. |
Tile = 1 | Repeats the center texture across the NinePatchRect. This won't cause any visible distortion. The texture must be seamless for this to work without displaying artifacts between edges. |
TileFit = 2 | Repeats the center texture across the NinePatchRect, but will also stretch the texture to make sure each tile is visible in full. This may cause the texture to be distorted, but less than |