Search Unity

Limiting a scoreborard

Discussion in 'Getting Started' started by Ofek2k, Dec 18, 2022.

  1. Ofek2k

    Ofek2k

    Joined:
    Dec 17, 2022
    Posts:
    1
    Hi im pretty new to unity and i took someone code and changed it a bit
    the code is making a scoreboard base of name and score from anoter script
    I want to limit the scoreboard to only 12 best players and not every one, how can I do it?




    Code (CSharp):
    1. using UnityEngine.SceneManagement;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using CodeMonkey.Utils;
    7.  
    8. public class HighscoreTable : MonoBehaviour {
    9.     private Transform entryContainer;
    10.     private Transform entryTemplate;
    11.     private List<Transform> highscoreEntryTransformList;
    12.     private int player_score;
    13.     private string player_name;
    14.     void Start()
    15.     {
    16.         bool press = ButtonPressed.pressed;
    17.         if (press)
    18.         {
    19.             player_score = score.scoring;
    20.             player_name = Score_name.your_name;
    21.             AddHighscoreEntry(player_score, player_name);
    22.         }
    23.     }
    24.  
    25.     private void Awake() {
    26.         entryContainer = transform.Find("highscoreEntryContainer");
    27.         entryTemplate = entryContainer.Find("highscoreEntryTemplate");
    28.  
    29.         entryTemplate.gameObject.SetActive(false);
    30.  
    31.         string jsonString = PlayerPrefs.GetString("highscoreTable");
    32.         Highscores highscores = JsonUtility.FromJson<Highscores>(jsonString);
    33.  
    34.       /*  if (highscores == null) {
    35.             // There's no stored table, initialize
    36.             Debug.Log("Initializing table with default values...");
    37.             AddHighscoreEntry(1000000, "CMK");
    38.             AddHighscoreEntry(897621, "JOE");
    39.             AddHighscoreEntry(872931, "DAV");
    40.             AddHighscoreEntry(785123, "CAT");
    41.             AddHighscoreEntry(542024, "MAX");
    42.             AddHighscoreEntry(68245, "AAA");
    43.             // Reload
    44.             jsonString = PlayerPrefs.GetString("highscoreTable");
    45.             highscores = JsonUtility.FromJson<Highscores>(jsonString);
    46.         }
    47.        */
    48.         // Sort entry list by Score
    49.         for (int i = 0; i < highscores.highscoreEntryList.Count; i++) {
    50.             for (int j = i + 1; j < highscores.highscoreEntryList.Count; j++) {
    51.                 if (highscores.highscoreEntryList[j].score > highscores.highscoreEntryList[i].score) {
    52.                     // Swap
    53.                     HighscoreEntry tmp = highscores.highscoreEntryList[i];
    54.                     highscores.highscoreEntryList[i] = highscores.highscoreEntryList[j];
    55.                     highscores.highscoreEntryList[j] = tmp;
    56.                 }              
    57.             }
    58.         }
    59.  
    60.         highscoreEntryTransformList = new List<Transform>();
    61.         foreach (HighscoreEntry highscoreEntry in highscores.highscoreEntryList) {
    62.             CreateHighscoreEntryTransform(highscoreEntry, entryContainer, highscoreEntryTransformList);
    63.         }
    64.     }
    65.  
    66.     private void CreateHighscoreEntryTransform(HighscoreEntry highscoreEntry, Transform container, List<Transform> transformList) {
    67.         float templateHeight = 31f;
    68.         Transform entryTransform = Instantiate(entryTemplate, container);
    69.         RectTransform entryRectTransform = entryTransform.GetComponent<RectTransform>();
    70.         entryRectTransform.anchoredPosition = new Vector2(0, -templateHeight * transformList.Count);
    71.         entryTransform.gameObject.SetActive(true);
    72.         int rank = transformList.Count + 1;
    73.         string rankString;
    74.         switch (rank) {
    75.         default:
    76.             rankString = rank + "TH"; break;
    77.  
    78.         case 1: rankString = "1ST"; break;
    79.         case 2: rankString = "2ND"; break;
    80.         case 3: rankString = "3RD"; break;
    81.         }
    82.  
    83.         entryTransform.Find("posText").GetComponent<Text>().text = rankString;
    84.  
    85.         int score = highscoreEntry.score;
    86.  
    87.         entryTransform.Find("scoreText").GetComponent<Text>().text = score.ToString();
    88.  
    89.         string name = highscoreEntry.name;
    90.         entryTransform.Find("nameText").GetComponent<Text>().text = name;
    91.  
    92.         // Set background visible odds and evens, easier to read
    93.         entryTransform.Find("background").gameObject.SetActive(rank % 2 == 1);
    94.        
    95.         // Highlight First
    96.         if (rank == 1) {
    97.             entryTransform.Find("posText").GetComponent<Text>().color = Color.green;
    98.             entryTransform.Find("scoreText").GetComponent<Text>().color = Color.green;
    99.             entryTransform.Find("nameText").GetComponent<Text>().color = Color.green;
    100.         }
    101.  
    102.         // Set tropy
    103.         switch (rank) {
    104.         default:
    105.             entryTransform.Find("trophy").gameObject.SetActive(false);
    106.             break;
    107.         case 1:
    108.             entryTransform.Find("trophy").GetComponent<Image>().color = UtilsClass.GetColorFromString("FFD200");
    109.             break;
    110.         case 2:
    111.             entryTransform.Find("trophy").GetComponent<Image>().color = UtilsClass.GetColorFromString("C6C6C6");
    112.             break;
    113.         case 3:
    114.             entryTransform.Find("trophy").GetComponent<Image>().color = UtilsClass.GetColorFromString("B76F56");
    115.             break;
    116.  
    117.         }
    118.  
    119.         transformList.Add(entryTransform);
    120.     }
    121.  
    122.     private void AddHighscoreEntry(int score, string name) {
    123.         // Create HighscoreEntry
    124.         HighscoreEntry highscoreEntry = new HighscoreEntry { score = score, name = name };
    125.        
    126.         // Load saved Highscores
    127.         string jsonString = PlayerPrefs.GetString("highscoreTable");
    128.         Highscores highscores = JsonUtility.FromJson<Highscores>(jsonString);
    129.  
    130.         if (highscores == null) {
    131.             // There's no stored table, initialize
    132.             highscores = new Highscores() {
    133.                 highscoreEntryList = new List<HighscoreEntry>()
    134.             };
    135.         }
    136.  
    137.         // Add new entry to Highscores
    138.         highscores.highscoreEntryList.Add(highscoreEntry);
    139.  
    140.         // Save updated Highscores
    141.         string json = JsonUtility.ToJson(highscores);
    142.         PlayerPrefs.SetString("highscoreTable", json);
    143.         PlayerPrefs.Save();
    144.     }
    145.  
    146.     private class Highscores {
    147.         public List<HighscoreEntry> highscoreEntryList;
    148.     }
    149.  
    150.     /*
    151.      * Represents a single High score entry
    152.      * */
    153.     [System.Serializable]
    154.     private class HighscoreEntry {
    155.         public int score;
    156.         public string name;
    157.     }
    158.  
    159. }
    160.  
     
  2. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    Replace foreach with for loop or add a counter inside.

    Code (CSharp):
    1. highscoreEntryTransformList = new List<Transform>();
    2. int loopNumber = 0;
    3. foreach (HighscoreEntry highscoreEntry in highscores.highscoreEntryList) {
    4.     CreateHighscoreEntryTransform(highscoreEntry, entryContainer, highscoreEntryTransformList);
    5.     loopNumber++;
    6.    
    7.     if(loopNumber >= 12) {
    8.         break;
    9.     }
    10. }