Você pode deixar os widgets interativos da seguinte forma:
Adicionando o argumento
player_ui_slot{InputMode := ui_input_mode.All }ao adicionar um widget usando a funçãoAddWidget().Vinculando um manipulador de eventos ao evento do widget.
No momento, você pode deixar os seguintes widgets interativos:
As seções a seguir são exemplos de como deixar esses widgets interativos.
Interações com botões
Siga estas etapas para criar uma IU com um botão e adicionar um comportamento que exiba o texto do botão selecionado na tela quando o jogador interagir com o botão:
Abra o arquivo Verse que criou em Removendo um widget.
Crie uma função chamada
CreateMyUI()onde você criará a interface de usuário.CreateMyUI() : canvas = MyUIButton : button_loud = button_loud{DefaultText := TextForMyUI} MyInteractableButtons : canvas = canvas: Slots := array: canvas_slot: Anchors := anchors{Minimum := vector2{X := 0.25, Y := 0.5}, Maximum := vector2{X := 0.5, Y := 0.5}} Offsets := margin{Top := 0.0, Left := 0.0, Right := 0.0, Bottom := 0.0} Alignment := vector2{X := 0.5, Y := 0.5} SizeToContent := true Widget := MyUIButton return MyInteractableButtonsAltere o tipo de
MaybeMyUIPerPlayerpara[player]?canvas:var MaybeMyUIPerPlayer : [player]?canvas = map{}Altere a atribuição de
NewUIpara chamar sua nova funçãoCreateMyUI().Verse# UI are added per Player. HandleButtonInteraction(Agent : agent) : void = if (InPlayer := player[Agent], PlayerUI := GetPlayerUI[InPlayer]): if (MyUI := MaybeMyUIPerPlayer[InPlayer]?): PlayerUI.RemoveWidget(MyUI) if (set MaybeMyUIPerPlayer[InPlayer] = false) {} else: NewUI := CreateMyUI() PlayerUI.AddWidget(NewUI) if (set MaybeMyUIPerPlayer[InPlayer] = option{NewUI}) {}Adicione o argumento
player_ui_slot{InputMode := ui_input_mode.All}ao adicionar um novo widget, para que o jogador possa usar o cursor no canvas quando ele for criado.Verse# UI are added per Player. HandleButtonInteraction(Agent : agent) : void = if (InPlayer := player[Agent], PlayerUI := GetPlayerUI[InPlayer]): if (MyUI := MaybeMyUIPerPlayer[InPlayer]?): PlayerUI.RemoveWidget(MyUI) if (set MaybeMyUIPerPlayer[InPlayer] = false) {} else: NewUI := CreateMyUI() PlayerUI.AddWidget(NewUI, player_ui_slot{InputMode := ui_input_mode.All}) if (set MaybeMyUIPerPlayer[InPlayer] = option{NewUI}) {}Todos os botões da interface do usuário têm um evento
OnClickao qual você pode inscrever. Espera-se que o manipulador de eventos tenha um tipo de retornovoide um parâmetro com o tipowidget_message.CreateMyUI() : canvas = MyUIButton : button_loud = button_loud{DefaultText := TextForMyUI} MyUIButton.OnClick().Subscribe(HandleSelectedUIButton) MyInteractableButtons : canvas = canvas: Slots := array: canvas_slot: Anchors := anchors{Minimum := vector2{X := 0.25, Y := 0.5}, Maximum := vector2{X := 0.5, Y := 0.5}} Offsets := margin{Top := 0.0, Left := 0.0, Right := 0.0, Bottom := 0.0} Alignment := vector2{X := 0.5, Y := 0.5} SizeToContent := true Widget := MyUIButton return MyInteractableButtons HandleSelectedUIButton(Message : widget_message) : void = # Defina o que acontece quando o jogador pressiona o botãoEm
HandleSelectedUIButton, exiba o texto do botão na tela e remova o widget da tela. Umawidget_messagesabe qual jogador e elemento da interface de usuário a selecionou. Assim, você pode usar essa informação para obter o texto do botão e encontrar o widget que é exibido para o jogador para removê-lo da tela.VerseHandleSelectedUIButton(Message : widget_message) : void = if (PlayerUI := GetPlayerUI[Message.Player], MyUI := MaybeMyUIPerPlayer[Message.Player]?, SelectedButton := text_button_base[Message.Source]): Print("Player selected {SelectedButton.GetText()}") PlayerUI.RemoveWidget(MyUI) if (set MaybeMyUIPerPlayer[Message.Player] = false) {}Veja a seguir o código completo deste exemplo.
Verseusing { /Fortnite.com/Devices } using { /Verse.org/Simulation } using { /UnrealEngine.com/Temporary/UI } using { /Fortnite.com/UI } using { /UnrealEngine.com/Temporary/SpatialMath } hello_world_device := class(creative_device): # Set the Button device in the Editor to reference the device in the level @editable
Interações com a barra
Siga estas etapas para criar uma IU com um controle deslizante e adicionar um comportamento que gera o valor definido na tela quando o jogador interagir com o controle deslizante:
Abra o arquivo Verse que criou em Interações com botões.
Atualize a função chamada
CreateMyUI()com a barra.CreateMyUI() : canvas = MyUIButton : button_loud = button_loud{DefaultText := TextForMyUI} MyUIButton.OnClick().Subscribe(HandleSelectedUIButton) MyUISlider : slider_regular = slider_regular: DefaultValue := 5.0 DefaultMinValue := 0.0 DefaultMaxValue := 10.0 DefaultStepSize := 0.5 MyInteractableWidgets : canvas = canvas: Slots := array: canvas_slot: Anchors := anchors{Minimum := vector2{X := 0.25, Y := 0.5}, Maximum := vector2{X := 0.5, Y := 0.5}} Offsets := margin{Top := 0.0, Left := 0.0, Right := 0.0, Bottom := 0.0} Alignment := vector2{X := 0.5, Y := 0.5} SizeToContent := true Widget := MyUISlider canvas_slot: Anchors := anchors{Minimum := vector2{X := 0.25, Y := 0.6}, Maximum := vector2{X := 0.5, Y := 0.6}} Offsets := margin{Top := 0.0, Left := 0.0, Right := 0.0, Bottom := 0.0} Alignment := vector2{X := 0.5, Y := 0.5} SizeToContent := true Widget := MyUIButton return MyInteractableWidgetsTodas as barras da interface de usuário têm um evento
OnValueChanged()listenablepara o qual você pode se inscrever. Espera-se que o manipulador de eventos tenha um tipo de retornovoide um parâmetro com o tipowidget_message.CreateMyUI() : canvas = MyUIButton : button_loud = button_loud{DefaultText := TextForMyUI} MyUIButton.OnClick().Subscribe(HandleSelectedUIButton) MyUISlider : slider_regular = slider_regular: DefaultValue := 5.0 DefaultMinValue := 0.0 DefaultMaxValue := 10.0 DefaultStepSize := 0.5 MyUISlider.OnValueChanged().Subscribe(HandleValueChangedUISlider) MyInteractableWidgets : canvas = canvas: Slots := array: canvas_slot: Anchors := anchors{Minimum := vector2{X := 0.25, Y := 0.5}, Maximum := vector2{X := 0.5, Y := 0.5}} Offsets := margin{Top := 0.0, Left := 0.0, Right := 0.0, Bottom := 0.0} Alignment := vector2{X := 0.5, Y := 0.5} SizeToContent := true Widget := MyUISlider canvas_slot: Anchors := anchors{Minimum := vector2{X := 0.25, Y := 0.6}, Maximum := vector2{X := 0.5, Y := 0.6}} Offsets := margin{Top := 0.0, Left := 0.0, Right := 0.0, Bottom := 0.0} Alignment := vector2{X := 0.5, Y := 0.5} SizeToContent := true Widget := MyUIButton return MyInteractableWidgets HandleValueChangedUISlider(Message : widget_message) : void = # Definir o que acontece quando o jogador alterar o valor do controle deslizanteEm
HandleValueChangedUISlider, gere o valor da barra na tela. Umawidget_messagesabe qual jogador interagiu e qual elemento da interface de usuário selecionou. Portanto, você pode usar essas informações para obter o valor da barra e encontrar o widget exibido para o jogador.VerseHandleValueChangedUISlider(Message : widget_message) : void = if: PlayerUI := GetPlayerUI[Message.Player] MyUI := MaybeMyUIPerPlayer[Message.Player]? ChangedSlider := slider_regular[Message.Source] then: Print("Player changed slider value to {ChangedSlider.GetValue()}")Veja a seguir o código completo deste exemplo.
Verseusing { /Fortnite.com/Devices } using { /Verse.org/Simulation } using { /UnrealEngine.com/Temporary/UI } using { /Fortnite.com/UI } using { /UnrealEngine.com/Temporary/SpatialMath } interactable_slider := class(creative_device): # Set the Button device in the Editor to reference the device in the level @editable