文本和字符
可参考的官方例子:
char_input_events
.
如果你想在 Bevy 应用程序中实现文本输入,请使用本节介绍的内容(而不是键盘输入)。用这种方式,文本和字符的输入都能按照用户对他们的操作系统所期望的那样正常工作,包括 Unicode 支持。
Bevy 将为来自操作系统的每个 Unicode 码产生一个 ReceivedCharacter
事件。
这个例子展示了如何让用户将文本输入到一个字符串中(这里存储为一个本地资源):
/// prints every char coming in; press enter to echo the full string
fn text_input(
mut char_evr: EventReader<ReceivedCharacter>,
keys: Res<Input<KeyCode>>,
mut string: Local<String>,
) {
for ev in char_evr.iter() {
println!("Got char: '{}'", ev.char);
string.push(ev.char);
}
if keys.just_pressed(KeyCode::Return) {
println!("Text input: {}", *string);
string.clear();
}
}