Loading a Gum Project (.gumj/.gumx)
Introduction
Gum projects can be loaded in a game project. Gum projects are made up of multiple files including:
.gumj - the main Gum project
.gusx - Gum screen files
.gucx - Gum component files
.gutx - Gum standard element files
.png - image files
.fnt - font files
Creating a Gum Project
Before creating a Gum project, it is recommended that you already have a functional Game project. Next, create the Gum project in the Gum tool:
Open the Gum tool
Select File > New Project
Enter a Project name, such as
GumProject. Leave Create C# project unchecked, since you already have a game project.Set the Location to the folder that holds your game's content. Gum creates a new folder named after the project inside it. See below for recommended locations:
Set the Location to your game's Content folder. The project is created in Content/GumProject.
Set the Location to your game's Content folder. The project is created in Content/GumProject.
Set the Location to your game's Content folder. The project is created in Content/GumProject.
Set the Location to your game's Content folder. The project is created in Content/GumProject.
Web targets (KNI BlazorGL and other WebAssembly hosts) must serve the Gum project from wwwroot. Set the Location to your web project's wwwroot/Content folder. The project is created in wwwroot/Content/GumProject.
Files placed anywhere else are not published as static web assets and will fail to load at runtime with a 404.
Set the Location to your project's folder. The project is created in GumProject.
It's best to put your Gum project in a folder that is not shared with any other content that it stays organized from the rest of your content files. Remember, Gum creates lots of files.
For every option in the dialog, see Creating a Project. To start without an existing game project, check Create C# project and Gum creates a runnable game with the Gum project inside it.
Adding the Gum Project to your .csproj
To add the Gum files to your csproj:
Open your .csproj in a text editor
Add a line to copy all files in the Gum project folder including the .gumj file itself. For example, your .csproj might look this (see tabs below)
The Microsoft.NET.Sdk.BlazorWebAssembly SDK automatically publishes everything under wwwroot as static web assets, so a Gum project saved into wwwroot/Content/GumProject requires no additional <ItemGroup> entry to ship.
If you keep your Gum project outside wwwroot and want to include it via a wildcard, you can copy the files into wwwroot at build time:
.NET MAUI projects do not currently reference Gum projects so the file does not need to be added ot the game project. Currently .NET MAUI projects must use full code generation to reference a Gum project.
For more information about wildcard support in .csproj files, see this page on how to include wildcards in your .csproj:
Sharing a .gumj File
If your game targets multiple platforms, you may have multiple .csproj files. A single .csproj file can be linked by multiple projects. One way to achieve this is to create a linked wildcard include. For example, Gum files which are relative to a DesktopGL project can be included in an Android project using the following item in a .csproj file:
Loading a Gum Project
To load a Gum Project:
Make sure that your Gum project has at least one Screen
Open your file that has your Gum initialization code, such as Game1.cs or Project.cs
Modify the Initialize method by passing it a Gum project file path
By default the Gum path is relative to your game's Content folder. On KNI BlazorGL (and other WebAssembly hosts) this resolves under wwwroot/Content, so the same "GumProject/GumProject.gumj" value works on web as long as the project lives at wwwroot/Content/GumProject/GumProject.gumj.
If your Gum project is not part of the the folder you can still load it by using the "../" prefix to step out of the Content folder. For example, the following code would load a Gum project located at <exe location>/GumProject/GumProject.gumj:
See the Silk.NET setup page for how to create window.
.NET MAUI projects do not currently support loading .gumj projects.
The code above loads the Gum project using the desired file path, such as "GumProject/GumProject.gumj".
ToGraphicalUiElement
Once a Gum project is loaded, all of its screens and components can be accessed through the ObjectFinder.Self.GumProjectSave property. Any screen or component can be converted to a GraphicalUiElement, which is the visual object that displays in game.
The code in the previous section creates a GraphicalUiElement from the first screen in the project.
Note that calling ToGraphicalUiElement creates a GraphicalUiElement (Gum object) from the first screen. You can access any screen in the the Gum project if your project has multiple Screens.
You can get a reference to elements within the screen by calling GetGraphicalUiElementByName, as shown in the following code:
ToForms
A component that wraps a Forms control, such as a Button, can be created directly as the Forms control with ToForms. ToForms creates the same visual as ToGraphicalUiElement and returns its FrameworkElement, so you do not need to read FormsControlAsObject and cast it yourself.
ToForms returns null for elements that have no Forms control, such as a plain container or a screen. The non-generic ToForms() returns a FrameworkElement?, and ToForms<T>() returns T?, which is also null when the control is a different type. Like ToGraphicalUiElement, ToForms does not add the result to the root, so call AddToRoot when you want it displayed.
Last updated
Was this helpful?

