You've already forked UnrealEngineUWP
mirror of
https://github.com/izzy2lost/UnrealEngineUWP.git
synced 2026-03-26 18:15:20 -07:00
SlateInGameUI: Removed Overview header Updated Engine version Expanded on a few acronyms upon their first uses Changes capitalization on a few terms. SlateProjectSetup: Removed Overview header Updated Engine version Changed font settings for multiple terms/lines Changed some grammar/wording to meet our instructional guide's standards [CL 2615366 by Matthew Clark in Main branch]
105 lines
4.0 KiB
Plaintext
105 lines
4.0 KiB
Plaintext
Availability:Public
|
|
Title: Using Slate In-Game
|
|
Crumbs:%ROOT%, Programming, Programming/Slate
|
|
Description:Using Slate UI widgets for in-game user interfaces.
|
|
Version:4.9
|
|
|
|
|
|
Slate widgets can be used in-game to create Heads Up Displays (HUDs) or other User Interface (UI) elements, such
|
|
as menus. You will generally create one or more container widgets, each of which may be composed of several
|
|
other types of widgets that are responsible for a particular aspect of the UI.
|
|
|
|
<!-- HUD widget w/ menu overlay -->
|
|
|
|
For example, you may have a single overall widget for your game's HUD, as well as widgets for the main menu,
|
|
options menu, pause menu, scoreboard, etc. Each of these widgets would then probably be made up of a combination of
|
|
other custom widgets, labels, text boxes, images, and other types of elements.
|
|
|
|
<!-- container widget breakdown -->
|
|
|
|
Each of these container widgets can then be added or removed depending on the circumstances in the game:
|
|
|
|
* When the game starts, a main menu widget would be added.
|
|
* When they choose one of the options in the menu - to start the game perhaps - the main menu widget would then be removed.
|
|
* If the player pauses the game at any point, the pause menu widget would be added.
|
|
* When the game is resumed, the pause menu widget is removed.
|
|
* When the HUD for the player is initialized, the HUD widget would be added.
|
|
|
|
|
|
## Project Setup
|
|
|
|
[INCLUDE:Programming/Slate/Setup#main(offset:1)]
|
|
|
|
## Displaying Widgets
|
|
|
|
In order to display a Slate widget in your game, it must be added to the game's viewport. Overlaid widgets
|
|
are ordered according to the Z-order specified when adding them, with larger Z-order values appearing on top
|
|
of smaller Z-orders.
|
|
|
|
### Accessing the Game Viewport
|
|
|
|
The game's viewport is an instance of the `GameViewportClient` class. A reference to the current game viewport
|
|
is accessible via the `GameViewport` member of UEngine, which can be accessed using the `GEngine` global pointer
|
|
to the current UEngine instance for the game.
|
|
|
|
For example:
|
|
|
|
GEngine->GameViewport
|
|
|
|
[REGION:note]
|
|
`GEngine` and `GameViewport` can be `NULL` so you should always check their values before trying to access them
|
|
or any of their members.
|
|
[/REGION]
|
|
|
|
### Adding the Widget to the Viewport
|
|
|
|
Slate widgets are added to the viewport by passing a reference - a `TSharedref<SWidget>` to be exact - to the widget
|
|
to `GameViewportClient::AddViewportWidgetContent()`. This function takes in both a widget and a Z-order, which
|
|
determines the sorting order for the new widget as mentioned previously. The Z-order is optional, however, and defaults
|
|
to a value of `0`.
|
|
|
|
The reference to the widget you want to add to the viewport can be stored as a member of some class, such as your
|
|
HUD, or it can be created and passed in at the time of calling the function.
|
|
|
|
Passing a reference to a widget stored in a member variable (as a `TSharedPtr`):
|
|
|
|
GEngine->GameViewport->AddViewportWidgetContent(
|
|
SNew(MyWidgetPtr.ToSharedRef())
|
|
);
|
|
|
|
Creating the widget using `SNew()` when passing it to `GameViewportClient::AddViewportWidgetContent()`:
|
|
|
|
GEngine->GameViewport->AddViewportWidgetContent(
|
|
SNew(SWeakWidget)
|
|
.PossiblyNullContent(MyWidgetClass)
|
|
);
|
|
|
|
Or using `SAssignNew()` to create it, assign it to a `TSharedPtr` member, and pass it in:
|
|
|
|
GEngine->GameViewport->AddViewportWidgetContent(
|
|
SAssingNew(MyWidgetPtr, SWeakWidget)
|
|
.PossiblyNullContent(MyWidgetClass)
|
|
);
|
|
|
|
### Removing Widgets from the Viewport
|
|
|
|
Slate widgets can be removed from the viewport individually by passing a reference to a previously added widget
|
|
to `GameViewportClient::RemoveViewportWidgetContent()`.
|
|
|
|
For example:
|
|
|
|
GEngine->GameViewport->RemoveViewportWidgetContent(
|
|
SNew(MyWidgetPtr.ToSharedRef())
|
|
);
|
|
|
|
In addition, all widgets can be removed from the viewport at once by calling `GameViewportClient::RemoveAllViewportWidgets()`
|
|
|
|
For example:
|
|
|
|
GEngine->GameViewport->RemoveAllViewportWidgets();
|
|
|
|
[REGION:note]
|
|
`GEngine` and `GameViewport` can be `NULL` so you should always check their values before trying to access them
|
|
or any of their members.
|
|
[/REGION]
|