我在 Godot 4 中使用了
RichTextLabel
,并且我试图在其中插入一个特殊的 unicode 字符(我正在使用 fontawesome)。我知道该字符的 unicode 索引。如何通过该索引将其添加到标签中?
我尝试过:
\uf6e3
\Uf6e3
u+f6e3
U+f6e3
[unicode]f6e3[/unicode]
以上所有内容都只是按原样打印控制序列,它们不打印我试图引用的unicode字符。
唯一可行的方法是将 unicode 字符(作为文本)逐字复制到剪贴板中,然后将其直接粘贴到编辑器中的标签文本中。这可以工作,但不是很有用,因为编辑器本身无法显示该字符。我更愿意在编辑器中看到控制序列,并在游戏中看到实际角色。
不幸的是,Godot 无法直接显示这一点,我已经为您编写了一个小解决方法脚本,可以实时用 unicode 字符替换您的文本。我希望这对你有帮助:)
@tool
extends RichTextLabel
@export_multiline var UnicodeText: String = "":
set(value):
var updated_value = replace_unicode_sequences(value)
self.text = updated_value
notify_property_list_changed()
get:
return text
func replace_unicode_sequences(value: String) -> String:
var result = value
var regex = RegEx.new()
regex.compile("\\\\u([0-9a-fA-F]{4})")
var matches = regex.search_all(value)
for match in matches:
var codepoint = int("0x" + match.get_string(1))
var unicode_char = char(codepoint)
result = result.replace(match.get_string(0), unicode_char)
return result