Class UIFactory
Utility class for constructing and configuring various UI elements in Unity.
public static class UIFactory
- Inheritance
-
UIFactory
- Inherited Members
Remarks
Provides static helpers for building complex hierarchies—panels, text, layouts, scroll views, buttons, etc.— while handling RectTransform setup, layout components, and consistent styling in one place.
Methods
BindAcceptButton(Button, Text, string, UnityAction)
Updates a button's display text and binds the supplied click callback.
public static void BindAcceptButton(Button btn, Text label, string text, UnityAction callback)
Parameters
btnButtonButton to configure.
labelTextText component associated with the button.
textstringText to assign to the label.
callbackUnityActionDelegate invoked when the button is clicked.
ButtonRow(string, Transform, float, TextAnchor)
Creates a container configured with a UnityEngine.UI.HorizontalLayoutGroup for arranging buttons in a row.
public static GameObject ButtonRow(string name, Transform parent, float spacing = 12, TextAnchor alignment = 4)
Parameters
namestringName of the row GameObject.
parentTransformTransform that will hold the row.
spacingfloatSpacing between children in the layout (defaults to 12).
alignmentTextAnchorChild alignment; defaults to UnityEngine.TextAnchor.MiddleCenter.
Returns
- GameObject
The created row GameObject.
ButtonWithLabel(string, string, Transform, Color, float, float)
Creates a rectangular button with a centered text label and returns useful components.
public static (GameObject, Button, Text) ButtonWithLabel(string name, string label, Transform parent, Color bgColor, float Width, float Height)
Parameters
namestringName assigned to the button GameObject.
labelstringText displayed in the button.
parentTransformParent transform for the button.
bgColorColorBackground color for the button image.
WidthfloatDesired width in pixels.
HeightfloatDesired height in pixels.
Returns
- (GameObject, Button, Text)
A tuple of (GameObject, Button, Text) for further configuration.
ClearChildren(Transform)
Destroys all child GameObjects of the provided parent Transform.
public static void ClearChildren(Transform parent)
Parameters
parentTransformTransform to clear.
CreateQuestRow(string, Transform, out GameObject, out GameObject)
Constructs a quest row entry with dedicated icon/text panels and common layout settings.
public static GameObject CreateQuestRow(string name, Transform parent, out GameObject iconPanel, out GameObject textPanel)
Parameters
namestringName suffix applied to generated GameObjects.
parentTransformParent transform for the row.
iconPanelGameObjectOutputs the panel intended for quest icons.
textPanelGameObjectOutputs the panel intended for quest title/description.
Returns
- GameObject
The fully constructed row GameObject.
CreateRowButton(GameObject, UnityAction, bool)
Adds a UnityEngine.UI.Button component to a row container and wires up click/interactability.
public static void CreateRowButton(GameObject go, UnityAction clickHandler, bool enabled)
Parameters
goGameObjectTarget GameObject that already has an UnityEngine.UI.Image for visuals.
clickHandlerUnityActionCallback to invoke on button click.
enabledboolWhether the resulting button should start as interactable.
CreateTextBlock(Transform, string, string, bool)
Creates a stacked title/subtitle block and optionally appends a completed label.
public static void CreateTextBlock(Transform parent, string title, string subtitle, bool isCompleted)
Parameters
parentTransformTransform that will receive the text elements.
titlestringPrimary heading displayed in bold.
subtitlestringSecondary descriptive text below the title.
isCompletedboolAdds an "Already Delivered" status line when true.
FitContentHeight(RectTransform)
Ensures a content UnityEngine.RectTransform grows tall enough to fit its children.
public static void FitContentHeight(RectTransform content)
Parameters
contentRectTransformThe RectTransform whose vertical size should adapt to its children.
Remarks
Adds a UnityEngine.UI.ContentSizeFitter if one is missing and configures it for preferred vertical sizing.
HorizontalLayoutOnGO(GameObject, int, int, int, int, int, TextAnchor)
Adds a UnityEngine.UI.HorizontalLayoutGroup to a GameObject and preconfigures its sizing behavior.
public static void HorizontalLayoutOnGO(GameObject go, int spacing = 10, int padLeft = 0, int padRight = 0, int padTop = 0, int padBottom = 0, TextAnchor alignment = 4)
Parameters
goGameObjectGameObject that should host the layout group.
spacingintSpacing between children; defaults to 10.
padLeftintLeft padding value.
padRightintRight padding value.
padTopintTop padding value.
padBottomintBottom padding value.
alignmentTextAnchorChild alignment; defaults to UnityEngine.TextAnchor.MiddleCenter.
Panel(string, Transform, Color, Vector2?, Vector2?, bool)
Creates a UI panel GameObject with an Image background and configurable anchoring.
public static GameObject Panel(string name, Transform parent, Color bgColor, Vector2? anchorMin = null, Vector2? anchorMax = null, bool fullAnchor = false)
Parameters
namestringName of the panel GameObject.
parentTransformTransform that becomes the parent of the panel.
bgColorColorColor applied to the panel background.
anchorMinVector2?Optional minimum anchor; defaults to centered anchor.
anchorMaxVector2?Optional maximum anchor; defaults to centered anchor.
fullAnchorboolWhen true, stretches the panel to fill its parent regardless of anchor arguments.
Returns
- GameObject
The created panel GameObject (with RectTransform/Image components attached).
RoundedButtonWithLabel(string, string, Transform, Color, float, float, int, Color)
Creates a rounded button composed of a mask container, button, and centered text label.
public static (GameObject, Button, Text) RoundedButtonWithLabel(string name, string label, Transform parent, Color bgColor, float width, float height, int fontSize, Color textColor)
Parameters
namestringName of the underlying button GameObject.
labelstringText shown inside the button.
parentTransformParent transform for the mask container.
bgColorColorBackground color applied to the inner button.
widthfloatPreferred width for the control (also applied to a LayoutElement).
heightfloatPreferred height for the control.
fontSizeintFont size for the label.
textColorColorColor of the label text.
Returns
- (GameObject, Button, Text)
The tuple (mask container GameObject, Button component, Text component).
ScrollableVerticalList(string, Transform, out ScrollRect)
Builds a ScrollRect hierarchy configured for vertical scrolling and returns the content RectTransform.
public static RectTransform ScrollableVerticalList(string name, Transform parent, out ScrollRect scrollRect)
Parameters
namestringName of the root scroll view GameObject.
parentTransformTransform that will own the scroll view.
scrollRectScrollRectOutputs the created UnityEngine.UI.ScrollRect component.
Returns
- RectTransform
The RectTransform of the content container where list items should be added.
SetIcon(Sprite, Transform)
Sets an icon as a child of the specified parent transform with the given sprite.
public static void SetIcon(Sprite sprite, Transform parent)
Parameters
spriteSpriteThe sprite to be used as the icon.
parentTransformThe transform that will act as the parent of the icon.
SetLayoutGroupPadding(LayoutGroup, int, int, int, int)
Convenience helper to configure the padding on any UnityEngine.UI.LayoutGroup.
public static void SetLayoutGroupPadding(LayoutGroup layoutGroup, int left, int right, int top, int bottom)
Parameters
layoutGroupLayoutGroupTarget layout group.
leftintLeft padding value.
rightintRight padding value.
topintTop padding value.
bottomintBottom padding value.
Text(string, string, Transform, int, TextAnchor, FontStyle)
Creates a UnityEngine.UI.Text element configured with the supplied content and styling.
public static Text Text(string name, string content, Transform parent, int fontSize = 14, TextAnchor anchor = 0, FontStyle style = 0)
Parameters
namestringName of the GameObject to create.
contentstringInitial string displayed inside the text component.
parentTransformTransform that will contain the new text element.
fontSizeintFont size to apply, defaults to 14.
anchorTextAnchorText alignment; defaults to UnityEngine.TextAnchor.UpperLeft.
styleFontStyleFont style flag; defaults to UnityEngine.FontStyle.Normal.
Returns
- Text
The configured Text(string, string, Transform, int, TextAnchor, FontStyle) component.
TopBar(string, Transform, string, float, int, int, int, int)
Creates a top-bar container with padding, title text, and layout metadata.
public static GameObject TopBar(string name, Transform parent, string title, float topbarSize, int paddingLeft, int paddingRight, int paddingTop, int paddingBottom)
Parameters
namestringName of the bar GameObject.
parentTransformTransform that will contain the bar.
titlestringDisplay text shown in the bar.
topbarSizefloatNormalized height (Y anchor) reserved for the bar.
paddingLeftintLeft padding applied by the layout group.
paddingRightintRight padding applied by the layout group.
paddingTopintTop padding applied by the layout group.
paddingBottomintBottom padding applied by the layout group.
Returns
- GameObject
The instantiated bar GameObject.
VerticalLayoutOnGO(GameObject, int, RectOffset?)
Adds and configures a UnityEngine.UI.VerticalLayoutGroup on the supplied GameObject.
public static void VerticalLayoutOnGO(GameObject go, int spacing = 10, RectOffset? padding = null)
Parameters
goGameObjectGameObject that should host the layout.
spacingintSpacing between children in pixels (default 10).
paddingRectOffsetOptional padding override; defaults to 10px on every side.