Skip to content

NButton

NButton 组件是UI系统中的一个交互组件,它通过监听用户的输入(如点击)来触发相应的事件。在引擎中,按钮的外观和行为通过图片和文本组件进行控制,按钮的交互逻辑通过UI事件系统进行管理。

接口名称返回值接口含义
SetButtonBackgroundStylevoid设置按钮背景样式类型
GetButtonBackgroundStyleETransitionStyle获取按钮背景样式类型
SetNormalTexturevoid设置正常显示时的纹理
SetHighlightedTexturevoid设置选中时显示的纹理
SetPressedTexturevoid设置按下时显示的纹理
SetDisabledTexturevoid设置不可交互时显示的纹理
SetNormalColorvoid设置正常显示的颜色
SetHighlightedColorvoid设置选中时显示的颜色
SetPressedColorvoid设置按下时显示的颜色
SetDisabledColorvoid设置不可交互时显示的颜色
SetButtonTextvoid设置按钮显示的文本内容
GetButtonTextstd::string获取按钮显示的文本内容
SetButtonFontvoid设置按钮文本使用的字体
SetButtonDisabledvoid设置按钮是否可交互
GetButtonLabelNActorPtr获取按钮子节点文本控件
SetAlphavoid设置按钮显示的透明度
GetAlphafloat获取按钮显示的透明度

设置和获取背景样式

接口: void SetButtonBackgroundStyle(ETransitionStyle ButtonBkgStyle);    ETransitionStyle GetButtonBackgroundStyle();

参数: ButtonBkgStyle:按钮背景显示类型,分为纹理,纯色

描述:设置按钮背景显示效果,可以是纹理,也可以是纯色。

代码示例:

cpp
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颜色值

描述:通过上述接口可以实现,未选中显示的效果,选中事件背景显示的效果,按下事件背景显示的效果,以及不允许交互背景显示的效果。

代码示例:

cpp
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:设置按钮显示的文本内容

描述: 通过上述接口可以设置按钮文本以及获取按钮显示的文本内容。

代码示例:

cpp
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字体文件的路径

描述:通过上述接口可以动态修改文本字体样式。

代码示例:

cpp
void NButtonTest::Start()
{
    auto buttonAct = GetNActor();
    auto buttonComp = buttonAct->GetComponent<NButton>();
    buttonComp->SetButtonFont("test","Assets/Fonts/simhei.ttf");
}

设置按钮是否可交互

接口: void SetButtonDisabled(bool isDisabled);

参数: isDisabled:否是可交互

描述: 如果不可以交互,按钮将不再接收事件,同时按钮背景实现为不可交互样式。

代码示例:

cpp
void NButtonTest::Start()
{
    auto buttonAct = GetNActor();
    auto buttonComp = buttonAct->GetComponent<NButton>();
    buttonComp->SetButtonDisabled(true);
}