Table of Contents

Capsule with rigid body and window

In this example, we demonstrate how to set up a 3D scene that includes a capsule with a rigid body as well as a simple window displaying a text message.

This example is organized into multiple methods for better readability and maintainability. It is structured as follows:

  • Start(Scene rootScene) This is the entry point for setting up the scene. It calls other methods to set up the 3D scene, add the capsule, load the font, and add the window.
  • AddCapsule(Scene rootScene) This method creates a 3D capsule and adds it to the scene at a specific position.
  • LoadFont() This method loads the font that will be used for the UI window.
  • AddWindow(Scene rootScene) This method calls CreateUIEntity() to create an entity with a UI component, and then adds this entity to the root scene.
  • CreateUIEntity() This method creates an entity that has a UI component. The UI component includes a canvas as its root element.
  • CreateCanvas() This method creates a canvas element that will be the root of the UI component.
  • CreateTextBlock(SpriteFont? _font) This method creates a TextBlock element that displays the message "Hello, World". It uses the loaded font and sets other properties like color and size.

This modular approach makes the code easier to understand and maintain. Each method has a clear responsibility.

Stride UI Example

View on GitHub.

using Stride.CommunityToolkit.Engine;
using Stride.CommunityToolkit.Rendering.ProceduralModels;
using Stride.CommunityToolkit.Skyboxes;
using Stride.Core.Mathematics;
using Stride.Engine;
using Stride.Graphics;
using Stride.Rendering;
using Stride.UI;
using Stride.UI.Controls;
using Stride.UI.Panels;

SpriteFont? _font;

using var game = new Game();

game.Run(start: Start);

void Start(Scene rootScene)
{
    game.SetupBase3DScene();
    game.AddSkybox();

    AddCapsule(rootScene);

    LoadFont();

    AddWindow(rootScene);
}

void AddCapsule(Scene rootScene)
{
    var entity = game.Create3DPrimitive(PrimitiveModelType.Capsule);

    entity.Transform.Position = new Vector3(0, 8, 0);

    entity.Scene = rootScene;
}

void LoadFont()
{
    _font = game.Content.Load<SpriteFont>("StrideDefaultFont");
}

void AddWindow(Scene rootScene)
{
    var uiEntity = CreateUIEntity();

    uiEntity.Scene = rootScene;
}

Entity CreateUIEntity()
{
    return new Entity
    {
        new UIComponent
        {
            Page = new UIPage { RootElement = CreateCanvas() },
            RenderGroup = RenderGroup.Group31
        }
    };
}

Canvas CreateCanvas()
{
    var canvas = new Canvas { Width = 300, Height = 100, BackgroundColor = new Color(248, 177, 149, 100) };

    canvas.Children.Add(CreateTextBlock(_font));

    return canvas;
}

TextBlock CreateTextBlock(SpriteFont? _font)
{
    if (_font is null)
    {
        Console.WriteLine("Font is null");
    }

    return new TextBlock
    {
        Text = "Hello, World",
        TextColor = Color.White,
        TextSize = 20,
        Margin = new Thickness(3, 3, 3, 0),
        Font = _font
    };
}