User Interface
Last updated
Last updated
One of the toughest parts of game development is crafting great user interfaces (UIs).
HYTOPIA makes this process remarkably simple. You can build anything from basic menus to highly complex interfaces using standard web tools like HTML, CSS, and JavaScript. If you’d rather work with a framework like React or Svelte, that’s no problem—HYTOPIA supports those too!
One strength that sets HYTOPIA apart is its extremely flexible and unopinionated UI system. If you can create a webpage, you can create the UI for your game.
Your user interfaces are injected into the same page (DOM) as the HYTOPIA game scene while enabling your user interfaces and your game’s server to seamlessly exchange game state, interactions, and data in real time.
The HYTOPIA game scene and your injected content is loaded in a CSP (Content-Security Policy) controlled iframe, isolating game behavior from any meaningful user data.
All aspects of custom UIs are handled automatically and internally via a WebSocket connection, ensuring everything stays fast and responsive.
Any interaction you can implement on a webpage can work in your UI!
HYTOPIA supports 2 distinct types of user interfaces. Here's the difference between the two.
Overlay UI - The Overlay UI is the global user interface that overlays the game scene. This is great for things like menus, skill bars, leaderboards, countdown timers, visual effects, and other UI elements that do not require spatial placement in the game scene.
Scene UIs - Scene UIs exist spatially within the 3D game scene itself. They can be attached to entities to follow them, placed at a fixed position, and more. Health bars, entity status icons, quest symbols, NPC messages, and much more are examples of what you could use Scene UIs for.
Both the Overlay UI and Scene UI elements are defined in a single .html file.
In this example, we'll use simple HTML and Javascript to create a basic box on the screen as part of the Overlay UI. As long as your UI is bundled/rendered into an entry .html file, it can be loaded as a HYTOPIA UI regardless of the framework you use.
Make sure to never include <html>
, <body>
, or <head>
tags in your entry .html file!
In your assets
folder of your project, we'll create a new folder called ui
. Within that, create a file called index.html
. Your folder and file can be named whatever you like, but for this example we'll keep it simple. Your file should exist at assets/ui/index.html
In your index.html
file, let's create a basic box UI - remember, UI's act more or less like a transparently overlayed web page, so we can use HTML, CSS and Javascript for it.
Now, in our server code, specifically in our world.onPlayerJoin
callback, we can load the UI for the player when they join.
Boom! That's all we have to do, our UI is fully loaded.
UI is loaded and controlled on a per-player basis, giving you fine grain controls of what each player sees.
If you call player.ui.load()
again, it will override the previously loaded UI and load a new UI for the player.
Here's what we should see from our example! A red box positioned in the top right of our screen.
If you want to load images in your UI using things like <img /> tag, or load other file types that exist in your assets
folder, you'll need prefix their relative file path with the magic value {{CDN_ASSETS_URL}}
.
For example, let's say in our index.html we have an image we want to load that exists at assets/images/icon.png
. We can correctly load it by setting the src
as follows.
All occurrences of this magic {{CDN_ASSETS_URL}}
value are replaced at runtime with the origin server your assets serve from. In local development this origin is your local game server, in production when deployed to HYTOPIA services this will be an arbitrarily assigned CDN url.
You can use this to get the correct URI of any asset type from your assets
directory. Stylesheets, images, videos, etc.
Nearly every client UI and game server needs to be able to receive or send some amount of data.
Here's how we can do that.
We can send a JSON compatible object of any shape to a player's client as follows.
When sending data, it's sent to a specific player. If you need to send a data update to all players, you can use the PlayerManager to iterate all connected players and send data. In our index.html file.
To receive data on the client, you can use the hytopia
global variable in your index.html that is automatically injected by the client and always available when your UI loads.
It's that simple! You have full control over the way data is sent and received by players, as well as the shape of the data based on the needs of your game.
Similar to how we send data from our server to our client UI, we can send data back from our player's client UI to our server. In our index.html:
We can receive UI data sent from each player's client UI and handle it on the server however we need to with the following:
Scene UIs have their own internally tracked state you can control. Each Scene UIs state is unique to that given instance of the scene UI.
Scene UI state management is similar to the state patterns of React. Each Scene UI instance can have its own internally tracked state defined by you, and updated directly to trigger UI updates of just that scene element without extra logic.
You can learn how to use Scene UI state management in Scene UIs.
If you're building more complex UIs with modals, fade effects, and a lot of managed state, you'll likely want to use some popular framework like React, Svelte, etc.
To use one of these frameworks to build a UI, you'll simply need to make sure the output bundles to a .html file that excludes <html>
, <head>
and <body> tags
. Add that file to your assets
folder, and then load it as ui with player.ui.load()
The entire game scene, including the UI is loaded for players in a content security policy (CSP) controlled iframe. This sandboxing blocks all external network requests and access to sensitive user data. It effectively acts as a maximum set of isolation around the game and its UI relative to the connected player and their HYTOPIA account.
Overlay UI
Learn more about the Overlay UI that overlay the game scene.
Scene UIs
Learn about Scene UIs that exist in 3D space within the game scene.
Scene UI Manager
Learn more about the Scene UI Manager that tracks and allows lookup of Scene UIs in the world.