CategoryPanel.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. GameManagerScript gms;
  16. List<Category> categories;
  17. // Start is called before the first frame update
  18. void Start()
  19. {
  20. PopulatePanel();
  21. }
  22. private void PopulatePanel() {
  23. if (categories == null) {
  24. categories = Database.Instance.GetCategories();
  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 = 18;
  40. newText.resizeTextMinSize = 8;
  41. newText.alignment = TextAnchor.MiddleCenter;
  42. }
  43. public Category GetCategoryById(int id) {
  44. if (categories == null) {
  45. categories = Database.Instance.GetCategories();
  46. }
  47. foreach (Category cat in categories) {
  48. if (cat.id == id) {
  49. return cat;
  50. }
  51. }
  52. return null;
  53. }
  54. }