The Generation Scope options control whether the entire selected object is shown in the generated code window or if only the current state is selected.
If Selected Object is selected, then the entire object's generated code is displayed.
The Selected State option is useful if you would like to see only the changes made by the selected state.
This option controls whether generated code is displayed in the Code tab. If checked, generated code is displayed and is updated whenever a change is made in Gum.
Usually this option can remain checked even if you are not using generated code; however, very large components and screens may generate a large amount of code, which can slightly affect Gum's performance when selecting objects or making changes. If you are experiencing slowdowns and you do not need code to be generated, you can uncheck this option.
The Is CodeGen Plugin Enabled checkbox controls whether Gum generates code automatically to the destination location specified by the Code Project Root. Gum automatically generates code if:
The Is CodeGen Plugin Enabled is checked
A Code Project Root is specified
An object has its Generation Behavior set to GenerateAutomaticallyOnPropertyChange
If this option is unchecked, then code will not automatically be saved to disk even if the other options are set.
For more information on automatic code generation, see the section on automatically saving generated code.
The Inheritance Location options allow you to control where inheritance is specified in generated code (custom or generated code).
When an element is selected, its generated code is displayed in the Gum tool. If automatic or manual saving of generated code is enabled, then this generated code is saved to disk at the desired location. When generated code is saved, it is saved with the Runtime.Generated.cs
suffix. An accompanying custom code file is also saved in the same folder. This custom code is generated initially, but after that it is not re-generated by Gum. This pair of files allows Gum to continue to update the .Generated.cs file without overwriting custom code.
The Inheritance Location option controls whether inheritance is added to custom or generated code. In most cases it's desirable to keep inheritance in generated code; however, some environments (such as .NET Maui) may benefit from writing inheritance in custom code.
The Code tab provides generated code for your current Gum objects.
This tab provides the following functionality:
Immediate display of generated code for the selected object
Ability to see generated code for individual instances in an object or for an entire component/screen
Ability to generate code automatically for a variety of target platforms
If you are working with Gum in a C# environment then the Code tab can help you write Gum code.
To enable code generation:
Select a Screen, Component, or instance
Check the Is CodeGen Plugin Enabled checkbox
Check the Show CodeGen Preview checkbox to display the current selection in the preview window
The generation of code may make selection slightly slower, especially when viewing complex screens or components. If you are experiencing performance problems, you may consider unchecking the Show CodeGen Preview checkbox when performing editing.
If you have a single instance selected, the preview window displays the code for creating the instance and assigning its variables. This is especially useful if you are unsure how to reproduce a particular layout in code. For example, the following image shows the generated code for a Text named TextInstance.
The generated code shows all of the assignments necessary to reproduce the current instance's layout. Keep in mind that only explicitly-set variables are displayed. Any default (green background) variables are not assigned in generated code.
If a Screen or Component is selected, then an entire class for the component is displayed in the preview window. This generated code includes:
using
statements
A partial
class with the suggested name. The name appends the word "Runtime" to the Screen or Component name
enum
declaration for all categories
Properties for each category including switch statements assigning all properties for each state
A property for each instance in the Screen or Component
Initialization of all variables including variables on the instances
The Code tab supports the automatic copying of files to disk. By using this feature, C# projects can automatically stay in sync with Gum projects, eliminating the need to write custom Runtime objects.
Projects should be backed up or committed to source control before enabling automatic code generation to make it easy to undo changes.
To set up automatic code generation, first enable the code generation plugin as shown above. The Show CodeGen Preview checkbox does not need to be checked.
Next, modify the values in the Project-Wide Code Generation section and the Element Code Generation section as discussed in the following sections:
Select the desired Output Library, such as MonoGame.
If you are planning on loading the .gumx project, select the FindByName option.
If you would like the entire project generated, select the FullyInCode option. This option enables working in Gum to create layouts which will work fully in code without loading a .gumx file. This is especially improtant if you are working on a platform with limited IO access. Generated code can run faster than loading a .gumx file since it does not require file IO, XML parsing, and reflection.
Add the following using statement at the end of the Project-wide Using Statement box so that references to standard runtime types are found.
If you plan on creating Screens, you should also add using statements for your component runtimes
Enter the location of the folder containing the .csproj file in the Code Project Root text box. If an absolute path is entered, it will be saved to a relative path so that generation works for all users working on a project regardless of where a project is cloned even though it appears absolute in Gum. For example: C:\Users\Owner\Documents\GitHub\Gum\Samples\MonoGameGumCodeGeneration\
Enter the project's Root Namespace, such as MyGame
.
Enter the type that you would like all Screen runtimes to inherit from. If you're not sure what to enter, then use GraphicalUiElement
. If your game uses a custom class that you have written for all Screens, then use the name of that class. You can switch to custom classes later as your project grows.
Select NeverGenerate for components which should not generate to disk
Select GenerateManually if you would like to generate code only when the Generate Code button is clicked.
Select GenerateAutomaticallyOnPropertyChange to generate code whenever a property changed. This option is useful once you are comfortable with code generation. It results in code being generated automatically as you make edits in Gum.