Appearance
NOTE
这是旧版 UGUI 的 InputField (Legacy),用于输入昵称、账号、密码、聊天内容等字符串。

一、整体结构
c
InputField (Legacy)
├── RectTransform
├── CanvasRenderer
├── Image
├── InputField
└── Text Area
├── Text(Legacy Text)
└── Placeholder(Legacy Text)InputField:负责编辑逻辑。Text:负责显示输入内容。Placeholder:输入框为空时显示提示。Image:负责输入框背景和点击区域。
二、截图中的配置
Rect Transform
Position:X=35.38287,Y=-133.5418,Z=0Width:160Height:30Anchor:中心Pivot:0.5, 0.5Rotation:0Scale:1
它决定输入框的位置和大小。
Image
Source Image:InputFieldBackgroundImage Type:SlicedFill Center:开启Pixels Per Unit Multiplier:1Raycast Target:开启Maskable:开启
Sliced 使用九宫格拉伸,适合输入框背景。Sprite 需要在 Sprite Editor 中设置 Border。
三、InputField 组件
Interactable
当前开启,表示输入框可以获得焦点并输入文字。
关闭后:
- 不能点击。
- 不能输入。
- 使用 Disabled Color。
Transition
当前为 Color Tint,会根据输入框状态改变背景颜色:
Normal Color:正常。Highlighted Color:鼠标悬停。Pressed Color:按下。Selected Color:获得输入焦点。Disabled Color:不可交互。
Target Graphic
当前为:
c
InputField (Legacy) (Image)表示状态颜色作用于背景 Image,不会自动改变文字颜色。
Navigation
当前为 Automatic,控制键盘或手柄在多个 UI 输入框之间切换。
Text Component
当前引用:
c
Text (Legacy) (Text)它是实际显示用户输入内容的旧版 Text。
四、下方未显示的输入设置
Text
输入框的初始文字或当前内容。
Character Limit
最大字符数。
c
0 = 通常表示不限制Content Type
决定允许输入什么内容:
Standard:普通文本。Integer Number:整数。Decimal Number:小数。Alphanumeric:字母和数字。Email Address:邮箱。Password:密码。Pin:数字密码。Custom:自定义规则。
Line Type
Single Line:单行输入。Multi Line Submit:多行输入,提交键提交。Multi Line Newline:多行输入,提交键换行。
Placeholder
输入框为空时显示的提示文字。
例如:
c
请输入昵称
请输入账号
请输入密码光标设置
Caret Blink Rate:光标闪烁频率。Caret Width:光标宽度。Custom Caret Color:自定义光标颜色。Selection Color:选中文字时的背景颜色。
其他设置
Hide Mobile Input:移动端是否隐藏系统原生输入框。Read Only:只读模式,只显示内容,不能编辑。
五、输入事件
On Value Changed (String)
每次文字发生变化时触发。
c
public void OnValueChanged(string value)
{
Debug.Log("当前输入:" + value);
}End Edit (String)
用户提交或输入框失去焦点时触发。
六、代码示例
c
using UnityEngine;
using UnityEngine.UI;
public class LegacyInputDemo : MonoBehaviour
{
[SerializeField] private InputField input;
public void OnValueChanged(string value)
{
Debug.Log(value);
}
public void Submit()
{
Debug.Log(input.text);
}
public void Clear()
{
input.text = "";
}
}读取输入内容时使用:
c
input.text不要只读取子物体 Text,因为密码模式可能显示星号,文字也可能被裁剪。
七、Legacy InputField 与 TMP_InputField
| Legacy InputField | TMP_InputField |
|---|---|
使用 InputField | 使用 TMP_InputField |
| 子物体使用 Legacy Text | 子物体使用 TextMeshProUGUI |
| 功能较基础 | 字体和排版能力更强 |
| 适合旧项目 | 新项目更推荐 |
Unity 官方将 InputField 定义为“让用户编辑 Text 文本的 UI 控件”。InputField 官方文档