Skip to content

Latest commit

 

History

History
107 lines (54 loc) · 4.15 KB

File metadata and controls

107 lines (54 loc) · 4.15 KB

DOTS and UI Toolkit Integration Sample

This sample demonstrates the use of UI Toolkit within an Entities-based project. In the game, the player controls a wizard who collects ingredients for his magical soup.

To understand this sample, you'll need some basic prior knowledge of both UI Toolkit and Entities:

The code of the project resides under the Assets/Scripts directory, which has three subdirectories: Gameplay, ScriptableObjects, and UI. The entity components are mainly defined in the Gameplay/Components subdirectory.

For a walkthrough of the project, see this video (18 minutes).

Gameplay code

Gameplay/GameManagerSystem.cs

A system that controls the macro-level game state (which is expressed as a GameState enum stored in the GameData singleton component).

Gameplay/GameInput.cs

A static class that contains InputAction objects, which are initialized by the GameManagerSystem. Used by the systems to read input.

Gameplay/PlayerMovementSystem.cs

A system that moves the player character. Because the character is a dynamic rigidbody, its movement is controlled by setting its PhysicsVelocity component.

Gameplay/CameraFollowSystem.cs

A system that makes the camera follow the player.

Gameplay/QuestSystem.cs

A system that handles the quest interactions: picking up the ingredients, updating the HUD display of collected ingredients, and turning in the quest at the cauldron.

Gameplay/EnergyBallSystem.cs

A system that controls the energy ball pickups. When the player walks near an energy ball, it gravitates towards the player and orbits around them.

ScriptableObjects/CollectablesData.cs

The set of ingredient names and their associated sprites for display in the inventory.

ScriptableObjects/DialogueData.cs

The dialogue strings displayed at the start and end of the game.

ScriptableObjects/QuestData.cs

The counts of ingredients to collect that are required to complete the quest.

UI code

UI/UIScreen.cs

A base class for the other screen classes. ("Screen" here means really a UI element. We call them "screens" to avoid confusion with the element types provided by UI Toolkit itself.)

UIScreen inherits from ScriptableObject so that its instances can be stored in a UnityObjectRef.

Each UIScreen has an EntityCommandBuffer so that it can record 'event' entities to be processed in the next frame. For example, when the user clicks the close button on the inventory screen, the click handler uses the EntityCommandBuffer to create an event entity representing the click action.

Gameplay/EventSystem.cs

A system which every frame will:

  1. Destroy the event entities of the prior frame.
  2. Playback the EntityCommandBuffer of the screens.
  3. Create a new EntityCommandBuffer for the screens.

Gameplay/UISystem.cs

A system which handles the event entities generated by the UI screens. For example, UISystem will query for events that signal a screen's close button was clicked, and if such an entity exists, UISystem will close the screen.

UI/DialogueScreen.cs

A wrapper for the dialogue UI element that appears at the start and end of the game.

UI/HelpScreen.cs

A wrapper for the help UI element (the help screen that appears when you click the question mark button).

UI/HintScreen.cs

A wrapper for the hint UI element (the text that appears next to an ingredient when it's near the player).

UI/HUDScreen.cs

A wrapper for the HUD UI element (the bottom toolbar of buttons for opening the help screen and inventory screen).

UI/InventoryScreen.cs

A wrapper for the pop-up inventory UI element.

UI/InventorySlot.cs

A wrapper for the UI elements representing a slot of the inventory.

UI/QuestScreen.cs

A wrapper for the quest UI element (the quest status info in the top-right).

UI/SplashScreen.cs

A wrapper for the splash UI element (the "press any key" text at the very start of the game).