Skip to content

Instantly share code, notes, and snippets.

@derlin
Created April 26, 2017 06:50
Show Gist options
  • Save derlin/901d5f93b56df2894a1811861a753668 to your computer and use it in GitHub Desktop.
Save derlin/901d5f93b56df2894a1811861a753668 to your computer and use it in GitHub Desktop.
Score display
using UnityEngine;
using UnityEngine.UI;
public class ScoresTable : MonoBehaviour
{
public GameObject cellPrefab;
public GameObject noScore;
public GameObject headers;
void Start()
{
float[] scores = ScoringData.Instance.Scores;
string[] meta = ScoringData.Instance.Stats;
if (scores.Length == 0)
{
headers.SetActive(false);
gameObject.SetActive(false);
noScore.SetActive(true);
return;
}
noScore.SetActive(false);
for (int i = 0; i < scores.Length; i++)
{
// score
GameObject o = Instantiate(cellPrefab, gameObject.transform);
o.GetComponent<Text>().text = Utils.FormatTime(scores[i]);
string[] fields = meta[i].Split('#');
// purge
o = Instantiate(cellPrefab, gameObject.transform);
o.GetComponent<Text>().text = string.Format("{0}", fields[1]);
// date
o = Instantiate(cellPrefab, gameObject.transform);
o.GetComponent<Text>().text = fields[0];
}
}
}

Structure:

Panel
↳ NoScoreText - Text: text to show when no score available
↳ Headers - GameObject: 
  ↳ Header1 - Text: Duration
  ↳ Header2 - Text: Stats
  ↳ Header3 - Text: Date
↳ Cells - Panel: 
  Contains:
   * GridLayoutGroup : with fixed column count --> 3
   * ScoresTable Script

Prefabs:

  • HeaderPrefab: (optional) style of the headers
  • CellPrefab: prefab containing a Text component, the one to pass to the ScoresData script and used to instantiate the cells in the table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment