CategoryPanel.cs 1.7 KB

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