| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- 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<Text>();
- 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>();
- rectTransform.sizeDelta = new Vector2(0, 15);
- rectTransform.localPosition = new Vector3(0, 0, 0);
- rectTransform.localScale = new Vector3(1, 1, 1);
- }
- }
|