NButton
NButton 组件是UI系统中的一个交互组件,它通过监听用户的输入(如点击)来触发相应的事件。在引擎中,按钮的外观和行为通过图片和文本组件进行控制,按钮的交互逻辑通过UI事件系统进行管理。
| 接口名称 | 返回值 | 接口含义 |
|---|---|---|
| SetButtonBackgroundStyle | void | 设置按钮背景样式类型 |
| GetButtonBackgroundStyle | ETransitionStyle | 获取按钮背景样式类型 |
| SetNormalTexture | void | 设置正常显示时的纹理 |
| SetHighlightedTexture | void | 设置选中时显示的纹理 |
| SetPressedTexture | void | 设置按下时显示的纹理 |
| SetDisabledTexture | void | 设置不可交互时显示的纹理 |
| SetNormalColor | void | 设置正常显示的颜色 |
| SetHighlightedColor | void | 设置选中时显示的颜色 |
| SetPressedColor | void | 设置按下时显示的颜色 |
| SetDisabledColor | void | 设置不可交互时显示的颜色 |
| SetButtonText | void | 设置按钮显示的文本内容 |
| GetButtonText | std::string | 获取按钮显示的文本内容 |
| SetButtonFont | void | 设置按钮文本使用的字体 |
| SetButtonDisabled | void | 设置按钮是否可交互 |
| GetButtonLabel | NActorPtr | 获取按钮子节点文本控件 |
| SetAlpha | void | 设置按钮显示的透明度 |
| GetAlpha | float | 获取按钮显示的透明度 |
设置和获取背景样式
接口: void SetButtonBackgroundStyle(ETransitionStyle ButtonBkgStyle); ETransitionStyle GetButtonBackgroundStyle();
参数: ButtonBkgStyle:按钮背景显示类型,分为纹理,纯色
描述:设置按钮背景显示效果,可以是纹理,也可以是纯色。
代码示例:
void NButtonTest::Start()
{
auto buttonAct = GetNActor();
auto buttonComp = buttonAct->GetComponent<NButton>();
buttonComp->SetButtonBackgroundStyle(ETransitionStyle::ETS_IMAGE);
}设置不同事件背景显示效果
接口: void SetNormalTexture(const std::string fileName); void SetHighlightedTexture(const std::string fileName); void SetPressedTexture(const std::string fileName); void SetDisabledTexture(const std::string fileName); void SetNormalColor(Color32 color); void SetHighlightedColor(Color32 color); void SetPressedColor(Color32 color); void SetDisabledColor(Color32 color); 参数:
| 参数 | 说明 |
|---|---|
| fileName | 图片资源路径 |
| color | 颜色值 |
描述:通过上述接口可以实现,未选中显示的效果,选中事件背景显示的效果,按下事件背景显示的效果,以及不允许交互背景显示的效果。
代码示例:
void NButtonTest::Start()
{
auto buttonAct = GetNActor();
auto buttonComp = buttonAct->GetComponent<NButton>();
buttonComp->SetButtonBackgroundStyle(ETransitionStyle::ETS_IMAGE);
buttonComp->SetPressedTexture("Assets/Textures/Skybox.png");
}设置和获取按钮显示的文本内容
接口: void SetButtonText(const std::string& text); std::string GetButtonText();
参数: text:设置按钮显示的文本内容
描述: 通过上述接口可以设置按钮文本以及获取按钮显示的文本内容。
代码示例:
void NButtonTest::Start()
{
auto buttonAct = GetNActor();
auto buttonComp = buttonAct->GetComponent<NButton>();
buttonComp->SetButtonText("Test");
auto str = buttonComp->GetButtonText();
}设置文本显示的字体
接口: void SetButtonFont(std::string font, std::string fontPath);
参数:
| 参数 | 说明 |
|---|---|
| font | 字体的名称 |
| fontPath | 字体文件的路径 |
描述:通过上述接口可以动态修改文本字体样式。
代码示例:
void NButtonTest::Start()
{
auto buttonAct = GetNActor();
auto buttonComp = buttonAct->GetComponent<NButton>();
buttonComp->SetButtonFont("test","Assets/Fonts/simhei.ttf");
}设置按钮是否可交互
接口: void SetButtonDisabled(bool isDisabled);
参数: isDisabled:否是可交互
描述: 如果不可以交互,按钮将不再接收事件,同时按钮背景实现为不可交互样式。
代码示例:
void NButtonTest::Start()
{
auto buttonAct = GetNActor();
auto buttonComp = buttonAct->GetComponent<NButton>();
buttonComp->SetButtonDisabled(true);
}
