using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class StatsScript : MonoBehaviour { public GameObject statsNames; public GameObject statsValues; public GameObject statsLinePrefab; private Text[] statsTextNames; private Text[] statsTextValues; private List statLines = new List(); GameManagerScript gms; private void Start() { gms = GameObject.Find("GameManager").GetComponent(); CreateStandardStats(); AddPlayersToStats(gms.GetPlayers()); } private void CreateStandardStats() { StatsLine round = CreateStatLine(); round.SetStatName("Round"); int roundValue = gms.GetDatabase().GetRoundValue(gms.GameId); round.SetStatValue(roundValue.ToString()); round.name = "roundStat"; StatsLine lostQuestions = CreateStatLine(); lostQuestions.SetStatName("Questions lost"); lostQuestions.SetStatValue("0"); lostQuestions.name = "questionsLost"; StatsLine playerTitle = CreateStatLine(); playerTitle.SetStatName("Players"); playerTitle.SetStatValue(""); playerTitle.MakeBold(); statLines.Add(round); statLines.Add(lostQuestions); statLines.Add(playerTitle); } private StatsLine CreateStatLine() { GameObject slp = Instantiate(statsLinePrefab, new Vector2(0, 0), Quaternion.identity) as GameObject; StatsLine statLine = slp.GetComponent(); statLine.transform.SetParent(this.transform); return statLine; } public void AddPlayersToStats(List> players) { foreach (KeyValuePair player in players) { StatsLine p = CreateStatLine(); p.SetStatName(player.Key); p.SetStatValue(player.Value.ToString()); statLines.Add(p); } } public void IncreaseRoundValue() { foreach (StatsLine sl in statLines) { if (sl.GetName().Equals("Round")) { Int32.TryParse(sl.GetValue(), out int round); round++; sl.SetStatValue(round.ToString()); break; } } } public void MakeBold(string playerName) { foreach (StatsLine sl in statLines) { if (sl.GetName().Equals(playerName)) { sl.MakeBold(); } else { sl.UnBold(); } } } public void SetQuestionsInAnswerLine(string playerName, int count) { foreach (StatsLine sl in statLines) { if (sl.GetName().Equals(playerName)) { sl.SetStatValue(count.ToString()) ; break; } } return; } }