using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.Linq; public class SelectCategoryScript : MonoBehaviour { [SerializeField] Button doneButton; [SerializeField] Button closeButton; [SerializeField] GameObject CategoryToSelectPrefab; [SerializeField] GameObject SelectCategoryPanel; [SerializeField] GameObject ContentPanel; // Start is called before the first frame update private List categories; private void Start() { doneButton.onClick.AddListener(CloseDialogAction); closeButton.onClick.AddListener(CloseDialogAction); GetCategories(); int pos = 0; foreach (CategoryPanel.Category cat in categories) { GameObject go = Instantiate(CategoryToSelectPrefab, Vector3.zero, Quaternion.identity); CategorySelection c = go.GetComponent(); c.SetSelected(true); c.SetCategoryText(cat.name, cat.questionCount); c.transform.SetSiblingIndex(pos++); c.transform.localScale = new Vector3(1, 1, 1); c.transform.SetParent(ContentPanel.transform, false); } } private void GetCategories() { if (categories == null || categories.Count == 0) { categories = new List(); categories = OnlineDatabase.Instance.GetCategories(categories, -1); } } internal void CloseDialogAction() { SelectCategoryPanel.GetComponent().UpdateCategoryText(); gameObject.SetActive(false); } internal int GetSelectedCategoriesQuestionCount() { return 0; } internal string GetCategoriesSelectedCount() { string returnValue; int selectionCount = GetSelectedCategoriesAsList().Count; if (selectionCount == categories.Count) { returnValue = LocalizationManager.Instance.GetText("ALL"); } else { returnValue = selectionCount.ToString(); } return returnValue; } private List GetSelectedCategoriesAsList() { return ContentPanel.GetComponentsInChildren().Where(c => c.isSelected()).ToList(); } public List GetSelectedCategories() { GetCategories(); List categoriesName = categories.Select(c1 => c1.name).ToList(); List selectedCategoyNames = GetSelectedCategoriesAsList().Select(c2 => c2.getCategoryName()).ToList(); List names = categoriesName.Intersect(selectedCategoyNames).ToList(); return categories.Where(c => names.Contains(c.name)).ToList(); } internal void ShowDialog() { gameObject.SetActive(true); } }