Jump to content
Unity Insider Forum

Niels1972

Newbie
  • Gesamte Inhalte

    1
  • Benutzer seit

  • Letzter Besuch

Über Niels1972

  • Geburtstag 05.04.1972

Profile Information

  • Gender
    Male

Niels1972's Achievements

Newbie

Newbie (1/3)

0

Ansehen in der Community

  1. Guten Tag Ich bin gerade dabei ein Highscore Tabelle zu erstellen (bin ein Neuling in Unity und in C#. Deswegen hab ich recht viele Videos mir angekuckt hab 2 gute mir rausgesucht (Unten sind die verlinkt) hab alles nach Anweisung gemacht (einmal den Inputfiel für den Namen und einmal die UI->Canvas für den Highscore) und es läuft alles prima. Aber die Frage ist jetzt von mir wie bekomme ich es hin das der eingetragene Name in der Tabelle erscheint? (bin über jeden Tipp froh). _____________________________________________________________________ Name (skript) using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Name : MonoBehaviour { public Text obj_text; public InputField display; // Use this for initialization void Start () { obj_text.text = PlayerPrefs.GetString("Name..."); } // Update is called once per frame public void Create () { obj_text.text = display.text; PlayerPrefs.SetString("Name...", obj_text.text); PlayerPrefs.Save(); } } ____________________________________________________________________ Highscore (skript) using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class HighscoreTable : MonoBehaviour { private Transform entryContainer; private Transform entryTemplate; private List<Transform> highscoreEntryTransformList; private void Awake() { entryContainer = transform.Find("highscoreEntryContainer"); entryTemplate = entryContainer.Find("highscoreEntryTemplate"); entryTemplate.gameObject.SetActive(false); //PlayerPrefs.DeleteKey("highscoreTable"); string jsonString = PlayerPrefs.GetString("highscoreTable"); Highscores highscores = JsonUtility.FromJson<Highscores>(jsonString); if (highscores == null) { // There's no stored table, initialize Debug.Log("Initializing table with default values..."); AddHighscoreEntry(1000000, "CMK"); AddHighscoreEntry(897621, "JOE"); AddHighscoreEntry(872931, "DAV"); AddHighscoreEntry(785123, "CAT"); AddHighscoreEntry(542024, "MAX"); AddHighscoreEntry(68245, "AAA"); // Reload jsonString = PlayerPrefs.GetString("highscoreTable"); highscores = JsonUtility.FromJson<Highscores>(jsonString); } RefreshHighscoreTable(); } private void RefreshHighscoreTable() { string jsonString = PlayerPrefs.GetString("highscoreTable"); Highscores highscores = JsonUtility.FromJson<Highscores>(jsonString); // Sort entry list by Score 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) { // Swap HighscoreEntry tmp = highscores.highscoreEntryList[i]; highscores.highscoreEntryList[i] = highscores.highscoreEntryList[j]; highscores.highscoreEntryList[j] = tmp; } } } if (highscoreEntryTransformList != null) { foreach (Transform highscoreEntryTransform in highscoreEntryTransformList) { Destroy(highscoreEntryTransform.gameObject); } } highscoreEntryTransformList = new List<Transform>(); foreach (HighscoreEntry highscoreEntry in highscores.highscoreEntryList) { CreateHighscoreEntryTransform(highscoreEntry, entryContainer, highscoreEntryTransformList); } } private void CreateHighscoreEntryTransform(HighscoreEntry highscoreEntry, Transform container, List<Transform> transformList) { float templateHeight = 31f; 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) { default: rankString = rank + "TH"; break; case 1: rankString = "1ST"; break; case 2: rankString = "2ND"; break; case 3: rankString = "3RD"; break; } entryTransform.Find("posText").GetComponent<Text>().text = rankString; int score = highscoreEntry.score; entryTransform.Find("scoreText").GetComponent<Text>().text = score.ToString(); string name = highscoreEntry.name; entryTransform.Find("nameText").GetComponent<Text>().text = name; transformList.Add(entryTransform); } public void AddHighscoreEntry(int score, string name) { // Create HighscoreEntry HighscoreEntry highscoreEntry = new HighscoreEntry { score = score, name = name }; // Load saved Highscores string jsonString = PlayerPrefs.GetString("highscoreTable"); Highscores highscores = JsonUtility.FromJson<Highscores>(jsonString); if (highscores == null) { // There's no stored table, initialize highscores = new Highscores() { highscoreEntryList = new List<HighscoreEntry>() }; } // Add new entry to Highscores highscores.highscoreEntryList.Add(highscoreEntry); // Save updated Highscores string json = JsonUtility.ToJson(highscores); PlayerPrefs.SetString("highscoreTable", json); PlayerPrefs.Save(); RefreshHighscoreTable(); } private class Highscores { public List<HighscoreEntry> highscoreEntryList; } /* * Represents a single High score entry * */ [System.Serializable] private class HighscoreEntry { public int score; public string name; } } _____________________________________________________________________________________________________________________________ (Super videos) https://www.youtube.com/watch?v=uoLk9iBTnls https://www.youtube.com/watch?v=iAbaqGYdnyI&list=PLJNehZ2ZSk-YdkaJWFuBpnypTlqT5ePbg&index=21
×
×
  • Neu erstellen...