StatsScript.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class StatsScript : MonoBehaviour {
  7. public GameObject statsNames;
  8. public GameObject statsValues;
  9. private Text[] statsTextNames;
  10. private Text[] statsTextValues;
  11. GameManagerScript gms;
  12. private void Start() {
  13. gms = GameObject.Find("GameManager").GetComponent<GameManagerScript>();
  14. AddTextComponent(statsNames, "Player",false,-1);
  15. AddTextComponent(statsValues, "",true,-1);
  16. //AddPlayersFromRefs();
  17. statsTextNames = statsNames.GetComponentsInChildren<Text>();
  18. statsTextValues = statsValues.GetComponentsInChildren<Text>();
  19. }
  20. public void AddPlayersToStats(List<KeyValuePair<string,int>> players) {
  21. int count = players.Count;
  22. for (int i = 0; i < count; i++) {
  23. AddTextComponent(statsNames, players[i].Key, false, i);
  24. AddTextComponent(statsValues, players[i].Value.ToString(), true, i);
  25. }
  26. }
  27. private void AddTextComponent(GameObject parent, string text, bool alignCenter, int index) {
  28. GameObject playerHeaderGameObject = new GameObject();
  29. playerHeaderGameObject.transform.parent = parent.transform;
  30. Text newTextObject = playerHeaderGameObject.AddComponent<Text>();
  31. newTextObject.text = text;
  32. newTextObject.font = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf");
  33. newTextObject.color = new Color32(50,50,50,255);
  34. newTextObject.resizeTextForBestFit = true;
  35. newTextObject.resizeTextMinSize = 10;
  36. newTextObject.resizeTextMaxSize = 14;
  37. if (index >= 0) {
  38. newTextObject.name = "Player" + index;
  39. }
  40. if (alignCenter) {
  41. newTextObject.alignment = TextAnchor.MiddleCenter;
  42. }
  43. RectTransform rectTransform = newTextObject.GetComponent<RectTransform>();
  44. rectTransform.sizeDelta = new Vector2(0, 15);
  45. rectTransform.localPosition = new Vector3(0, 0, 0);
  46. rectTransform.localScale = new Vector3(1, 1, 1);
  47. }
  48. public void MakeBold(string playerName) {
  49. statsTextNames = statsNames.GetComponentsInChildren<Text>();
  50. statsTextValues = statsValues.GetComponentsInChildren<Text>();
  51. for (int i = 0; i < statsTextNames.Length; i++) {
  52. if (statsTextNames[i].text.Equals(playerName)) {
  53. statsTextNames[i].fontStyle = FontStyle.Bold;
  54. statsTextValues[i].fontStyle = FontStyle.Bold;
  55. } else {
  56. statsTextNames[i].fontStyle = FontStyle.Normal;
  57. statsTextValues[i].fontStyle = FontStyle.Normal;
  58. }
  59. }
  60. }
  61. public void IncreaseRoundValue() {
  62. foreach (Text text in statsTextValues) {
  63. if (text.name.Equals("Stats Value Round")) {
  64. Int32.TryParse(text.text, out int round);
  65. round++;
  66. text.text = round.ToString();
  67. gms.GetDatabase().SetRoundValue(gms.GameId, round);
  68. break;
  69. }
  70. }
  71. }
  72. public void SetQuestionsInAnswerLine(string playerName, int count) {
  73. for (int i = 0; i < statsTextNames.Length; i++) {
  74. if (statsTextNames[i].text.Equals(playerName)) {
  75. statsTextValues[i].text = count.ToString();
  76. }
  77. }
  78. }
  79. }