using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; public class FloatView : MonoBehaviour, ISettingView { [Header("Layout ссылки")] [SerializeField] private Image Bar; [SerializeField] List PercentLabels; [Header("Звук")] [SerializeField] private bool UseSound; [SerializeField] private AudioClip SwitchClip; [SerializeField] private AudioSource Source; //Кэш значений настройки private float minValue; private float maxValue; private float value; private float delta; private float step; public SettingDTO CachedSetting { get; private set; } public void Initialize(SettingDTO setting) { CachedSetting = setting; CacheSettingValues(setting); Source.clip = SwitchClip; Display(); } public void TakeInput(int direction) { if (direction == 0) { Source.Stop(); return; } value += step * Mathf.Sign(direction); value = Mathf.Clamp(value, minValue, maxValue); CachedSetting.FloatValue = value; if (UseSound) PlaySound(value); Display(); } private void Display() { float percent = (value - minValue) / delta; Bar.fillAmount = percent; foreach (var label in PercentLabels) { label.text = $"{percent * 100:0}"; } } private void CacheSettingValues(SettingDTO setting) { minValue = setting.FloatMin; maxValue = setting.FloatMax; step = setting.FloatStep; value = setting.FloatValue; delta = maxValue - minValue; } private void PlaySound(float val) { Source.pitch = val * 1.5f + 0.5f; Source.volume = val; if (!Source.isPlaying) Source.Play(); } }