using System; using System.Collections; using System.Collections.Generic; using Ichni.RhythmGame; using TMPro; using UnityEngine; using UnityEngine.Events; namespace Ichni.Editor { public class DynamicUIInputField : DynamicUIElement, IHaveAutoUpdate { public TMP_InputField inputField; public bool isAutoUpdate { get; set; } public bool isReceiving { get; set; } public override void Initialize(IBaseElement baseElement, string title, string parameterName) { base.Initialize(baseElement, title, parameterName); if (parameterName != string.Empty) { ApplyContent(); inputField.onEndEdit.AddListener(ApplyParameters); } } private void Update() { (this as IHaveAutoUpdate).UpdateContent(); } public void SetDefaultValue(string text) { inputField.text = text; } public T GetValue() { 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(); //获取对应变量的值 } 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); } connectedBaseElement.Refresh(); } public void AddListenerFunction(UnityAction action) { inputField.onEndEdit.AddListener(action); } } }