CategoryPanel.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class CategoryPanel : MonoBehaviour {
  7. [Serializable]
  8. public class Category {
  9. public string name;
  10. public int id;
  11. public Color32 color;
  12. public int questionCount;
  13. }
  14. [SerializeField] GameObject gameManager;
  15. string gameMode;
  16. List<Category> categories;
  17. // Start is called before the first frame update
  18. void Start() {
  19. PopulatePanel();
  20. }
  21. private void PopulatePanel() {
  22. if (categories == null) {
  23. categories = new List<Category>();
  24. Database.Instance.GetCategories(categories, GetGameMode(), gameManager.GetComponent<GameManagerScript>().GameId);
  25. }
  26. foreach (Category cat in categories) {
  27. AddText(cat.name, cat.color);
  28. }
  29. }
  30. private void AddText(string text, Color32 color) {
  31. color.a = 255;
  32. GameObject go = new GameObject(text);
  33. go.transform.SetParent(this.transform, false);
  34. Text newText = go.AddComponent<Text>();
  35. newText.text = text;
  36. newText.color = color;
  37. newText.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
  38. newText.resizeTextForBestFit = true;
  39. newText.resizeTextMaxSize = 40;
  40. newText.resizeTextMinSize = 8;
  41. newText.alignment = TextAnchor.MiddleCenter;
  42. }
  43. private string GetGameMode() {
  44. if (gameMode == null) {
  45. gameMode = PlayerPrefs.GetString(Constants.GAME_MODE);
  46. }
  47. return gameMode;
  48. }
  49. }