This page has been translated automatically.
Unigine Basics
1. Introduction
2. Managing Virtual Worlds
3. Preparing 3D Models
4. Materials
5. Cameras and Lighting
6. Implementing Application Logic
7. Making Cutscenes and Recording Videos
8. Preparing Your Project for Release
9. Physics
10. Optimization Basics
12. PROJECT3: Third-Person Cross-Country Arcade Racing Game
13. PROJECT4: VR Application With Simple Interaction
Warning! This version of documentation is OUTDATED, as it describes an older SDK version! Please switch to the documentation for the latest SDK version.
Warning! This version of documentation describes an old SDK version which is no longer supported! Please upgrade to the latest SDK version.

GUI for Crosshair and Current Game Stats

In games and simulators, the HUD (Heads-Up Display) is used to showcase important information or graphical elements. Our project also requires a HUD, where we will display current game statistics and provide a crosshair to aid the player in shooting. To achieve this, we can obtain the current GUI (Graphical User Interface) and add the required widgets as children. For the crosshair in the center of the screen, we will use WidgetSprite, which enables us to display any image in the UI.

  1. Create a HUD.cs component and copy the following code into it:

    HUD.cs

    Source code (C#)
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using Unigine;
    
    [Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- identifier is generated automatically for a new component
    public class HUD : Component
    {
        // crosshair parameters
        public AssetLink crosshairImage = null;
        public int crosshairSize = 16;
    
        private WidgetSprite sprite = null;
        private Gui screenGui = null;
        private ivec2 prev_size = new ivec2(0, 0);
    
    
        private void Init()
        {
       	 // get the current screen GUI
       	 screenGui = Gui.GetCurrent();
    
       	 // add WidgetSprite for the crosshair
       	 sprite = new WidgetSprite(screenGui, crosshairImage.AbsolutePath);
       	 // set the sprite size
       	 sprite.Width = crosshairSize;
       	 sprite.Height = crosshairSize;
    
       	 // add the sprite to GUI so that it would always be in the center of the screen and overlap other widgets
       	 screenGui.AddChild(sprite, Gui.ALIGN_CENTER | Gui.ALIGN_OVERLAP);
       	 // bind the widget lifetime to the world
       	 sprite.Lifetime = Widget.LIFETIME.WORLD;
        }
    
        private void Update()
        {
       	 ivec2 new_size = screenGui.Size;
       	 if (prev_size != new_size)
       	 {
       		 screenGui.RemoveChild(sprite);
       		 screenGui.AddChild(sprite, Gui.ALIGN_CENTER | Gui.ALIGN_OVERLAP);
       	 }
       	 prev_size = new_size;
        }
    }
  2. Create a NodeDummy, place it somewhere in the scene, name it HUD and add the HUD.cs component to it.

  3. Add the data/hud/crosshair.png file to the Crosshair Image field.

After compiling and running the application, you should get the following result:

Last update: 2024-04-04
Build: ()