UI文档
版本:1.18.10.4

索引

返回顶部

脚本系统

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.

引擎绑定

on(EventIdentifier, Callback)

This is used to get events from the UI Engine. These events can be originally sent from client scripts using `send_ui_event`, or created by the game and passed along by the UI Engine. The data sent from scripts to this function must be a string.

参数

名称 类型 描述
Callback JavaScript对象 The callback that will be called when the event happens
EventIdentifier 字符串 Specifies the event that function will react to

返回顶部

如何从一个客户端脚本监听事件的示例:


engine.on("exampleEventIdentifier", function (exampleData) { 
}



trigger(EventIdentifier, Arguments)

This is used to send events to the UI Engine.

参数

名称 类型 描述
Arguments JavaScript对象 The arguments passed to the callback
EventIdentifier 字符串 Specifies the event that function will react to

返回顶部

如何向UI引擎发送一个事件的示例:


engine.trigger("exampleEventIdentifier", eventDataObject);





获取脚本引擎

In order to create a link between the UI Engine and the Script Engine you need to capture the instance of the Script Engine.
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.

Example of how to capture the Script Engine:


let scriptInterface = undefined;
engine.on("facet:updated:scripting", function(interface) {
  scriptInterface = interface;
});
engine.trigger("facet:request", ["scripting"]);


返回顶部

脚本绑定

triggerEvent(Data)

This triggers the minecraft:ui_event on client scripts with the provided data.

参数

名称 类型 描述
Data 字符串 This string will be sent to "minecraft:ui_event" event in client scripts

返回顶部

如何向一个客户端脚本发送事件的示例:


scriptInterface.triggerEvent("SendThisDataToTheScript");







网站作者: destruc7i0n

本网站不隶属于Mojang Studios