Responsive Game UI
Code and workflow to make a responsive game UI in UI Builder

What you'll develop on this page

Game UI overlay responding to click events
We will make a Game UI that will respond to click events as well as display game and player data and add it to our Project.

What will appear in our GameUI

The game UI overlay we will create in this page
We will make a simple game UI overlay that provides the user the ability to return to NavigationScene as well as see game and player information.
Our GameUI uxml will use TitleScreenUI for its .screen and .header selector stylings. Although this isn't totally necessary because we will actually make a new selector class that modifies both of these selectors (.screen needs to be updated for white text and the header needs to be updated to expand to fit its contents), we decided to still include TitleScreenUI USS in GameUI uxml just to demonstrate how to use 2 USSs UI Builder... It just seems like an entire section in a gitbook dedicated to UI should have at least one (albeit nominal) example of multiple USS sheets, you know?
We will create a new USS called GameUI that will hold GameUI specific stylings.
Our GameUI will have a footer that hugs the bottom of the screen similar to how the header hugs the top of the screen.
The right side of the header will contain game information. Although the game information will be static in this section, we'll make it dynamic in the next Multiplayer section.
The bottom left of the screen will display static instructions on how to navigate the game. If you end up updating the game controls please be sure to update these instructions as well.
The bottom right of the screen will display player-specific information. Although the game information will be static in this section, we'll make it dynamic in the next Multiplayer section..

Creating our GameUI

    Now that we are ready to start switching between scenes let's give our game scene the respect it deserves and change its name from "SampleScene" to "MainScene" πŸ‹οΈβ€β™‚οΈ
    Navigate to Assets/Scenes
      Right-click SampleScene and rename to MainScene
      If you have a SampleSceneSettings file in your Project folder, then right-click on SampleSceneSettings and rename it to MainSceneSettings
      Right-click on SampleScene/ (folder) and rename to MainScene
    Open up our newly respected MainScene Scene (not folder) so that MainScene is in Hierarchy
Updating SampleScene to MainScene
    Right-click in the Hierarchy and create an empty GameObject named "GameUI"
      Now there is a GameUI GameObject in the MainScene
      Add a Event System (UI Toolkit) component
      Add a UI Document component
        Drag "PanelSettings" from Assets/UI onto Panel Settings
    Right-click in the Assets/UI folder and create a new UI Document named "GameUIManager" (Create > UI Toolkit > UI Document)
      Drag GameUIManager uxml onto Source Asset field in the UI Document component in Inspector when the GameUI GameObject is selected in Hierarchy
    Create a new script for our GameUIManager custom Visual Element (cVE), "GameUIManager"
      Right-click in UI > Create > C# Script
    Paste the code snippet below into GameUIManager.cs (cVE):
1
using UnityEngine;
2
using UnityEngine.SceneManagement;
3
using UnityEngine.UIElements;
4
using Unity.Burst;
5
using Unity.Entities;
6
using Unity.Mathematics;
7
using Unity.Networking.Transport;
8
using Unity.NetCode;
9
​
10
public class GameUIManager : VisualElement
11
{
12
VisualElement m_LeaveArea;
13
​
14
public new class UxmlFactory : UxmlFactory<GameUIManager, UxmlTraits> { }
15
​
16
public GameUIManager()
17
{
18
this.RegisterCallback<GeometryChangedEvent>(OnGeometryChange);
19
}
20
​
21
void OnGeometryChange(GeometryChangedEvent evt)
22
{
23
m_LeaveArea = this.Q("quit-game");
24
​
25
m_LeaveArea?.RegisterCallback<ClickEvent>(ev => ClickedButton());
26
}
27
​
28
// Start is called before the first frame update
29
void Start()
30
{
31
32
}
33
​
34
void ClickedButton() {
35
​
36
Debug.Log("Clicked quit game");
37
}
38
}
Copied!
Setting up our GameUI
    Open up UI Builder (Window > UI Toolkit > UI Builder)
    Navigate to the Library section of the left panel, click the Project tab and under UI Documents (UXML) within the Asset/UI folder, hover over the GameUIManager uxml for the open icon on the right to appear, and click the icon to open GameUIManager.uxml in Hierarchy
    In StyleSheets (top left) hit the "+" and
      Add Existing USS "TitleScreenUI"
        Sometimes we found in our testing that searching "TitleScreenUI" in Finder's search bar led to no results. If that happens to you just use the old fashion way of finding TitleScreenUI by opening up the Assets folder, then opening up the UI folder, and then scrolling to find TitleScreenUI.uss
      Create New USS "GameUI" (save it in Assets/UI folder, which you may need to expand Finder in order to do so)
    Find GameUIManager in the Custom Controls (C#) section of Library > Project tab and drag it into Hierarchy
    While #GameUIManager is highlighted in Hierarchy, go to Inspector and type "GameUIManager" into the Name field
    Save the GameUIManager uxml (best to save while GameUIManager.uxml is selected in Hierarchy -- for some reason it's less confusing to UI Builder that way!)
Setting up GameUI uxml in UI Builder
You will notice we are following the same "ScreenManager" pattern we used in NavigationScene.
Although we do not switch between views like we do in TitleScreenManager, we want to set up our game UI so it is easy to extend in the future.
We have already reviewed how to create and style views in the "Styling a View" section of our gitbook, so for the next few files we provide you with the code rather than re-creating the views piece-by-piece (not a lot to gain from more of the same).
    Paste the code snippet below into GameUIManager.uxml:
1
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
2
<Style src="TitleScreenUI.uss" />
3
<Style src="GameUI.uss" />
4
<GameUIManager name="GameUIManager" style="width: 100%; height: 100%;">
5
<ui:VisualElement name="screen" class="screen game-ui-screen">
6
<ui:VisualElement name="header" class="header game-ui-header">
7
<ui:Button text="Quit Game" name="quit-game" class="quit-game-button" />
8
<ui:VisualElement name="top-right-container" class="top-right-container">
9
<ui:Label text="Frank&apos;s Game" name="game-name" class="top-right-values" />
10
<ui:Label text="GAME NAME" name="game-name-label" class="top-right-labels" />
11
<ui:VisualElement name="spacer" class="spacers" />
12
<ui:Label text="127.0.0.1" name="game-ip" class="top-right-values" />
13
<ui:Label text="IP ADDRESS" name="ip-address-label" class="top-right-labels" />
14
<ui:VisualElement name="spacer" class="spacers" />
15
<ui:Label name="highest-score" class="top-right-values" />
16
<ui:Label text="HIGHEST SCORE" name="highest-score-label" class="top-right-labels" />
17
</ui:VisualElement>
18
</ui:VisualElement>
19
<ui:VisualElement name="footer" class="footer">
20
<ui:VisualElement name="bottom-left" class="bottom-left">
21
<ui:Label text="Use &quot;p&quot; key to self-destruct" name="instructions-1" class="instruction-text" />
22
<ui:Label text="Use space bar to fire missles" name="instructions-2" class="instruction-text" />
23
<ui:Label text="Right-click and move mouse to rotate" name="instructions-3" class="instruction-text" />
24
<ui:Label text="Use WASD keys to thrust" name="instructions-4" class="instruction-text" />
25
</ui:VisualElement>
26
<ui:VisualElement name="bottom-right" style="margin-right: 10px;">
27
<ui:Label text="Jonathan" name="player-name" class="top-right-values" />
28
<ui:Label text="PLAYER NAME" name="game-name-label" class="top-right-labels" />
29
<ui:VisualElement name="spacer" class="spacers" />
30
<ui:Label text="0" name="current-score" class="top-right-values" />
31
<ui:Label text="CURRENT SCORE" name="current-score-label" class="top-right-labels" />
32
<ui:VisualElement name="spacer" class="spacers" />
33
<ui:Label text="0" name="high-score" class="top-right-values" />
34
<ui:Label text="HIGH SCORE" name="current-score-label" class="top-right-labels" />
35
</ui:VisualElement>
36
</ui:VisualElement>
37
</ui:VisualElement>
38
</GameUIManager>
39
</ui:UXML>
40
​
Copied!
    Paste the code snippet below into GameUI.uss:
1
.quit-game-button:hover {
2
border-top-left-radius: 9px;
3
border-bottom-left-radius: 9px;
4
border-top-right-radius: 9px;
5
border-bottom-right-radius: 9px;
6
border-left-width: 5px;
7
border-right-width: 5px;
8
border-top-width: 5px;
9
border-bottom-width: 5px;
10
}
11
​
12
.quit-game-button:active {
13
background-color: rgb(255, 255, 255);
14
color: rgb(0, 0, 0);
15
}
16
​
17
.quit-game-button {
18
flex-direction: column-reverse;
19
padding-left: 0;
20
padding-right: 0;
21
padding-top: 0;
22
padding-bottom: 0;
23
margin-left: 10px;
24
margin-right: 0;
25
margin-top: 0;
26
margin-bottom: 0;
27
background-color: rgba(0, 0, 0, 0);
28
border-left-width: 3px;
29
border-right-width: 3px;
30
border-top-width: 3px;
31
border-bottom-width: 3px;
32
border-top-left-radius: 10px;
33
border-bottom-left-radius: 10px;
34
border-top-right-radius: 10px;
35
border-bottom-right-radius: 10px;
36
height: 68px;
37
border-left-color: rgb(255, 255, 255);
38
border-right-color: rgb(255, 255, 255);
39
border-top-color: rgb(255, 255, 255);
40
border-bottom-color: rgb(255, 255, 255);
41
font-size: 24px;
42
color: rgb(255, 255, 255);
43
white-space: normal;
44
max-width: 140px;
45
width: 100%;
46
}
47
​
48
.game-ui-screen {
49
background-color: rgba(0, 0, 0, 0);
50
justify-content: space-between;
51
color: rgb(255, 255, 255);
52
}
53
​
54
.game-ui-header {
55
height: auto;
56
}
57
​
58
.top-right-container {
59
margin-right: 10px;
60
margin-top: 10px;
61
}
62
​
63
.top-right-values {
64
color: rgb(255, 255, 255);
65
-unity-text-align: upper-right;
66
font-size: 18px;
67
}
68
​
69
.top-right-labels {
70
-unity-text-align: upper-right;
71
color: rgb(255, 255, 255);
72
font-size: 10px;
73
}
74
​
75
.spacers {
76
height: 3px;
77
}
78
​
79
.footer {
80
bottom: 0;
81
flex-direction: row;
82
justify-content: space-between;
83
position: absolute;
84
flex-grow: 1;
85
width: 100%;
86
height: auto;
87
padding-bottom: 10px;
88
}
89
​
90
.bottom-left {
91
flex-direction: column-reverse;
92
margin-left: 10px;
93
}
94
​
95
.instruction-text {
96
color: rgb(255, 255, 255);
97
white-space: normal;
98
font-size: 14px;
99
}
100
​
Copied!
    Once you've updated the GameUI uxml and Game UI USS with the code above, return to MainScene
Updating GameUIManager uxml and GameUI USS
    Hit "play" in MainScene and checkout the GameUI in action
      You can click on the "Quit Game" button which will trigger a console log
Out GameUI working
We've now built responsive GameUI
    We renamed our SampleScene to MainScene
    We created our GameUI GameObject
      We added Event System (UI Toolkit)
      We added UI Document
    We created GameUIManager uxml and placed it in UI Document's "Source Asset"
    We dragged our Panel Settings to UI Document's "Panel Settings"
    We created GameUIManager uxml and c VE
    We added TitleScreenUI and new GameUI as USS to GameUIManager
    We updated GameUIManager uxml and GameUI USS
Github branch link:β€Œ
git clone https://github.com/moetsi/Unity-DOTS-Multiplayer-XR-Sample/ git checkout 'Game-UI-Overlay'β€Œ
Last modified 7mo ago