using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class StatsScript : MonoBehaviour { public GameObject statsNames; public GameObject statsValues; Font Arial; private void Start() { AddTextComponent(statsNames, "Player",false); AddTextComponent(statsValues, "",true); AddPlayersFromRefs(); } private void AddPlayersFromRefs() { int playerCount = PlayerPrefs.GetInt("PlayerCount"); for (int i = 0; i < playerCount; i++) { string name = PlayerPrefs.GetString("Player" + i); AddTextComponent(statsNames, name, false); AddTextComponent(statsValues, "1", true); } } private void AddTextComponent(GameObject parent, string text, bool alignCenter) { 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 (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); } }