CategoryPanel.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. if (PlayerPrefs.GetString("GameMode").Equals("Local")) {
  25. Database.Instance.GetCategories(categories);
  26. } else if (PlayerPrefs.GetString("GameMode").Equals("Online")) {
  27. OnlineDatabase.Instance.GetCategories(categories);
  28. }
  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. public Category GetCategoryById(int id) {
  48. if (categories == null) {
  49. if (PlayerPrefs.GetString("GameMode").Equals("Local")) {
  50. Database.Instance.GetCategories(categories);
  51. } else if (PlayerPrefs.GetString("GameMode").Equals("Online")) {
  52. OnlineDatabase.Instance.GetCategories(categories);
  53. }
  54. }
  55. foreach (Category cat in categories) {
  56. if (cat.id == id) {
  57. return cat;
  58. }
  59. }
  60. return null;
  61. }
  62. }