using System.Collections; using System.Collections.Generic; using UnityEngine; using…
- User
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; using System.Text.RegularExpressions;
public class TimeInputField : MonoBehaviour { [SerializeField] private TMP_InputField inputField;
private int hours; private int minutes; private string newValue; private void Start() { inputField.onValueChanged.AddListener(OnValueChanged); } private void OnValueChanged(string value) { // Check if the input contains only digits if (!Regex.IsMatch(value, @"^\d *$")) { // Remove any non-digit characters from the input value = Regex.Replace(value, @"\D", ""); newValue = value; //return; } else { newValue = value; } // Check the input length for the first two characters if (newValue.Length == 2) { // Try parsing the hours if (int.TryParse(newValue, out hours)) { // Check if hours is not greater than 24 if (hours > 24) { hours = 24; } // Update the input field with the formatted hours inputField.text = string.Format("{0:D2} (ч) :", hours); return; } } // Check the input length for the next two characters if (newValue.Length == 4) { // Extract minutes string minutesStr = newValue.Substring(2, 2); // Try parsing the minutes if (int.TryParse(minutesStr, out minutes)) { // Check if minutes is greater than 60 if (minutes > 60) { // Calculate the exceeding minutes and add them to hours int exceedingMinutes = minutes - 60; int existingHours = 0; // Extract existing hours from the input field if (inputField.text.Length >= 9) { string existingHoursStr = inputField.text.Substring(0, 2); int.TryParse(existingHoursStr, out existingHours); } // Add the exceeding minutes to the existing hours int newHours = existingHours + 1; int newMinutes = exceedingMinutes; // Check if new hours is not greater than 24 if (newHours > 24) { newHours = 24; } // Apply the desired format string formattedValue = string.Format("{0:D2} (ч) : {1:D2} (мин)", newHours, newMinutes); inputField.text = formattedValue; return; } else { // Apply the desired format string formattedValue = string.Format("{0} : {1:D2} (мин)", inputField.text, minutes); inputField.text = formattedValue; return; } } } }} в этом коде баг что при вводе минут значения дублируются "02 (ч) :01 (мин) : 01 (мин)" можно ли как-то избежать этого дублирования?
- Assistant
- User
значение не обновилось и теперь выглядит так 02 (ч) :03 : 03 (мин)
- Assistant
- User
получилась такая проблема 01 (ч) :03 : 03 (мин) а должно быть так 01 (ч) : 03 (мин)
- Assistant
- User
проблема осталась 02 (ч) :04 : 04 (мин)
- Assistant
- User
может нам просто удалить значение после : в этом блоке и записать новое? else { // Apply the desired format string formattedValue = string.Format("{0} : {1:D2} (мин)", inputField.text, minutes); inputField.text = formattedValue; return; }
- Assistant
Да, вы правы, можно просто удалить значение после символа ":" и записать новое значение вместо него. Вот исправленный код:
else { // Remove the existing minutes after the colon inputField.text = inputField.text.Substring(0, inputField.text.IndexOf(":") + 2); // Apply the desired format with the new minutes string formattedValue = string.Format("{0} {1:D2} (мин)", inputField.text, minutes); inputField.text = formattedValue; return; }Теперь значение должно быть обновлено правильно без дублирования минут. Извините за предыдущие ошибки и благодарю за ваше терпение.
- User
зачем в этом блоке мы передаём string? if (formattedTime.Length == HoursLength) { hours = ValidateAndClampValue(formattedTime, MaxHours); UpdateInputFieldText(hours, "");
return; } - Assistant
Похоже, что в представленном фрагменте кода явно отсутствуют некоторые детали. Н
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.