CategoryPanel.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. string gameMode;
  17. GameManagerScript gms;
  18. List<Category> categories;
  19. // Start is called before the first frame update
  20. void Start()
  21. {
  22. PopulatePanel();
  23. }
  24. private void PopulatePanel() {
  25. if (categories == null) {
  26. categories = new List<Category>();
  27. Database.Instance.GetCategories(categories, GetGameMode());
  28. }
  29. foreach (Category cat in categories) {
  30. AddText(cat.name, cat.color);
  31. }
  32. }
  33. private void AddText(string text, Color32 color) {
  34. color.a = 255;
  35. GameObject go = new GameObject(text);
  36. go.transform.SetParent(this.transform, false);
  37. Text newText = go.AddComponent<Text>();
  38. newText.text = text;
  39. newText.color = color;
  40. newText.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
  41. newText.resizeTextForBestFit = true;
  42. newText.resizeTextMaxSize = 18;
  43. newText.resizeTextMinSize = 8;
  44. newText.alignment = TextAnchor.MiddleCenter;
  45. }
  46. public Category GetCategoryById(int id) {
  47. if (categories == null) {
  48. categories = new List<Category>();
  49. Database.Instance.GetCategories(categories, GetGameMode());
  50. }
  51. foreach (Category cat in categories) {
  52. if (cat.id == id) {
  53. return cat;
  54. }
  55. }
  56. return null;
  57. }
  58. private string GetGameMode() {
  59. if (gameMode == null) {
  60. gameMode = PlayerPrefs.GetString("GameMode");
  61. }
  62. return gameMode;
  63. }
  64. }