Last active
March 19, 2023 08:36
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// @kurtdekker | |
// | |
// ULTRA-simple fully-static GameManager for simple arcade-style games. | |
// | |
// NOTE: you DO NOT place this file into any scene! It is a pure static data container. | |
// | |
// Usage: | |
// - at start of game call GM.InitGame() | |
// - when you earn points, call GM.AddPoints(1234); | |
// - when you want to display score, get it from GM.Score (see GMShowScore.cs below) | |
// - when you want to display lives, get it from GM.Lives | |
// | |
// To persistently store high scores, see https://pastebin.com/VmngEK05 | |
// | |
public static class GM | |
{ | |
public static int Score; | |
public static int Lives; | |
// TODO: go nuts if you want to add more stuff like health and wave and coins, etc.! | |
public static void InitGame() | |
{ | |
Score = 0; | |
Lives = 4; | |
} | |
public static void AddScore( int points) | |
{ | |
Score += points; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
// @kurtdekker | |
// Put this ON the UnityEngine.UI.Text object you're using to display score | |
public class GMShowScore : MonoBehaviour | |
{ | |
int lastScore; | |
void UpdateToText() | |
{ | |
var text = GetComponent<UnityEngine.UI.Text>(); | |
// Be SURE this GameObject has a UnityEngine.UI.Text component on it! | |
text.text = "Score:" + lastScore; | |
} | |
void Start() | |
{ | |
UpdateToText(); | |
} | |
void Update () | |
{ | |
if (GM.Score != lastScore) | |
{ | |
lastScore = GM.Score; | |
UpdateToText(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Read comments at the top of each file for integration. In total you will need to:
NOTE: you do NOT put GM.cs into your scene: it is a pure static data container.