unity-ui

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Unity UI Skills

Unity UI 技能

BATCH-FIRST: Use
ui_create_batch
when creating 2+ UI elements.
优先批量操作:创建2个及以上UI元素时使用
ui_create_batch

Guardrails

使用规范

Mode: Full-Auto required
DO NOT (common hallucinations):
  • ui_create_canvas
    is the correct name — NOT
    ui_add_canvas
  • ui_create_label
    does not exist → use
    ui_create_text
  • ui_create_checkbox
    does not exist → use
    ui_create_toggle
  • ui_set_color
    does not exist → use
    component_set_property
    on Image/Text component
  • Do not confuse UGUI (this module) with UI Toolkit (uitoolkit module)
Routing:
  • For UI Toolkit (UXML/USS) → use
    uitoolkit
    module
  • For XR-compatible UI → use
    xr
    module's
    xr_setup_ui_canvas
    to convert Canvas to World Space
  • For text updates →
    ui_set_text
    (this module)
  • For layout arrangement →
    ui_layout_children
    ,
    ui_align_selected
    ,
    ui_distribute_selected
模式要求:必须使用全自动模式
禁止操作(常见的错误生成内容):
  • 正确的方法名是
    ui_create_canvas
    ,不是
    ui_add_canvas
  • 不存在
    ui_create_label
    方法,请使用
    ui_create_text
  • 不存在
    ui_create_checkbox
    方法,请使用
    ui_create_toggle
  • 不存在
    ui_set_color
    方法,请针对Image/Text组件使用
    component_set_property
    修改颜色
  • 不要混淆UGUI(本模块)和UI Toolkit(uitoolkit模块)
路由规则
  • 若使用UI Toolkit(UXML/USS)→ 请使用
    uitoolkit
    模块
  • 若需要XR兼容的UI → 请使用
    xr
    模块的
    xr_setup_ui_canvas
    将Canvas转换为世界空间模式
  • 若需要更新文本内容 → 请使用本模块的
    ui_set_text
  • 若需要布局排列 → 请使用
    ui_layout_children
    ui_align_selected
    ui_distribute_selected

Skills Overview

技能概览

Single ObjectBatch VersionUse Batch When
ui_create_*
ui_create_batch
Creating 2+ UI elements
Query/Utility Skills:
  • ui_set_text
    - Update text content
  • ui_find_all
    - Find UI elements
  • ui_set_rect
    - Set RectTransform size/position
  • ui_set_anchor
    - Set anchor preset
  • ui_layout_children
    - Arrange children in layout
  • ui_align_selected
    - Align selected elements
  • ui_distribute_selected
    - Distribute selected elements
  • ui_set_image
    - Set Image properties (type/fill/sprite)
  • ui_add_layout_element
    - Add/configure LayoutElement
  • ui_add_canvas_group
    - Add/configure CanvasGroup
  • ui_add_mask
    - Add Mask or RectMask2D
  • ui_add_outline
    - Add Shadow/Outline effect
  • ui_configure_selectable
    - Configure Selectable colors/transition/navigation

单对象方法批量版本适用场景
ui_create_*
ui_create_batch
创建2个及以上UI元素
查询/工具类技能
  • ui_set_text
    - 更新文本内容
  • ui_find_all
    - 查找UI元素
  • ui_set_rect
    - 设置RectTransform的尺寸/位置
  • ui_set_anchor
    - 设置锚点预设
  • ui_layout_children
    - 按布局排列子元素
  • ui_align_selected
    - 对齐选中的元素
  • ui_distribute_selected
    - 均匀分布选中的元素
  • ui_set_image
    - 设置Image属性(类型/填充/精灵图)
  • ui_add_layout_element
    - 添加/配置LayoutElement
  • ui_add_canvas_group
    - 添加/配置CanvasGroup
  • ui_add_mask
    - 添加Mask或RectMask2D
  • ui_add_outline
    - 添加阴影/描边效果
  • ui_configure_selectable
    - 配置可交互组件的颜色/过渡/导航规则

Single-Object Skills

单对象操作技能

ui_create_canvas

ui_create_canvas

Create a UI Canvas container.
ParameterTypeRequiredDefaultDescription
name
stringNo"Canvas"Canvas name
renderMode
stringNo"ScreenSpaceOverlay"ScreenSpaceOverlay/ScreenSpaceCamera/WorldSpace
创建UI Canvas容器。
参数类型必填默认值描述
name
string"Canvas"Canvas名称
renderMode
string"ScreenSpaceOverlay"渲染模式:ScreenSpaceOverlay/ScreenSpaceCamera/WorldSpace

ui_create_panel

ui_create_panel

Create a Panel container.
ParameterTypeRequiredDefaultDescription
name
stringNo"Panel"Panel name
parent
stringNonullParent Canvas/object
r/g/b/a
floatNo1,1,1,0.5Background color
width/height
floatNo200Size in pixels
创建Panel容器。
参数类型必填默认值描述
name
string"Panel"Panel名称
parent
stringnull父级Canvas/对象
r/g/b/a
float1,1,1,0.5背景颜色
width/height
float200尺寸(单位:像素)

ui_create_button

ui_create_button

Create a Button.
ParameterTypeRequiredDefaultDescription
name
stringNo"Button"Button name
parent
stringNonullParent object
text
stringNo"Button"Button label
width/height
floatNo160/30Size
x/y
floatNo0Position offset
创建Button。
参数类型必填默认值描述
name
string"Button"Button名称
parent
stringnull父级对象
text
string"Button"Button标签文本
width/height
float160/30尺寸
x/y
float0位置偏移

ui_create_text

ui_create_text

Create a Text element.
ParameterTypeRequiredDefaultDescription
name
stringNo"Text"Text name
parent
stringNonullParent object
text
stringNo"Text"Content
fontSize
intNo24Font size
r/g/b/a
floatNo0,0,0,1Text color
width/height
floatNo200/50Size
创建Text元素。
参数类型必填默认值描述
name
string"Text"Text元素名称
parent
stringnull父级对象
text
string"Text"文本内容
fontSize
int24字体大小
r/g/b/a
float0,0,0,1文本颜色
width/height
float200/50尺寸

ui_create_image

ui_create_image

Create an Image element.
ParameterTypeRequiredDefaultDescription
name
stringNo"Image"Image name
parent
stringNonullParent object
spritePath
stringNonullSprite asset path
r/g/b/a
floatNo1,1,1,1Tint color
width/height
floatNo100Size
创建Image元素。
参数类型必填默认值描述
name
string"Image"Image元素名称
parent
stringnull父级对象
spritePath
stringnull精灵图资源路径
r/g/b/a
float1,1,1,1着色颜色
width/height
float100尺寸

ui_create_inputfield

ui_create_inputfield

Create an InputField.
ParameterTypeRequiredDefaultDescription
name
stringNo"InputField"Field name
parent
stringNonullParent object
placeholder
stringNo"Enter text..."Placeholder
width/height
floatNo200/30Size
创建InputField。
参数类型必填默认值描述
name
string"InputField"输入框名称
parent
stringnull父级对象
placeholder
string"Enter text..."占位提示文本
width/height
float200/30尺寸

ui_create_slider

ui_create_slider

Create a Slider.
ParameterTypeRequiredDefaultDescription
name
stringNo"Slider"Slider name
parent
stringNonullParent object
minValue
floatNo0Minimum value
maxValue
floatNo1Maximum value
value
floatNo0.5Initial value
width/height
floatNo160/20Size
创建Slider。
参数类型必填默认值描述
name
string"Slider"滑块名称
parent
stringnull父级对象
minValue
float0最小值
maxValue
float1最大值
value
float0.5初始值
width/height
float160/20尺寸

ui_create_toggle

ui_create_toggle

Create a Toggle/Checkbox.
ParameterTypeRequiredDefaultDescription
name
stringNo"Toggle"Toggle name
parent
stringNonullParent object
label
stringNo"Toggle"Label text
isOn
boolNofalseInitial state
创建Toggle/Checkbox。
参数类型必填默认值描述
name
string"Toggle"开关名称
parent
stringnull父级对象
label
string"Toggle"标签文本
isOn
boolfalse初始状态

ui_set_text

ui_set_text

Update text content.
ParameterTypeRequiredDescription
name
stringYesText object name
text
stringYesNew content
更新文本内容。
参数类型必填描述
name
stringText对象名称
text
string新的文本内容

ui_find_all

ui_find_all

Find UI elements in scene.
ParameterTypeRequiredDefaultDescription
uiType
stringNonullFilter: Button/Text/Image/etc.
limit
intNo100Max results
查找场景中的UI元素。
参数类型必填默认值描述
uiType
stringnull过滤条件:Button/Text/Image等
limit
int100最大返回结果数

ui_set_rect

ui_set_rect

Set RectTransform size, position, and padding.
ParameterTypeRequiredDescription
name
stringNo*UI element name
instanceId
intNo*Instance ID
width
,
height
floatNoSize
x
,
y
floatNoPosition
left
,
right
,
top
,
bottom
floatNoPadding offsets
设置RectTransform的尺寸、位置和内边距。
参数类型必填描述
name
string否*UI元素名称
instanceId
int否*实例ID
width
,
height
float尺寸
x
,
y
float位置
left
,
right
,
top
,
bottom
float内边距偏移

ui_set_anchor

ui_set_anchor

Set anchor preset for a UI element.
ParameterTypeRequiredDefaultDescription
name
stringNo*-UI element name
instanceId
intNo*-Instance ID
preset
stringNo"MiddleCenter"Anchor preset
setPivot
boolNotrueAlso set pivot
Presets: TopLeft, TopCenter, TopRight, MiddleLeft, MiddleCenter, MiddleRight, BottomLeft, BottomCenter, BottomRight, StretchHorizontal, StretchVertical, StretchAll
为UI元素设置锚点预设。
参数类型必填默认值描述
name
string否*-UI元素名称
instanceId
int否*-实例ID
preset
string"MiddleCenter"锚点预设
setPivot
booltrue同时设置枢轴点
预设值:TopLeft、TopCenter、TopRight、MiddleLeft、MiddleCenter、MiddleRight、BottomLeft、BottomCenter、BottomRight、StretchHorizontal、StretchVertical、StretchAll

ui_layout_children

ui_layout_children

Arrange child UI elements in a layout.
ParameterTypeRequiredDefaultDescription
name
stringNo*-Parent element name
instanceId
intNo*-Instance ID
layout
stringNo"Vertical"Layout type
spacing
floatNo10Spacing between elements
Layout types: Vertical, Horizontal, Grid
按布局排列子UI元素。
参数类型必填默认值描述
name
string否*-父元素名称
instanceId
int否*-实例ID
layout
string"Vertical"布局类型
spacing
float10元素间距
布局类型:Vertical、Horizontal、Grid

ui_align_selected

ui_align_selected

Align selected UI elements.
ParameterTypeRequiredDefaultDescription
alignment
stringNo"Center"Alignment type
Alignments: Left, Center, Right, Top, Middle, Bottom
对齐选中的UI元素。
参数类型必填默认值描述
alignment
string"Center"对齐类型
对齐选项:Left、Center、Right、Top、Middle、Bottom

ui_distribute_selected

ui_distribute_selected

Distribute selected UI elements evenly.
ParameterTypeRequiredDefaultDescription
direction
stringNo"Horizontal"Distribution direction

均匀分布选中的UI元素。
参数类型必填默认值描述
direction
string"Horizontal"分布方向

Batch Skill

批量操作技能

ui_create_batch

ui_create_batch

Create multiple UI elements in one call.
ParameterTypeRequiredDescription
items
arrayYesArray of UI element configs
Item properties:
type
(required),
name
,
parent
,
text
,
width
,
height
,
x
,
y
,
r
,
g
,
b
,
a
, etc.
Supported types: Button, Text, Image, Panel, Slider, Toggle, InputField, Dropdown, ScrollView, RawImage, Scrollbar
Returns:
{success, totalItems, successCount, failCount, results: [{success, name, type, instanceId}]}
python
unity_skills.call_skill("ui_create_batch", items=[
    {"type": "Button", "name": "StartBtn", "parent": "MenuPanel", "text": "Start", "y": 60},
    {"type": "Button", "name": "OptionsBtn", "parent": "MenuPanel", "text": "Options", "y": 0},
    {"type": "Button", "name": "QuitBtn", "parent": "MenuPanel", "text": "Quit", "y": -60}
])

单次调用创建多个UI元素。
参数类型必填描述
items
arrayUI元素配置数组
元素配置属性
type
(必填)、
name
parent
text
width
height
x
y
r
g
b
a
支持的元素类型:Button、Text、Image、Panel、Slider、Toggle、InputField、Dropdown、ScrollView、RawImage、Scrollbar
返回值
{success, totalItems, successCount, failCount, results: [{success, name, type, instanceId}]}
python
unity_skills.call_skill("ui_create_batch", items=[
    {"type": "Button", "name": "StartBtn", "parent": "MenuPanel", "text": "Start", "y": 60},
    {"type": "Button", "name": "OptionsBtn", "parent": "MenuPanel", "text": "Options", "y": 0},
    {"type": "Button", "name": "QuitBtn", "parent": "MenuPanel", "text": "Quit", "y": -60}
])

Example: Efficient Menu Creation

示例:高效创建菜单

python
import unity_skills
python
import unity_skills

BAD: 5 API calls

不推荐写法:5次API调用

unity_skills.call_skill("ui_create_canvas", name="MainMenu") unity_skills.call_skill("ui_create_panel", name="MenuPanel", parent="MainMenu") unity_skills.call_skill("ui_create_button", name="StartBtn", parent="MenuPanel", text="Start", y=60) unity_skills.call_skill("ui_create_button", name="OptionsBtn", parent="MenuPanel", text="Options", y=0) unity_skills.call_skill("ui_create_button", name="QuitBtn", parent="MenuPanel", text="Quit", y=-60)
unity_skills.call_skill("ui_create_canvas", name="MainMenu") unity_skills.call_skill("ui_create_panel", name="MenuPanel", parent="MainMenu") unity_skills.call_skill("ui_create_button", name="StartBtn", parent="MenuPanel", text="Start", y=60) unity_skills.call_skill("ui_create_button", name="OptionsBtn", parent="MenuPanel", text="Options", y=0) unity_skills.call_skill("ui_create_button", name="QuitBtn", parent="MenuPanel", text="Quit", y=-60)

GOOD: 2 API calls

推荐写法:2次API调用

unity_skills.call_skill("ui_create_canvas", name="MainMenu") unity_skills.call_skill("ui_create_batch", items=[ {"type": "Panel", "name": "MenuPanel", "parent": "MainMenu", "width": 300, "height": 200}, {"type": "Button", "name": "StartBtn", "parent": "MenuPanel", "text": "Start", "y": 60}, {"type": "Button", "name": "OptionsBtn", "parent": "MenuPanel", "text": "Options", "y": 0}, {"type": "Button", "name": "QuitBtn", "parent": "MenuPanel", "text": "Quit", "y": -60} ])
undefined
unity_skills.call_skill("ui_create_canvas", name="MainMenu") unity_skills.call_skill("ui_create_batch", items=[ {"type": "Panel", "name": "MenuPanel", "parent": "MainMenu", "width": 300, "height": 200}, {"type": "Button", "name": "StartBtn", "parent": "MenuPanel", "text": "Start", "y": 60}, {"type": "Button", "name": "OptionsBtn", "parent": "MenuPanel", "text": "Options", "y": 0}, {"type": "Button", "name": "QuitBtn", "parent": "MenuPanel", "text": "Quit", "y": -60} ])
undefined

TextMeshPro Support

TextMeshPro 支持

UI Skills auto-detect TextMeshPro:
  • With TMP: Uses
    TextMeshProUGUI
  • Without TMP: Falls back to legacy
    UnityEngine.UI.Text
Response includes
usingTMP
field to indicate which was used.
UI技能会自动检测TextMeshPro:
  • 已安装TMP:使用
    TextMeshProUGUI
    组件
  • 未安装TMP:降级使用旧版
    UnityEngine.UI.Text
    组件
返回结果包含
usingTMP
字段,标识实际使用的文本组件类型。

New Element Creation Skills

新增元素创建技能

ui_create_dropdown

ui_create_dropdown

Create a Dropdown with options list and full Template/ScrollRect hierarchy.
ParameterTypeRequiredDefaultDescription
name
stringNo"Dropdown"Dropdown name
parent
stringNonullParent object
options
stringNo"Option A,Option B,Option C"Comma-separated options
width/height
floatNo160/30Size
创建带有选项列表和完整Template/ScrollRect层级的Dropdown。
参数类型必填默认值描述
name
string"Dropdown"下拉框名称
parent
stringnull父级对象
options
string"Option A,Option B,Option C"逗号分隔的选项列表
width/height
float160/30尺寸

ui_create_scrollview

ui_create_scrollview

Create a ScrollRect with Viewport, RectMask2D, and Content.
ParameterTypeRequiredDefaultDescription
name
stringNo"ScrollView"Name
parent
stringNonullParent object
width/height
floatNo300/200Size
horizontal
boolNofalseEnable horizontal scroll
vertical
boolNotrueEnable vertical scroll
movementType
stringNo"Elastic"Unrestricted/Elastic/Clamped
创建包含Viewport、RectMask2D和Content的ScrollRect。
参数类型必填默认值描述
name
string"ScrollView"滚动视图名称
parent
stringnull父级对象
width/height
float300/200尺寸
horizontal
boolfalse启用横向滚动
vertical
booltrue启用纵向滚动
movementType
string"Elastic"滚动类型:Unrestricted/Elastic/Clamped

ui_create_rawimage

ui_create_rawimage

Create a RawImage element (for Texture2D/RenderTexture).
ParameterTypeRequiredDefaultDescription
name
stringNo"RawImage"Name
parent
stringNonullParent object
texturePath
stringNonullTexture asset path
width/height
floatNo100Size
创建RawImage元素(用于展示Texture2D/RenderTexture)。
参数类型必填默认值描述
name
string"RawImage"RawImage元素名称
parent
stringnull父级对象
texturePath
stringnull纹理资源路径
width/height
float100尺寸

ui_create_scrollbar

ui_create_scrollbar

Create a standalone Scrollbar.
ParameterTypeRequiredDefaultDescription
name
stringNo"Scrollbar"Name
parent
stringNonullParent object
direction
stringNo"BottomToTop"LeftToRight/RightToLeft/BottomToTop/TopToBottom
value
floatNo0Initial value (0-1)
size
floatNo0.2Handle size (0-1)
numberOfSteps
intNo00=continuous, >0=discrete
创建独立的Scrollbar。
参数类型必填默认值描述
name
string"Scrollbar"滚动条名称
parent
stringnull父级对象
direction
string"BottomToTop"滚动方向:LeftToRight/RightToLeft/BottomToTop/TopToBottom
value
float0初始值(0-1)
size
float0.2滑块尺寸(0-1)
numberOfSteps
int00=连续滚动,大于0为离散滚动

Property Configuration Skills

属性配置技能

ui_set_image

ui_set_image

Set Image advanced properties (type, fill, sprite).
ParameterTypeRequiredDefaultDescription
name
stringNo*Image element name
instanceId
intNo*0Instance ID
type
stringNoSimple/Sliced/Tiled/Filled
fillMethod
stringNoRadial360/Radial180/Radial90/Horizontal/Vertical
fillAmount
floatNo0-1 fill amount
fillClockwise
boolNoFill direction
preserveAspect
boolNoPreserve aspect ratio
spritePath
stringNoSprite asset path
设置Image的高级属性(类型、填充、精灵图)。
参数类型必填默认值描述
name
string否*Image元素名称
instanceId
int否*0实例ID
type
string图片类型:Simple/Sliced/Tiled/Filled
fillMethod
string填充方式:Radial360/Radial180/Radial90/Horizontal/Vertical
fillAmount
float填充比例(0-1)
fillClockwise
bool填充方向是否为顺时针
preserveAspect
bool是否保持宽高比
spritePath
string精灵图资源路径

ui_add_layout_element

ui_add_layout_element

Add/configure LayoutElement constraints.
ParameterTypeRequiredDefaultDescription
name
stringNo*Element name
minWidth/minHeight
floatNoMinimum size
preferredWidth/preferredHeight
floatNoPreferred size
flexibleWidth/flexibleHeight
floatNoFlexible size (0=fixed, >0=grow)
ignoreLayout
boolNoIgnore layout group
layoutPriority
intNoPriority (higher overrides)
添加/配置LayoutElement约束。
参数类型必填默认值描述
name
string否*元素名称
minWidth/minHeight
float最小尺寸
preferredWidth/preferredHeight
float首选尺寸
flexibleWidth/flexibleHeight
float弹性尺寸(0=固定尺寸,大于0可拉伸)
ignoreLayout
bool是否忽略布局组规则
layoutPriority
int布局优先级(值越高优先级越高)

ui_add_canvas_group

ui_add_canvas_group

Add/configure CanvasGroup (alpha, interactable, blocksRaycasts).
ParameterTypeRequiredDefaultDescription
name
stringNo*Element name
alpha
floatNoGroup alpha (0-1)
interactable
boolNoChildren interactable
blocksRaycasts
boolNoBlocks raycasts
ignoreParentGroups
boolNoIgnore parent groups
添加/配置CanvasGroup(透明度、可交互性、射线检测阻挡)。
参数类型必填默认值描述
name
string否*元素名称
alpha
float组透明度(0-1)
interactable
bool子元素是否可交互
blocksRaycasts
bool是否阻挡射线检测
ignoreParentGroups
bool是否忽略父级CanvasGroup规则

ui_add_mask

ui_add_mask

Add Mask (stencil-based) or RectMask2D (rect clipping).
ParameterTypeRequiredDefaultDescription
name
stringNo*Element name
maskType
stringNo"RectMask2D"Mask/RectMask2D
showMaskGraphic
boolNotrueShow mask graphic (Mask only)
添加Mask(基于模板缓冲)或RectMask2D(矩形裁剪)。
参数类型必填默认值描述
name
string否*元素名称
maskType
string"RectMask2D"遮罩类型:Mask/RectMask2D
showMaskGraphic
booltrue是否显示遮罩图形(仅Mask类型生效)

ui_add_outline

ui_add_outline

Add Shadow or Outline visual effect.
ParameterTypeRequiredDefaultDescription
name
stringNo*Element name
effectType
stringNo"Outline"Shadow/Outline
r/g/b/a
floatNo0,0,0,0.5Effect color
distanceX/distanceY
floatNo1/-1Effect offset
useGraphicAlpha
boolNotrueUse graphic alpha
添加阴影或描边视觉效果。
参数类型必填默认值描述
name
string否*元素名称
effectType
string"Outline"效果类型:Shadow/Outline
r/g/b/a
float0,0,0,0.5效果颜色
distanceX/distanceY
float1/-1效果偏移
useGraphicAlpha
booltrue是否使用图形透明度

ui_configure_selectable

ui_configure_selectable

Configure Selectable properties on Button/Toggle/Slider/etc.
ParameterTypeRequiredDefaultDescription
name
stringNo*Element name
transition
stringNoNone/ColorTint/SpriteSwap/Animation
interactable
boolNoInteractable state
navigationMode
stringNoNone/Horizontal/Vertical/Automatic/Explicit
normalR/G/B
floatNoNormal state color
highlightedR/G/B
floatNoHighlighted state color
pressedR/G/B
floatNoPressed state color
disabledR/G/B
floatNoDisabled state color
colorMultiplier
floatNoColor multiplier (1-5)
fadeDuration
floatNoFade duration
配置Button/Toggle/Slider等可交互组件的属性。
参数类型必填默认值描述
name
string否*元素名称
transition
string过渡效果:None/ColorTint/SpriteSwap/Animation
interactable
bool可交互状态
navigationMode
string导航模式:None/Horizontal/Vertical/Automatic/Explicit
normalR/G/B
float正常状态颜色
highlightedR/G/B
float高亮状态颜色
pressedR/G/B
float按下状态颜色
disabledR/G/B
float禁用状态颜色
colorMultiplier
float颜色倍数(1-5)
fadeDuration
float淡入淡出时长

Best Practices

最佳实践

  1. Always create Canvas first
  2. Use Panels to organize related elements
  3. Use meaningful names for scripting access
  4. Set parent for proper hierarchy
  5. WorldSpace canvas for 3D UI (health bars, etc.)
  1. 始终优先创建Canvas
  2. 使用Panel组织相关联的元素
  3. 使用有意义的命名方便后续脚本访问
  4. 设置父元素以维护正确的层级关系
  5. 3D UI(如血条等)使用WorldSpace渲染模式的Canvas