Passion & UI
This commit is contained in:
64
Assets/Scripts/Settings/UI/SettingsEntryDropdown.cs
Normal file
64
Assets/Scripts/Settings/UI/SettingsEntryDropdown.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cielonos.Settings.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// enum 字段的设置条目 — 使用 TMP_Dropdown。
|
||||
/// 自动从枚举类型生成选项列表。
|
||||
/// </summary>
|
||||
public class SettingsEntryDropdown : SettingsEntryBase
|
||||
{
|
||||
[SerializeField] private TMP_Dropdown dropdown;
|
||||
|
||||
private Array enumValues;
|
||||
|
||||
protected override void SetupUI()
|
||||
{
|
||||
if (dropdown == null) return;
|
||||
|
||||
enumValues = Enum.GetValues(fieldInfo.FieldType);
|
||||
|
||||
dropdown.ClearOptions();
|
||||
var options = new List<string>(enumValues.Length);
|
||||
foreach (object val in enumValues)
|
||||
{
|
||||
options.Add(val.ToString());
|
||||
}
|
||||
dropdown.AddOptions(options);
|
||||
|
||||
dropdown.onValueChanged.AddListener(OnDropdownChanged);
|
||||
}
|
||||
|
||||
public override void RefreshValue()
|
||||
{
|
||||
if (dropdown == null || enumValues == null) return;
|
||||
|
||||
object currentValue = GetFieldValue();
|
||||
for (int i = 0; i < enumValues.Length; i++)
|
||||
{
|
||||
if (enumValues.GetValue(i).Equals(currentValue))
|
||||
{
|
||||
dropdown.SetValueWithoutNotify(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDropdownChanged(int index)
|
||||
{
|
||||
if (enumValues != null && index >= 0 && index < enumValues.Length)
|
||||
{
|
||||
SetFieldValue(enumValues.GetValue(index));
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (dropdown != null)
|
||||
dropdown.onValueChanged.RemoveListener(OnDropdownChanged);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user