unity – Making a Leaderboard


Making an attempt to create a excessive rating desk with saving and loading utilizing serialization/deserialization strategy. My code beneath compares the present gathered rating and loaded participant rating (if any).

public class GameManager : MonoBehaviour
{
    public Checklist targets;
    
    // standing of sport
    public bool gameIsActive;
    public bool gameLost;
    public bool gameComplete;

    non-public float spawnRate = 3.0f;

    // information variables
    public int rating;
    public string playerName;

    // excessive rating consumer information 
    public string bestPlayer;
    public int bestScore;

    // Audio clips for various eventualities
    public AudioClip mainSound;
    public AudioClip gameSound;
    
    // creating occasion
    public static GameManager Occasion;

    // timer length
    public float timerCountdown;

    non-public void Awake()
    {
        if (Occasion != null)
        { 
            Destroy(gameObject);
            return;
        }

        Occasion = this;
        DontDestroyOnLoad(gameObject);
    }

    // Begin known as earlier than the primary body replace
    void Begin()
    {
        LoadData();
        // Play the principle menu sound
        AudioManager.Occasion.PlaySound(mainSound, 1f);
    }
    non-public void Replace()
    {
        if (gameIsActive)
        {
            UpdateTimer();
        }
    }
    non-public void UpdateTimer()
    {
        if (timerCountdown > 0)
        {
            timerCountdown -= Time.deltaTime;
            timerCountdown = Mathf.Max(timerCountdown, 0);
        }
        else
        {
            EndGame();
        }
    }

    public void SubmitName(string args)
    {
        playerName = args;
        Debug.Log("Participant " + playerName + " has been submitted:");
    }

    public void GameStart(int setLevel)
    {
        gameIsActive = true;
        timerCountdown = 15f;
        gameComplete = false;
        gameLost = false;
        SceneManager.LoadScene(1);
        rating = 0;
        spawnRate /= setLevel;
        // Play the sport sound
        AudioManager.Occasion.PlaySound(gameSound, 0.05f);

        StartCoroutine(SpawnTargets());
    }

    IEnumerator SpawnTargets()
    {
        whereas (gameIsActive)
        {
            yield return new WaitForSeconds(spawnRate);
            
            int indexCount = Random.Vary(0, targets.Depend);
            Instantiate(targets[indexCount]);
            
        }
    }
    public void EndGame()
    {
        gameIsActive = false;
        if (rating < 0)
        {
            gameLost = true;
        }
        else 
        {
            gameComplete = true;
        }

        StartCoroutine(RestartGame());
    }

    IEnumerator RestartGame()
    {
        yield return new WaitForSeconds(3f);
        SceneManager.LoadScene(2);
    }

    public void IncreaseScore(int pointValue)
    {
        rating += pointValue;
    }

    public void DecreaseScore(int pointValue)
    {
        rating -= pointValue;
    }

    [System.Serializable]
    class SaveData
    {
        public int bestScore;
        public string bestPlayer; 
    }

    public void SaveBest(int rating, string playerName)
    { 
        SaveData saveData = new SaveData();
        saveData.bestScore = rating;
        saveData.bestPlayer = playerName;


        // rework occasion to JSON
        string json = JsonUtility.ToJson(saveData);

        attempt
        {
            // write string to a file
            File.WriteAllText(Software.persistentDataPath + "/savefile.json", json);
        }
        catch (Exception e)
        {
            Debug.LogWarning($"Failed to avoid wasting information: {e.Message}");
        }
    }
    public void LoadData()
    {
        string path = Software.persistentDataPath + "/savefile.json";
        if (File.Exists(path))
        { 
            string json = File.ReadAllText(path);

            SaveData information = JsonUtility.FromJson(json);

            bestScore = information.bestScore;
            bestPlayer = information.bestPlayer;
        }
    }
}
utilizing System.Collections.Generic;
utilizing TMPro;
utilizing UnityEngine;
utilizing UnityEngine.SceneManagement;
utilizing UnityEngine.UI;

public class LeaderboardUIHandler : MonoBehaviour
{
    public TextMeshProUGUI playerBestText;
    public TextMeshProUGUI scoreBestText;

    public Button continueButton;
    non-public string starterScene = "Starter";

    non-public void Awake()
    {
        GameManager.Occasion.LoadData();
    }
    // Begin known as earlier than the primary body replace
    void Begin()
    {
        //Debug.Log("Participant Title: " + GameManager.Occasion.bestPlayer);
        //Debug.Log("Participant Rating: " + GameManager.Occasion.bestScore);
        continueButton.onClick.AddListener(LoadStarter);
        CheckBestPlayer();
    }

    void CheckBestPlayer()
    {
        if (GameManager.Occasion.rating > GameManager.Occasion.bestScore)
        {
            GameManager.Occasion.bestScore = GameManager.Occasion.rating;
            GameManager.Occasion.bestPlayer = GameManager.Occasion.playerName;

            GameManager.Occasion.SaveBest(GameManager.Occasion.bestScore, GameManager.Occasion.bestPlayer);
            playerBestText.textual content = $"Greatest Participant: {GameManager.Occasion.bestPlayer}";
            scoreBestText.textual content = $"Greatest Rating: {GameManager.Occasion.bestScore}";
            Debug.Log("Greatest Participant Title: " + GameManager.Occasion.bestPlayer);
            Debug.Log("Greatest Participant Rating: " + GameManager.Occasion.bestScore);
        }
        else
        {
            Debug.Log("Participant " + GameManager.Occasion.playerName + " wasn't capable of beat excessive rating");
            DisplayBestPlayer();
        }
    }

    non-public void DisplayBestPlayer()
    {
        //Debug.Log($"Participant Title: {GameManager.Occasion.bestPlayer}");
        playerBestText.textual content = $"Participant Title: {GameManager.Occasion.bestPlayer}";
        Debug.Log("Loaded " + GameManager.Occasion.bestPlayer + "'s rating: " + GameManager.Occasion.bestScore);
        //Debug.Log($"Participant Rating: {GameManager.Occasion.bestScore}");
        scoreBestText.textual content = $"Participant Rating: {GameManager.Occasion.bestScore}";
    }

    void LoadStarter()
    {
        SceneManager.LoadScene(starterScene);
    }

    // Replace known as as soon as per body
    void Replace()
    {
        
    }
}
```

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles