Files
Studio/Assets/NewStudioPGD/Scripts/UI/Element/ProjectTemplateDropdown.cs

109 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using XRLib.UI;
namespace Studio.UI
{
public class ProjectTemplateDropdown : UIBase
{
private GameObject optionItemPrefab;
private RectTransform TemplateDropdown;
private RectTransform Content;
public TMP_InputField InputField_ProjectTemplate;
private List<string> options = new();
private List<GameObject> activeOptionItems = new List<GameObject>();
public enum EType
{
Test1,
Test2,
Test3,
Test4,
Test5,
Test6,
Test7,
}
public override void AfterAwake()
{
optionItemPrefab = Resources.Load<GameObject>("Prefabs/UI/PRF_QuickStartItem");
InputField_ProjectTemplate.onValueChanged.AddListener(FilteringOption);
InputField_ProjectTemplate.onSelect.AddListener(ShowDropdown);
HideDropdown();
}
private void Start()
{
CreateDropdown();
}
private void ShowDropdown(string input)
{
TemplateDropdown.gameObject.SetActive(true);
}
private void HideDropdown()
{
TemplateDropdown.gameObject.SetActive(false);
}
private void CreateDropdown()
{
ClearDropdownItems();
options = Enum.GetNames(typeof(EType)).ToList();
foreach (string option in options)
{
CreateOption(option);
}
}
private void CreateOption(string text)
{
UI_QuickStartItem item = Instantiate(optionItemPrefab, Content).GetComponent<UI_QuickStartItem>();
item.Init(text, () => OnClickOptionItem(text));
activeOptionItems.Add(item.gameObject);
}
private void OnClickOptionItem(string text)
{
InputField_ProjectTemplate.text = text;
HideDropdown();
EventSystem.current.SetSelectedGameObject(null);
}
private void FilteringOption(string input)
{
ClearDropdownItems();
foreach (string option in options)
{
if (option.ToLower().Contains(input.ToLower()))
{
CreateOption(option);
}
}
TemplateDropdown.gameObject.SetActive(true);
}
private void ClearDropdownItems()
{
foreach (GameObject item in activeOptionItems)
{
Destroy(item);
}
activeOptionItems.Clear();
}
}
}