LogoLogo
  • Gum Tool
    • Introduction
    • Setup
      • Running from Source
    • Showcase
    • Tutorials and Examples
      • Intro Tutorials
        • 1 - The Basics
        • 2 - Variables Tab
        • 3 - Files
        • 4 - Components
        • 5 - Exposing Variables
        • 6 - Parent
        • 7 - States
        • 8 - State Categories
      • Animation Tutorials
        • 1 - Introduction to Animation
        • 2 - Creating an Animation
        • 3 - Playing Animations inside other Animations
        • 4 - Combining Multiple Categories
      • Examples
        • Bottom-Up Stack
        • Centering
        • Custom NineSlice
        • Health Bar
        • Padding
    • Gum Elements
      • General Properties
        • Alpha
        • Base Type (Inheritance)
        • Blend
        • Clips Children
        • Has Events
        • Height Units
        • Ignored By Parent Size
        • Locked
        • Max Height
        • Max Width
        • Min Height
        • Min Width
        • Order
        • Parent
        • Rotation
        • Visible
        • Variable References
        • Width Units
        • X
        • X Origin
        • X Units
        • Y
        • Y Origin
        • Y Units
      • Behaviors
        • Default Implementation
      • Circle
      • ColoredRectangle
      • Component
        • Default Child Container
      • Container
        • Children Layout
        • Contained Type
        • Is Render Target
        • Stack Spacing
        • Wraps Children
      • NineSlice
        • Blend
        • Custom Frame Texture Coordinate Width
        • Texture Address
        • Texture Left
        • Texture Top
      • Polygon
        • Points
      • Skia Standard Elements
        • General Properties
          • Has Dropshadow
          • Is Filled
          • Use Gradient
        • Arc
          • Start Angle
          • Sweep Angle
          • Thickness
        • Canvas
        • ColoredCircle
        • LottieAnimation
        • RoundedRectangle
          • Corner Radius
        • Svg
      • Sprite
        • Color
        • Source File
        • Texture Address
        • Wrap
      • States
        • Categories
      • Text
        • Blue
        • Color
        • Font
        • Font Scale
        • Font Size
        • Green
        • Is Bold
        • Is Italic
        • MaxLettersToShow
        • Outline Thickness
        • Red
        • Text
        • Text Overflow Horizontal Mode
        • Text Overflow Vertical Mode
        • Use Custom Font
        • Use Font Smoothing
    • Project Tab
    • Code Tab
      • Runtime Generation Details
      • Generation Scope
      • Inheritance Location
      • Is CodeGen Plugin Enabled
      • Show CodeGen Preview
    • Localization
    • Menu
      • Content
      • Project Properties
    • Plugins
      • AddAndRemoveVariablesForType
      • AddMenuItem
      • Export
      • Setting Up Post Build Events
    • Variables
      • Add Variable
    • Bitmap font generator (.fnt)
    • Upgrading
      • Migrating 2025 April 27 to Preview
      • Breaking Changes
        • Removal of Variable Spaces
  • Code
    • Gum Code Reference
      • AnimationRuntime
      • BitmapCharacterInfo
      • BitmapFont
      • Camera
      • CircleRuntime
      • ColoredRectangleRuntime
      • Component Runtimes
      • ContainerRuntime
      • Cursor
      • Gum Class Overview
      • DataUiGrid
        • Reflection
        • Categories
      • ElementSave
        • ToGraphicalUiElement
      • GraphicalUiElement
        • Absolute Values
        • AddToManagers
        • AnimateSelf
        • ApplyState
        • BlendState
        • CanvasHeight
        • CanvasWidth
        • Children
        • ContainedElements
        • Font Values
        • GetAbsoluteHeight
        • GetAbsoluteWidth
        • GetChildByNameRecursively
        • GetFrameworkElementByName
        • IsAllLayoutSuspended
        • Parent
        • RemoveFromManagers
        • UpdateLayout
      • GumProjectSave
      • IDataUi
      • InstanceMember
      • InstanceSave
      • InteractiveGue
        • CurrentInputReceiver
        • RaiseChildrenEventsOutsideOfBounds
        • RollOverBubbling
      • IPositionedSizedObject
      • Layer
      • NineSliceRuntime
      • RectangleRuntime
      • Renderer
        • SinglePixelTexture
      • SelectedState
      • SpriteRenderer
        • LastFrameDrawStates
      • SpriteRuntime
        • TextureAddress
        • TextureHeight
        • TextureLeft
        • TextureTop
        • TextureWidth
      • TextRuntime
        • Color
        • FontScale
        • FontSize
        • HorizontalAlignment
        • Text Wrapping
        • VerticalAlignment
      • VariableSave
    • FNA
    • Kni
    • Meadow
    • MonoGame
      • Setup
        • Linking Game Project to Source (Optional)
      • Tutorials
        • Gum Project Forms Tutorial
          • Setup
          • Gum Screens
          • Common Component Types
          • Styling
          • Multiple Screens
        • Code-Only Gum Forms Tutorial
          • Setup
          • Forms Controls
          • ListBox Items
          • Input in Forms
        • [Deprecated] Gum Project (.gumx) Tutorial
          • Setup
          • Gum Screens
          • Gum Forms
          • Customizing Forms
          • Strongly Typed Components Using Code Generation
          • Multiple Screens
      • Setup for GumBatch (Optional)
      • Loading a Gum Project (Optional)
      • async Programming
      • Custom Runtimes
      • GumBatch
      • Gum Forms
        • Binding (ViewModels)
        • Controls
          • Button
          • CheckBox
          • FrameworkElement
            • BindingContext
            • IsEnabled
            • IsFocused
            • ModalRoot and PopupRoot
            • SetBinding
          • ListBox
          • ListBoxItem
          • MenuItem
          • PasswordBox
          • RadioButton
          • ScrollViewer
            • VerticalScrollBarValue
          • StackPanel
          • Slider
          • TextBox
        • Control Customization In Code
          • MenuItem
        • Control Customization in Gum Tool
        • Gamepad Support
        • Keyboard Support
        • Troubleshooting
      • File Loading
      • Resizing the Game Window
      • Known Issues
      • Samples
        • MonoGameGumFormsSample
    • Nez
    • Silk.NET
    • SkiaGum
      • Setup
        • WPF
    • .NET MAUI
      • Setup
Powered by GitBook
On this page
  • Introduction
  • Creating Multiple Screens
  • Modify Your Game (Game1) Class
  • Switching Between Screens
  • Showing No Screen
  • Conclusion

Was this helpful?

Edit on GitHub
Export as PDF
  1. Code
  2. MonoGame
  3. Tutorials
  4. Gum Project Forms Tutorial

Multiple Screens

PreviousStylingNextCode-Only Gum Forms Tutorial

Last updated 1 month ago

Was this helpful?

Introduction

A full project may include multiple screens such as a TitleScreen, LevelSelectScreen, and even GameScreen which might include a HUD or PauseMenu.

This tutorial introduces how to work with multiple Gum screens. For this tutorial we will focus on two new Screens: Screen1 and Screen2. It's okay if your project has more screens, but we'll be using just these two for this tutorial.

Creating Multiple Screens

Before we write any code, we'll create two screens. As mentioned above, a full game might have many screens. For this tutorial we'll create two simple screens, each with a single button and a label.

First we'll create Screen1:

  1. Right-click on the Screens folder to add a new screen named Screen1

  2. Drag+drop a Components/Controls/Label component into Screen1

  3. Set the Label Text property on the newly created LabelInstance to Screen 1 so that you can tell that you are on Screen1 when running your game

  4. Drag+drop a ButtonStandard component into Screen1

  5. Set the Text property on the newly created ButtonStandardInstance to Go to Screen 2

  6. Arrange the two instances so they are not overlapping

Next we'll create Screen2:

  1. Add a new Screen named Screen2

  2. Drag+drop a Component/Controls/Label into Screen2

  3. Set the Text property on the newly created LabelInstance to Screen 2 so that you can tell that you are in Screen2 when running your game

  4. Drag+drop a ButtonStandard component into Screen2

  5. Set the Text property on the newly created ButtonStandardInstance to Go to Screen 1

  6. Arrange the two instances so they are not overlapping

Modify Your Game (Game1) Class

Next we'll modify our Game class so it loads Screen1.

Make sure you have already setup code generation for your project. You can verify that it is set up correctly by selecting one of your screens. You should see the Code tab appear, and it should reference your .csproj location.

You can also click the Generate Code button on each screen file to make sure that code has been properly generated.

By generating code for both screens, you can access each screen as a strongly-typed class in your project: Screen1 and Screen2 .

You can load Screen1 by adding the following code to your Game class.

public class Game1 : Game
{
    private GraphicsDeviceManager _graphics;
    GumService Gum => GumService.Default;    

    public Game1()
    {
        _graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        IsMouseVisible = true;
    }

    protected override void Initialize()
    {
        Gum.Initialize(this,
            // This is relative to Content:
            "GumProject/GumProject.gumx");

        var screen = new Screen1();
        screen.AddToRoot();

        base.Initialize();
    }
// ...
public class Game1 : Game
{
    private GraphicsDeviceManager _graphics;
    GumService Gum => GumService.Default;    

    public Game1()
    {
        _graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        IsMouseVisible = true;
    }

    protected override void Initialize()
    {
        Gum.Initialize(this,
            // This is relative to Content:
            "GumProject/GumProject.gumx");

+       var screen = new Screen1();
+       screen.AddToRoot();

        base.Initialize();
    }
// ...

Switching Between Screens

When we generated our screens earlier, Gum created code files for each, including a custom code file where we can add our own code.

We can modify Screen1.cs and Screen2.cs to include code to move from one screen to the other.

Add a Click handler to our button which removes the existing screen and creates the new screen so your two screens look like the code shown in the following blocks.

using MonoGameGum;

partial class Screen1
{
    partial void CustomInitialize()
    {
        ButtonStandardInstance.Click += (_, _) =>
        {
            GumService.Default.Root.Children.Clear();
            var screen = new Screen2();
            screen.AddToRoot();
        };
    }
}
using MonoGameGum;

partial class Screen1
{
    partial void CustomInitialize()
    {
+       ButtonStandardInstance.Click += (_, _) =>
+       {
+           GumService.Default.Root.Children.Clear();
+           var screen = new Screen2();
+           screen.AddToRoot();
+       };
    }
}
using MonoGameGum;

partial class Screen2
{
    partial void CustomInitialize()
    {
        ButtonStandardInstance.Click += (_, _) =>
        {
            GumService.Default.Root.Children.Clear();
            var screen = new Screen1();
            screen.AddToRoot();
        };
    }
}
using MonoGameGum;

partial class Screen2
{
    partial void CustomInitialize()
    {
+       ButtonStandardInstance.Click += (_, _) =>
+       {
+           GumService.Default.Root.Children.Clear();
+           var screen = new Screen1();
+           screen.AddToRoot();
+       };
    }
}

Each screen clears the root (removes the previous screen) when its button is clicked, then creates and adds the next screen to the root.

Showing No Screen

This tutorial assumes that a Gum screen is always displayed.

Games can also completely remove Gum screens altogether. To do this, do not create a new screen after calling GumService.Default.Root.Children.Clear();

Conclusion

This tutorial showed how to switch between two screens by removing the old screen and creating a new screen. Although this is a simple example, the same concepts could be applied to a full game to switch between multiple screens.

Add a Label to Screen1
Label and Button in Screen1
Label and Button in Screen2
Gum referencing .csproj folder
Screen1 loaded in game
Custom code Screen files in our project
Switching Screens with Buttons