Table.cs
C# script
список с классом из полей
using UnityEngine;
public class Table:MonoBehaviour
{
private Trasform entryContainer;
private Transform entryTemplate;
private List<RowEntry> RowEntryList;
private List<Transform> RowEntryTransformList;
private void Awake(){
entryContainer = transform.Find("entryContainer");
entryTemplate = entryContainer.Find("entryTemplate");
entryTemplate.gameobject.SetActive(false);
RowEntryList = new List<RowEntry>(){
new RowEntry{ score = 1, name = "A"},
new RowEntry{ score = 2, name = "B"},
new RowEntry{ score = 3, name = "V"},
};
RowEntryTranformList = new List<Transform>();
foreach (RowEntry Entry in RowEntryTranformList) {
CreateRowEntryTransform(Entry, entryContainer, RowEntryTranformList)
}
}
private class RowEntry {
public int score;
public string name;
}
private void CreateRowEntryTransform(RowEntry Entry, Transform container, List<Transform> transformList) {
float templateHeight = 20f;
Transform entryTransform = Instantiate(entryTemplate, entryContainer);
RectTransform entryRectTransform = entryTransform.GetComponent<RectTransform>();
entryRectTransform.anchoredPosition = new Vector2(0, -templateHeight * transformList.Count);
entryTransform.gameObject.SetActive(true);
int rank = transformList.Count + 1;
string rankString;
switch (rank) {
default:
rankString = rank + "TH"; break;
case 1: rankString = "1ST"; break;
case 2: rankString = "2NF"; break;
case 3: rankString = "3RD"; break;
}
entryTransform.Find("Col1").GetComponent<Text>().text = rankString;
int score = Entry.score;
entryTransform.Find("Col2").GetComponent<Text>().text = score.ToString();
string name = Entry.name;
entryTransform.Find("Col3").GetComponent<Text>().text = name;
transformList.Add(entryTransform);
}
}