StatsScript.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. Font Arial;
  10. private void Start() {
  11. AddTextComponent(statsNames, "Player",false);
  12. AddTextComponent(statsValues, "",true);
  13. AddPlayersFromRefs();
  14. }
  15. private void AddPlayersFromRefs() {
  16. int playerCount = PlayerPrefs.GetInt("PlayerCount");
  17. for (int i = 0; i < playerCount; i++) {
  18. string name = PlayerPrefs.GetString("Player" + i);
  19. AddTextComponent(statsNames, name, false);
  20. AddTextComponent(statsValues, "1", true);
  21. }
  22. }
  23. private void AddTextComponent(GameObject parent, string text, bool alignCenter) {
  24. GameObject playerHeaderGameObject = new GameObject();
  25. playerHeaderGameObject.transform.parent = parent.transform;
  26. Text newTextObject = playerHeaderGameObject.AddComponent<Text>();
  27. newTextObject.text = text;
  28. newTextObject.font = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf");
  29. newTextObject.color = new Color32(50,50,50,255);
  30. newTextObject.resizeTextForBestFit = true;
  31. newTextObject.resizeTextMinSize = 10;
  32. newTextObject.resizeTextMaxSize = 14;
  33. if (alignCenter) {
  34. newTextObject.alignment = TextAnchor.MiddleCenter;
  35. }
  36. RectTransform rectTransform = newTextObject.GetComponent<RectTransform>();
  37. rectTransform.sizeDelta = new Vector2(0, 15);
  38. rectTransform.localPosition = new Vector3(0, 0, 0);
  39. rectTransform.localScale = new Vector3(1, 1, 1);
  40. }
  41. }