CategoryPanel.cs 1.8 KB

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