获取组件中文名称
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-07-10 13:54:01 |
👁️ 阅读: 1
在996引擎中获取组件的中文名称,可以通过以下几种方式:
```lua
-- 1. 获取文本组件的字符串内容(包含中文)
local textWidget = GUI:Text_Create(parent, "Text_myName", 100, 200, 16, "#ffffff", "这是中文名称")
local chineseText = GUI:Text_getString(textWidget)
SL:Print("文本组件中文内容:", chineseText)
-- 2. 获取按钮的标题文本(中文)
local button = GUI:Button_Create(parent, "btn_myButton", 150, 250, "res/public/1900000612.png")
GUI:Button_setTitleText(button, "中文按钮")
local buttonText = GUI:Button_getTitleText(button)
SL:Print("按钮中文标题:", buttonText)
-- 3. 通过父组件查找子组件的文本内容
local childText = GUI:getChildByName(parent, "Text_childName")
if childText and not GUI:Win_IsNull(childText) then
local content = GUI:Text_getString(childText)
SL:Print("查找到的子组件中文:", content)
end
-- 4. 从服务端获取中文变量值(如果需要)
local serverValue = SL:GetMetaValue("SERVER_VALUE", "变量名")
if serverValue then
SL:Print("服务端返回的中文值:", serverValue)
end
```
**关键函数说明:**
- `GUI:Text_getString(widget)` - 获取文本组件的字符串内容
- `GUI:Button_getTitleText(widget)` - 获取按钮的标题文本
- `GUI:getChildByName(parent, name)` - 通过名称查找子组件
- `GUI:Win_IsNull(widget)` - 判断组件是否有效
**注意事项:**
1. 中文文本直接使用双引号包含即可,如`"中文名称"`
2. 获取前建议先判断组件是否有效,避免空指针错误
3. 如果组件是通过动态创建的,确保在获取前已经创建完成
4. 服务端的中文变量可以通过`SL:GetMetaValue("SERVER_VALUE", param)`获取
需要获取特定类型组件的中文名称,可以告诉我具体的组件类型!
← 返回问答库