103 lines
3.2 KiB
C#
103 lines
3.2 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
public class NumberInput : MonoBehaviour
|
||
{
|
||
[Header("Ключ prefs")]
|
||
[SerializeField] private string key;
|
||
[SerializeField] private bool loadData;
|
||
[SerializeField] private bool saveDataOnClose = true;
|
||
|
||
[Header("Звуки")]
|
||
[SerializeField] private AudioClip switchSlotSound;
|
||
[SerializeField] private AudioClip switchLetterSound;
|
||
[SerializeField] private AudioClip acceptSound;
|
||
[SerializeField] private AudioClip openSound;
|
||
|
||
[Header("Скорость переключения")]
|
||
[SerializeField] private float MaxSpeedLetterPerSecond = 8;
|
||
[SerializeField] private float MinSpeedLetterPerSecond = 3;
|
||
[SerializeField] private float MaxSpeedTime = 3;
|
||
[SerializeField] private float MinSpeedTime = 0.5f;
|
||
|
||
private float switchButtonPressedTime = 0;
|
||
private float letterSwitchSpeed = 0;
|
||
private float switchDelayRest = 1;
|
||
|
||
//[SerializeField] private int defaultValue = 1;
|
||
[SerializeField] private int minValue = 1;
|
||
[SerializeField] private int maxValue = 999;
|
||
[SerializeField] private int currentValue;
|
||
|
||
[SerializeField] TextMeshProUGUI valueLabel;
|
||
|
||
public void Initialize()
|
||
{
|
||
LoadSavedData();
|
||
SoundManager.instance.playClip(openSound);
|
||
|
||
}
|
||
|
||
public int AcceptAndClose()
|
||
{
|
||
if (!string.IsNullOrEmpty(key) && saveDataOnClose)
|
||
{
|
||
PlayerPrefs.SetInt(key, currentValue);
|
||
SettingsManager.OnSettingsChanged?.Invoke();
|
||
}
|
||
SoundManager.instance.playClip(acceptSound);
|
||
return currentValue;
|
||
|
||
}
|
||
private void LoadSavedData()
|
||
{
|
||
currentValue = 1;
|
||
if (loadData)
|
||
currentValue = PlayerPrefs.GetInt(key, 1);
|
||
|
||
// Заполняем сохраненными данными
|
||
valueLabel.text = currentValue.ToString();
|
||
}
|
||
|
||
public void SwitchValue(int continiousDirection)
|
||
{
|
||
continiousDirection = Mathf.Clamp(continiousDirection, -1, 1);
|
||
if (continiousDirection == 0)//сброс переключения
|
||
{
|
||
switchDelayRest = 0;
|
||
switchButtonPressedTime = 0;
|
||
return;
|
||
}
|
||
|
||
if (switchDelayRest < 0)
|
||
{
|
||
//переключение
|
||
currentValue = Math.Clamp(currentValue + continiousDirection, minValue, maxValue);
|
||
valueLabel.text = currentValue.ToString();
|
||
|
||
recalculateRest();
|
||
}
|
||
switchDelayRest -= Time.deltaTime;
|
||
switchButtonPressedTime += Time.deltaTime;
|
||
|
||
|
||
void recalculateRest()
|
||
{
|
||
float k = (MaxSpeedLetterPerSecond - MinSpeedLetterPerSecond)
|
||
/ (MaxSpeedTime - MinSpeedTime);
|
||
float b = MinSpeedLetterPerSecond - k * MinSpeedTime;
|
||
letterSwitchSpeed = k * switchButtonPressedTime + b;
|
||
letterSwitchSpeed = Mathf.Clamp(letterSwitchSpeed,
|
||
MinSpeedLetterPerSecond,
|
||
MaxSpeedLetterPerSecond);
|
||
switchDelayRest = 1 / letterSwitchSpeed;
|
||
}
|
||
}
|
||
|
||
|
||
}
|