Files
ichni_Creator_Studio/Assets/Scripts/DynamicUI/DynamicUIElements/Simple/DynamicUIEnumDropdown.cs

44 lines
1.5 KiB
C#
Raw Normal View History

2025-02-12 18:46:46 -05:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Ichni.RhythmGame;
using TMPro;
using UnityEngine;
2025-02-17 14:46:14 -05:00
using UnityEngine.Events;
2025-02-12 18:46:46 -05:00
namespace Ichni.Editor
{
2025-02-17 14:46:14 -05:00
public class DynamicUIEnumDropdown : DynamicUIElement
2025-02-12 18:46:46 -05:00
{
public TMP_Dropdown dropdown;
2025-07-26 10:52:52 -04:00
public List<int> realValues;
2025-02-12 18:46:46 -05:00
public override void Initialize(IBaseElement baseElement, string title, string parameterName)
{
base.Initialize(baseElement, title, parameterName);
dropdown.value = (int)connectedBaseElement.GetType().GetField(parameterName).GetValue(connectedBaseElement); //获取对应变量的值
dropdown.onValueChanged.AddListener(ApplyParameters);
}
public void SetUpEnum(Type enumType)
{
2025-02-13 14:26:37 -05:00
dropdown.ClearOptions();
2025-02-12 18:46:46 -05:00
List<string> enumNameList = System.Enum.GetNames(enumType).ToList();
2025-07-26 10:52:52 -04:00
realValues = System.Enum.GetValues(enumType).Cast<int>().ToList();
2025-02-12 18:46:46 -05:00
dropdown.AddOptions(enumNameList);
}
private void ApplyParameters(int value)
{
2025-07-26 10:52:52 -04:00
connectedBaseElement.GetType().GetField(parameterName).SetValue(connectedBaseElement, realValues[value]);
2025-02-12 18:46:46 -05:00
connectedBaseElement.Refresh();
}
2025-04-19 23:21:27 +08:00
2025-04-14 17:49:47 -04:00
public override DynamicUIElement AddListenerFunction(UnityAction action)
2025-02-17 14:46:14 -05:00
{
2025-03-20 02:42:10 -04:00
dropdown.onValueChanged.AddListener(_ => action());
2025-04-14 17:49:47 -04:00
return this;
2025-02-17 14:46:14 -05:00
}
2025-02-12 18:46:46 -05:00
}
}