Files
XericLibrary-Publish/ConfigUIElement/Scripts/Element/ConfigDropdown.cs
2025-04-10 15:32:00 +08:00

78 lines
2.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
using UnityEngine.UI;
namespace LRC
{
public class ConfigDropdown : ConfigSelectableItem
{
#region ÊôÐÔ×ÖÎ
public TMP_Dropdown targetDropdown;
#endregion
#region ÉúÃüÖÜÆÚ
protected override void Awake()
{
base.Awake();
targetDropdown.onValueChanged.AddListener(index =>
{
SetValue(index);
});
}
public void InitializationDropdown(IEnumerable<string> options)
{
targetDropdown.options = options
.Select(option => new TMP_Dropdown.OptionData(option)).ToList();
targetDropdown.RefreshShownValue();
if (IsValueInitNull)
RefreshValueWithoutEvent(0);
}
#endregion
#region ʵÏÖ
protected override void SetValue(object newValue)
{
base.SetValue(CastIntNumber(newValue));
}
public override void RefreshValueWithoutEvent(object newValue, bool forceSet = false)
{
base.RefreshValueWithoutEvent(CastIntNumber(newValue), forceSet);
}
protected override void Initialization_ChildConstruction(UIBehaviour component)
{
base.Initialization_ChildConstruction(component);
var name = component.transform.name;
if (name is not "dropdown") return;
if (component is TMP_Dropdown dropdown)
{
if (targetDropdown != null)
targetDropdown = dropdown;
}
}
protected override void RefreshFormatValue(object newValue)
{
base.RefreshFormatValue(newValue);
if (newValue == null)
return;
targetDropdown.value = CastIntNumber(newValue);
targetDropdown.RefreshShownValue();
}
#endregion
}
}