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

80 lines
2.6 KiB
C#
Raw Normal View History

using System;
2025-02-11 22:58:56 -05:00
using System.Collections;
using System.Collections.Generic;
2025-02-12 18:46:46 -05:00
using Ichni.RhythmGame;
2025-02-11 22:58:56 -05:00
using TMPro;
using UnityEngine;
using UnityEngine.Events;
2025-02-11 22:58:56 -05:00
namespace Ichni.Editor
{
public class DynamicUIInputField : DynamicUIElement, IHaveAutoUpdate
2025-02-11 22:58:56 -05:00
{
public TMP_InputField inputField;
public bool isAutoUpdate { get; set; }
public bool isReceiving { get; set; }
2025-02-12 18:46:46 -05:00
public override void Initialize(IBaseElement baseElement, string title, string parameterName)
2025-02-11 22:58:56 -05:00
{
2025-02-12 18:46:46 -05:00
base.Initialize(baseElement, title, parameterName);
if (parameterName != string.Empty)
{
ApplyContent();
inputField.onEndEdit.AddListener(ApplyParameters);
}
2025-02-11 22:58:56 -05:00
}
private void Update()
{
(this as IHaveAutoUpdate).UpdateContent();
}
public void SetDefaultValue(string text)
{
inputField.text = text;
}
public T GetValue<T>()
{
return (T)Convert.ChangeType(inputField.text, typeof(T));
}
public void SetAutoUpdate(bool enable)
{
isAutoUpdate = enable;
isReceiving = true;
inputField.onSelect.AddListener(_ => isReceiving = false);
inputField.onDeselect.AddListener(_ => isReceiving = true);
}
public void ApplyContent()
{
inputField.text = connectedBaseElement.GetType().GetField(parameterName).GetValue(connectedBaseElement).ToString(); //获取对应变量的值
}
2025-02-11 22:58:56 -05:00
private void ApplyParameters(string text)
{
Type type = connectedBaseElement.GetType().GetField(parameterName).FieldType;
if (type == typeof(int))
{
connectedBaseElement.GetType().GetField(parameterName).SetValue(connectedBaseElement, int.Parse(text));
}
else if (type == typeof(float))
{
connectedBaseElement.GetType().GetField(parameterName).SetValue(connectedBaseElement, float.Parse(text));
}
else if (type == typeof(string))
{
connectedBaseElement.GetType().GetField(parameterName).SetValue(connectedBaseElement, text);
}
2025-02-12 18:46:46 -05:00
connectedBaseElement.Refresh();
2025-02-11 22:58:56 -05:00
}
2025-04-14 17:49:47 -04:00
public override DynamicUIElement AddListenerFunction(UnityAction action)
{
2025-03-20 02:42:10 -04:00
inputField.onEndEdit.AddListener(_ => action());
2025-04-14 17:49:47 -04:00
return this;
}
2025-02-11 22:58:56 -05:00
}
}