Consulting, services, computer engineering. Implementation of technology solutions and support for businesses.

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

Windows 11 v26100 Csharp First FPS; GUI graphical user interface and win condition

 

Windows 11 26100 CsharpFirstFPS gui graphical user interface and win condition done

 

Windows 11 v26100 Csharp First FPS; GUI graphical user interface and win condition

 

 

Who am i / About the author...

ident021 180 facebook linkedin insider mcp learn metadatas sans cravate 128x128 autocolors 18052006 191123 mcse microsoft logomcsa microsoft logomcdba logo

since 2001, Insider since 2014.

 

Thanks to Microsoft EMEA GBS to let me participate in the year 2014 to the Insider project during my work. Then, This Windows test project, here in version 11, is based on the cycle of the insider Preview Builds, now in the Dev Channel: it is fun to test the new features. I can learn new features, validate them and share them with you :-)

Why game engines? Because as a system/network/database developer I only see consoles and screens with unfunny and ugly text. With game engines i can show you colors and animations, that's more fun and more eye-catching :)

This tutorial is made in a test environment, for pleasure and not to earn money. It is a non-profit project. It shows that the concept is possible, for a demontration, artistic, or educational purposes, school learning course. Don't test in a real environment in production. 

 

Windows 11 v26100 Csharp First FPS; GUI graphical user interface and win condition : last articles

 

 

how to install Godot engine

Windows 11 v23619 testing with a Unity game engine Hello World

How to use signals with Godo Engine (Red bouncing ball)

A Paddle Game with Godot engine

Windows 11 v26058 C Sharp with Copilot Godot Engine multiplayer client server POC

Windows 11 v26080 Blender installation

Windows 11 v26100 Csharp First FPS building the level

Windows 11 v26100 Csharp First FPS creating a health pickup

Windows 11 v26100 Csharp First FPS moving the player

Windows 11 v26100 Csharp First FPS Scripting camera behavior to follow the player

Windows 11 v26100 Csharp First FPS Player is shooting bullets

 

 

Windows 11 v26100 Csharp First FPS; GUI graphical user interface and win condition : GUI

Goal : shows our player’s stats to display health, win and loss conditions

UI elements in Unity can be added in the following two ways:

•  Unity UI (uGUI)

•  UI Toolkit (isn’t written in C#)

//docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/index.html

add a simple UI to the game scene that displays the items collected, player

health, and progress information variables that are stored in GameBehavior.cs

Hierarchy/UI/Text-TextMeshPro

 

 Windows 11 v26100 Csharp First FPS GUI graphical user interface and win condition import TextMeshPro

 

I Click on « Import TMP Essentials » only.

A new canvas game object appears in the Hiearchy, with a child game object : text (TMP)

 

Right click Hiearchy / text (TMP) /UI / Legacy/Text Mesh Pro

I Click « 2D » tab on the scene and i see « New Text Mesh Pro ».

 

I rename it Health

Hierarchy/Select Health/ Inspector : rect transform/ top left

Like this :

 

Windows 11 v26100 Csharp First FPS GUI graphical user interface and win condition top left

 

 

Color : Black

Rect transform X=110, Y=-35

Text property : Health :

 

I Repeat operation with « items : »  Pos X=110, Y=-85, text property : Items :

I Repeat operation with « Progress : »  anchor=Bottom center, Pos X=0, Y=15, Z=0

text property : Collect all the items to win !

Width 435, height : 50

connect the variables

I update GameBehavior.cs like this

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// add the TMPro namespace so we have access to the TMP_Text variable type
using TMPro;
// using directive for UnityEngine.UI to access the Button class
using UnityEngine.UI;
// We add the SceneManagement namespace with the using keyword, which handles all scene-related logic like creating loading scenes.
using UnityEngine.SceneManagement;


public class GameBehavior : MonoBehaviour
{

// UI button variable to connect to our Win Condition button in the Hierarchy
public Button WinButton;

// new public variable for the max number of items in the level
public int MaxItems = 4;
// TMP_Text variables, which we connect in the Inspector panel
public TMP_Text HealthText;
public TMP_Text ItemText;
public TMP_Text ProgressText;


// set the initial values of our health and items text using the += operator
void Start()
{
ItemText.text += _itemsCollected;
HealthText.text += _playerHP;
}

//private because they should only be modifiable in this class
private int _itemsCollected = 0;


// public variable called Items with get and set properties
public int Items
{
// return the value stored in _itemsCollected whenever Items are accessed from an outside class
get { return _itemsCollected; }
// set property to assign _itemsCollected to the new value of Items whenever it’s updated
set
{
_itemsCollected = value;
Debug.LogFormat("Items: {0}", _itemsCollected);
// update the text property of ItemText to show the updated items count.
ItemText.text = "Items Collected: " + Items;

// if statement in the set property of _itemsCollected
if (_itemsCollected >= MaxItems)
{
ProgressText.text = "You've found all the items!";

// Since we set the Win Condition button as Hidden when the game starts, we reactivate it when the game is won.
WinButton.gameObject.SetActive(true);
// We set Time.timeScale to 0 to pause the game when the win screen is displayed, which disables any input or movement.
Time.timeScale = 0f;
}
else
{
ProgressText.text = "Item found, only " + (MaxItems - _itemsCollected) + " more!";
}

}
}

public void RestartScene()
{
// We create a new method called RestartScene and call LoadScene() when the win screen button is clicked:
SceneManager.LoadScene(0);
// only one scene in our project, we use index 0 to restart the game from the beginning
// when the scene restarts, all controls and behaviors will be able to execute again
Time.timeScale = 1f;

}

private int _playerHP = 10;

public int HP
{
get { return _playerHP; }
set
{
_playerHP = value;
// update the text property of HealthText with the new value
HealthText.text = "Player Health: " + HP;
Debug.LogFormat("Lives: {0}", _playerHP);
}
}

}

 

Hierarchy/Game_Manager/Inspector : the 3 variables appears

I Select Game_Manager in the Hierarchy and drag over our three text objects one by one

into their corresponding GameBehavior script fields in the Inspector:

 

Windows 11 v26100 Csharp First FPS GUI graphical user interface and win condition three text objects

 

Windows 11 v26100 Csharp First FPS; GUI graphical user interface and win condition : button You Won!

 

I right-click in the Hierarchy and select + UI / Button - TextMeshPro,

I rename this button « Win Condition »

I  Select Win Condition and set the Pos X and Pos Y to 0, its Width to 225, and its Height to 115

I Click on the arrow to the right of the Win Condition button to expand its text child object,

Then i change the text in inspector/Text input : You won!:

 

Windows 11 v26100 Csharp First FPS GUI graphical user interface and win condition button you won

 

Hierarchy/Win condition/Inspector : I uncheck the checked button to hide the gameobject  when there is no victory : the button will be hidden until we win the game.

 

Windows 11 v26100 Csharp First FPS GUI graphical user interface and win condition hide you win button

 

I select Game_Manager in the Hierarchy and drag the Win Condition button from the Hierarchy to the Game Behavior (Script) in the Inspector

 

Windows 11 v26100 Csharp First FPS GUI graphical user interface and win condition button you won drag

 

 

Windows 11 v26100 Csharp First FPS; GUI graphical user interface and win condition : pause and restarting the game

 

Unity provides a property in the Time class called timeScale, which when set to 0 freezes the game scene.

I modify the GameBehavior.cs C# script like this:

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// add the TMPro namespace so we have access to the TMP_Text variable type
using TMPro;
// using directive for UnityEngine.UI to access the Button class
using UnityEngine.UI;
// We add the SceneManagement namespace with the using keyword, which handles all scene-related logic like creating loading scenes.
using UnityEngine.SceneManagement;

public class GameBehavior : MonoBehaviour
{

// UI button variable to connect to our Win Condition button in the Hierarchy
public Button WinButton;


// new public variable for the max number of items in the level
public int MaxItems = 4;
// TMP_Text variables, which we connect in the Inspector panel
public TMP_Text HealthText;
public TMP_Text ItemText;
public TMP_Text ProgressText;

// set the initial values of our health and items text using the += operator
void Start()
{
ItemText.text += _itemsCollected;
HealthText.text += _playerHP;
}

//private because they should only be modifiable in this class
private int _itemsCollected = 0;

// public variable called Items with get and set properties
public int Items
{
// return the value stored in _itemsCollected whenever Items are accessed from an outside class
get { return _itemsCollected; }
// set property to assign _itemsCollected to the new value of Items whenever it’s updated
set
{
_itemsCollected = value;
Debug.LogFormat("Items: {0}", _itemsCollected);
// update the text property of ItemText to show the updated items count.
ItemText.text = "Items Collected: " + Items;

// if statement in the set property of _itemsCollected
if (_itemsCollected >= MaxItems)
{
ProgressText.text = "You've found all the items!";

// Since we set the Win Condition button as Hidden when the game starts, we reactivate it when the game is won.
WinButton.gameObject.SetActive(true);
// We set Time.timeScale to 0 to pause the game when the win screen is displayed, which disables any input or movement.
Time.timeScale = 0f;
}
else
{
ProgressText.text = "Item found, only " + (MaxItems - _itemsCollected) + " more!";
}

}
}

public void RestartScene()
{
// We create a new method called RestartScene and call LoadScene() when the win screen button is clicked:
SceneManager.LoadScene(0);
// only one scene in our project, we use index 0 to restart the game from the beginning
// when the scene restarts, all controls and behaviors will be able to execute again
Time.timeScale = 1f;

}


private int _playerHP = 10;

public int HP
{
get { return _playerHP; }
set
{
_playerHP = value;
// update the text property of HealthText with the new value
HealthText.text = "Player Health: " + HP;
Debug.LogFormat("Lives: {0}", _playerHP);
}
}


}

 

I select Win Condition from the Hierarchy, scroll down in the Inspector to the OnClick section of the Button component, and hit the plus icon

Hierarchy, drag the Game_Manager into the slot underneath Runtime to tell  the button we want to choose a method from our manager script to fire when the button is pushed

I select GameBehavior | RestartScene () to set the method we want the button to execute.

I hit play, F5 to test: the player won when he grabs the 4 health pills, then game is paused and can be restarted by pressing the You won button!

 

Windows 11 v26100 Csharp First FPS; GUI graphical user interface and win condition : conclusion done

 

Done! i created the project Csharp First FPS GUI graphical user interface and win condition, on windows 11 version 26100.

Windows 11 26100 CsharpFirstFPS gui graphical user interface and win condition done

Windows 11 v26100 Csharp GUI graphical user interface and win condition : need help ? 

Need help with Windows 11 or C# with Unity?

Fill this form or send an email on loic @consultingit.fr or This email address is being protected from spambots. You need JavaScript enabled to view it. and i will be happy to help you :)