SelectCategoryScript.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using System.Linq;
  7. public class SelectCategoryScript : MonoBehaviour
  8. {
  9. [SerializeField] Button doneButton;
  10. [SerializeField] Button closeButton;
  11. [SerializeField] GameObject CategoryToSelectPrefab;
  12. [SerializeField] GameObject SelectCategoryPanel;
  13. [SerializeField] GameObject ContentPanel;
  14. // Start is called before the first frame update
  15. private List<CategoryPanel.Category> categories;
  16. private void Start()
  17. {
  18. doneButton.onClick.AddListener(CloseDialogAction);
  19. closeButton.onClick.AddListener(CloseDialogAction);
  20. GetCategories();
  21. int pos = 0;
  22. foreach (CategoryPanel.Category cat in categories)
  23. {
  24. GameObject go = Instantiate(CategoryToSelectPrefab, Vector3.zero, Quaternion.identity);
  25. CategorySelection c = go.GetComponent<CategorySelection>();
  26. c.SetSelected(true);
  27. c.SetCategoryText(cat.name, cat.questionCount);
  28. c.transform.SetSiblingIndex(pos++);
  29. c.transform.localScale = new Vector3(1, 1, 1);
  30. c.transform.SetParent(ContentPanel.transform, false);
  31. }
  32. }
  33. private void GetCategories()
  34. {
  35. if (categories == null || categories.Count == 0) {
  36. categories = new List<CategoryPanel.Category>();
  37. categories = OnlineDatabase.Instance.GetCategories(categories, -1);
  38. }
  39. }
  40. internal void CloseDialogAction() {
  41. SelectCategoryPanel.GetComponent<selectCategoriesPanelController>().UpdateCategoryText();
  42. gameObject.SetActive(false);
  43. }
  44. internal int GetSelectedCategoriesQuestionCount()
  45. {
  46. return 0;
  47. }
  48. internal string GetCategoriesSelectedCount()
  49. {
  50. string returnValue;
  51. int selectionCount = GetSelectedCategoriesAsList().Count;
  52. if (selectionCount == categories.Count)
  53. {
  54. returnValue = LocalizationManager.Instance.GetText("ALL");
  55. }
  56. else
  57. {
  58. returnValue = selectionCount.ToString();
  59. }
  60. return returnValue;
  61. }
  62. private List<CategorySelection> GetSelectedCategoriesAsList()
  63. {
  64. return ContentPanel.GetComponentsInChildren<CategorySelection>().Where(c => c.isSelected()).ToList();
  65. }
  66. public List<CategoryPanel.Category> GetSelectedCategories() {
  67. GetCategories();
  68. List<string> categoriesName = categories.Select(c1 => c1.name).ToList();
  69. List<string> selectedCategoyNames = GetSelectedCategoriesAsList().Select(c2 => c2.getCategoryName()).ToList();
  70. List<string> names = categoriesName.Intersect(selectedCategoyNames).ToList();
  71. return categories.Where(c => names.Contains(c.name)).ToList();
  72. }
  73. internal void ShowDialog()
  74. {
  75. gameObject.SetActive(true);
  76. }
  77. }