CategoryPanel.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. gms = GameObject.Find("GameManager").GetComponent<GameManagerScript>();
  21. categories = gms.GetDatabase().GetCategories();
  22. PopulatePanel();
  23. }
  24. private void PopulatePanel() {
  25. foreach (Category cat in categories) {
  26. AddText(cat.name, cat.color);
  27. }
  28. }
  29. private void AddText(string text, Color32 color) {
  30. color.a = 255;
  31. GameObject go = new GameObject(text);
  32. go.transform.SetParent(this.transform, false);
  33. Text newText = go.AddComponent<Text>();
  34. newText.text = text;
  35. newText.color = color;
  36. newText.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
  37. newText.resizeTextForBestFit = true;
  38. newText.resizeTextMaxSize = 18;
  39. newText.resizeTextMinSize = 8;
  40. /*
  41. LayoutElement le = go.AddComponent<LayoutElement>();
  42. le.preferredHeight = 20f;
  43. le.minHeight = 10f;
  44. */
  45. newText.alignment = TextAnchor.MiddleCenter;
  46. }
  47. public Category GetCategoryById(int id) {
  48. foreach (Category cat in categories) {
  49. if (cat.id == id) {
  50. return cat;
  51. }
  52. }
  53. return null;
  54. }
  55. }