Question by
faizworksin · Aug 17, 2020 at 08:34 PM ·
leaderboardupdate problemscoreboard
High score leader board update problem
[1]: /storage/temp/165709-capture.png
My code manage to update the leader board if the score score height from the list, but when update there is issue with data
using JetBrains.Annotations;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.SocialPlatforms.Impl;
using UnityEngine.UI;
public class HighScoreTable : MonoBehaviour
{
private Transform entryContainer;
private Transform entryTemplate;
private List<Transform> highscoreEntryTransformList;
private int playerscore;
private bool check = false;
public string TheName;
public DateTime dateNow;
public GameObject InputNamePanel;
public GameObject SaveButton;
public GameObject CancelButton;
public GameObject InputName;
//[SerializeField] private HighScoreTable highscoretable;
private List<HighscoreEntry> highscoreEntryList;
public void Update()
{
entryContainer = transform.Find("HighScoreContainer");
entryTemplate = entryContainer.Find("HighScoreTemplate");
entryTemplate.gameObject.SetActive(false);
playerscore = GameManager.instance.PlayerScore;
if (PlayerPrefs.GetString("highscoreTable") == "")
{
Debug.Log("Masuk");
highscoreEntryList = new List<HighscoreEntry>()
{
new HighscoreEntry{ score = 1, name = "James", date = "16/08/2020"},
new HighscoreEntry{ score = 1, name = "Jamie", date = "16/08/2020"},
};
highscoreEntryTransformList = new List<Transform>();
Highscores higscores = new Highscores { highscoreEntryList = highscoreEntryList };
string json = JsonUtility.ToJson(higscores);
PlayerPrefs.SetString("highscoreTable", json);
PlayerPrefs.Save();
Debug.Log(PlayerPrefs.GetString("highscoreTable"));
}
string jsonString = PlayerPrefs.GetString("highscoreTable");
Highscores highscores = JsonUtility.FromJson<Highscores>(jsonString);
for (int i = 0; i < highscores.highscoreEntryList.Count; i++)
{
if (playerscore > highscores.highscoreEntryList[i].score)
check = true;
else if (highscores.highscoreEntryList == null)
check = true;
}
if (check == true)
{
InputNamePanel.SetActive(true);
}
for (int i = 0; i < highscores.highscoreEntryList.Count; i++)
{
for (int j = i + 1; j < highscores.highscoreEntryList.Count; j++)
{
if (highscores.highscoreEntryList[j].score > highscores.highscoreEntryList[i].score)
{
HighscoreEntry tmp = highscores.highscoreEntryList[i];
highscores.highscoreEntryList[i] = highscores.highscoreEntryList[j];
highscores.highscoreEntryList[j] = tmp;
}
}
}
highscoreEntryTransformList = new List<Transform>();
foreach (HighscoreEntry highscoreEntry in highscores.highscoreEntryList.Take(5))
{
CreateHighScoreEntryTransform(highscoreEntry, entryContainer, highscoreEntryTransformList);
}
}
private void CreateHighScoreEntryTransform(HighscoreEntry highscoreEntry, Transform container, List<Transform>transformList)
{
float templateHeight = 20f;
Transform entryTransform = Instantiate(entryTemplate, container);
RectTransform entryRectTransform = entryTransform.GetComponent<RectTransform>();
entryRectTransform.anchoredPosition = new Vector2(0, -templateHeight * transformList.Count);
entryTransform.gameObject.SetActive(true);
int rank = transformList.Count + 1;
string rankString;
switch (rank)
{
case 1:
rankString = "1ST";
break;
case 2:
rankString = "2ND";
break;
case 3:
rankString = "3RD";
break;
default:
rankString = rank + "TH";
break;
}
entryTransform.Find("RankText").GetComponent<TextMeshProUGUI>().text = rankString;
string name = highscoreEntry.name;
entryTransform.Find("NameText").GetComponent<TextMeshProUGUI>().text = name;
string date = highscoreEntry.date;
entryTransform.Find("DoPText").GetComponent<TextMeshProUGUI>().text = date.ToString();
int score = highscoreEntry.score;
entryTransform.Find("ScoreText").GetComponent<TextMeshProUGUI>().text = score.ToString();
transformList.Add(entryTransform);
}
public void Cancel()
{
check = false;
GameManager.instance.PlayerScore = 0;
InputNamePanel.SetActive(false);
}
public void InputDetail()
{
TheName = InputName.GetComponent<TextMeshProUGUI>().text;
dateNow = DateTime.Today;
string date = dateNow.ToString("dd/MM/yyyy");
AddHighscoreEntry(playerscore, TheName, date);
check = false;
GameManager.instance.PlayerScore = 0;
InputNamePanel.SetActive(false);
}
private void AddHighscoreEntry(int score, string name, string date)
{
//create Highscore
HighscoreEntry highscoreEntry = new HighscoreEntry { score = score, name = name, date = date};
//Load score
string jsonString = PlayerPrefs.GetString("highscoreTable");
Highscores highscores = JsonUtility.FromJson<Highscores>(jsonString);
//add new entry
highscores.highscoreEntryList.Add(highscoreEntry);
//save updated
string json = JsonUtility.ToJson(highscores);
PlayerPrefs.SetString("highscoreTable", json);
PlayerPrefs.Save();
}
private class Highscores
{
public List<HighscoreEntry> highscoreEntryList;
}
[System.Serializable]
private class HighscoreEntry
{
public int score;
public string name;
public string date;
}
}
capture.png
(335.2 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613005841im_/https://answers.unity.com/themes/thub/images/avi.jpg)