SelectCategoryScript.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. doneButton.onClick.AddListener(CloseDialogAction);
  18. closeButton.onClick.AddListener(CloseDialogAction);
  19. if (categories == null) {
  20. categories = new List<CategoryPanel.Category>();
  21. }
  22. categories = OnlineDatabase.Instance.GetCategories(categories, -1);
  23. foreach (CategoryPanel.Category cat in categories)
  24. {
  25. GameObject go = Instantiate(CategoryToSelectPrefab, Vector3.zero, Quaternion.identity);
  26. CategorySelection c = go.GetComponent<CategorySelection>();
  27. c.SetSelected(true);
  28. c.SetCategoryText(cat.name, cat.questionCount);
  29. c.transform.localScale = new Vector3(1,1,1);
  30. c.transform.SetParent(ContentPanel.transform, false);
  31. }
  32. }
  33. internal void CloseDialogAction() {
  34. SelectCategoryPanel.GetComponent<selectCategoriesPanelController>().UpdateCategoryText();
  35. gameObject.SetActive(false);
  36. }
  37. internal int GetSelectedCategoriesQuestionCount()
  38. {
  39. return 0;
  40. }
  41. internal string GetCategoriesSelectedCount()
  42. {
  43. string returnValue;
  44. int selectionCount = GetSelectedCategoriesAsList().Count;
  45. if (selectionCount == categories.Count)
  46. {
  47. returnValue = LocalizationManager.Instance.GetText("ALL");
  48. }
  49. else
  50. {
  51. returnValue = selectionCount.ToString();
  52. }
  53. return returnValue;
  54. }
  55. private List<CategorySelection> GetSelectedCategoriesAsList()
  56. {
  57. return ContentPanel.GetComponentsInChildren<CategorySelection>().Where(c => c.isSelected()).ToList();
  58. }
  59. public List<CategoryPanel.Category> GetSelectedCategories() {
  60. List<string> categoriesName = categories.Select(c1 => c1.name).ToList();
  61. List<string> selectedCategoyNames = GetSelectedCategoriesAsList().Select(c2 => c2.getCategoryName()).ToList();
  62. List<string> names = categoriesName.Intersect(selectedCategoyNames).ToList();
  63. return categories.Where(c => names.Contains(c.name)).ToList();
  64. }
  65. internal void ShowDialog()
  66. {
  67. gameObject.SetActive(true);
  68. }
  69. }