49 lines
1.3 KiB
C#
49 lines
1.3 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
public class TMPDropdownWithCallbacks : MonoBehaviour
|
|
{
|
|
private TMP_Dropdown dropdown;
|
|
[SerializeField] List<CustomTmpDropdownOption> listOptions;
|
|
private bool isResetting = false;
|
|
|
|
void Start()
|
|
{
|
|
dropdown = GetComponent<TMP_Dropdown>();
|
|
dropdown.ClearOptions();
|
|
//dropdown.onValueChanged.RemoveAllListeners();
|
|
dropdown.onValueChanged.AddListener(HandleDropdownValueChanged);
|
|
|
|
for (int i = 0; i < listOptions.Count; i++)
|
|
{
|
|
CustomTmpDropdownOption option = listOptions[i];
|
|
dropdown.options.Add(new TMP_Dropdown.OptionData(option.optionName));
|
|
}
|
|
}
|
|
public void HandleDropdownValueChanged(int index)
|
|
{
|
|
if (isResetting) return;
|
|
if (index >= listOptions.Count)
|
|
return;
|
|
listOptions[index].callback?.Invoke();
|
|
StartCoroutine(ResetDropdownSelection(index));
|
|
}
|
|
private IEnumerator ResetDropdownSelection(int index)
|
|
{
|
|
isResetting = true;
|
|
yield return null;
|
|
dropdown.value = 0;
|
|
isResetting = false;
|
|
yield return null;
|
|
}
|
|
}
|
|
[Serializable]
|
|
public class CustomTmpDropdownOption
|
|
{
|
|
public string optionName;
|
|
public UnityEvent callback;
|
|
} |