UI文档
版本: 1.16.200.2(正式版)
此文档已有已翻译版本
*请前往API 文档>其它文档>H5UI 查看
脚本系统
The custom UI for Minecraft is based on HTML 5.
You can write JavaScript within the HTML file to listen and respond to events from the UI Engine. These events can be triggered by the UI Engine itself or you can trigger them from your scripts.
In order to use custom UI, the resource pack containing the custom UI needs to have the custom UI capabilities enabled. To do this, simply add "experimental_custom_ui" to the capabilities array in the pack's manifest. You can check the Turn-Based RPG demo for an example of how to do this.
获取脚本引擎
The engine.on() function needs to listen for the event "facet:updated:scripting" and you need to store the return value.
You will then need to request the script engine by triggering the "facet:request" event and passing it "scripting" in a vector.
The order of the calls is important. If you trigger the request before you registered the listener you won't be able to capture the callback.
捕获脚本引擎示例:
let scriptInterface = undefined;
engine.on("facet:updated:scripting", function(interface) {
scriptInterface = interface;
});
engine.trigger("facet:request", ["scripting"]);
引擎绑定
on(事件标识符, 回调函数)
参数
Name | Type | Description |
---|---|---|
Callback | JavaScript Object | The callback that will be called when the event happens |
EventIdentifier | String | Specifies the event that function will react to |
客户端脚本监听事件示例
engine.on("exampleEventIdentifier", function (exampleData) {
});
trigger(事件标识符,参数)
参数
Name | Type | Description |
---|---|---|
Arguments | JavaScript Object | The arguments passed to the callback |
EventIdentifier | String | Specifies the event that function will react to |
发送事件至UI引擎示例
engine.trigger("exampleEventIdentifier", eventDataObject);
脚本绑定
Name | Type | Description |
---|---|---|
Data | String | This string will be sent to "minecraft:ui_event" event in client scripts |
事件发送到客户端示例
scriptInterface.triggerEvent("SendThisDataToTheScript");