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; GameManagerScript gms; private void Start() { statLines = new List(); gms = GameObject.Find("GameManager").GetComponent(); CreateStandardStats(); } private void CreateStandardStats() { StatsLine round = CreateStatLine(); round.SetStatName("Round"); round.SetStatValue("1"); 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); } } private void AddTextComponent(GameObject parent, string text, bool alignCenter, int index) { GameObject playerHeaderGameObject = new GameObject(); playerHeaderGameObject.transform.parent = parent.transform; Text newTextObject = playerHeaderGameObject.AddComponent(); newTextObject.text = text; newTextObject.font = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf"); newTextObject.color = new Color32(50, 50, 50, 255); newTextObject.resizeTextForBestFit = true; newTextObject.resizeTextMinSize = 10; newTextObject.resizeTextMaxSize = 14; if (index >= 0) { newTextObject.name = "Player" + index; } if (alignCenter) { newTextObject.alignment = TextAnchor.MiddleCenter; } RectTransform rectTransform = newTextObject.GetComponent(); rectTransform.sizeDelta = new Vector2(0, 15); rectTransform.localPosition = new Vector3(0, 0, 0); rectTransform.localScale = new Vector3(1, 1, 1); } public void IncreaseRoundValue() { foreach (Text text in statsTextValues) { if (text.name.Equals("Stats Value Round")) { Int32.TryParse(text.text, out int round); round++; text.text = round.ToString(); gms.GetDatabase().SetRoundValue(gms.GameId, round); break; } } } public void SetQuestionsInAnswerLine(string playerName, int count) { for (int i = 0; i < statsTextNames.Length; i++) { if (statsTextNames[i].text.Equals(playerName)) { statsTextValues[i].text = count.ToString(); } } } }