95 lines
2.6 KiB
C#
95 lines
2.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class LightSettingPanel : BasePanel
|
|
{
|
|
public Slider BrightnessSlider;
|
|
private SettingsViewModel _vm;
|
|
public TextMeshProUGUI BrightnessText;
|
|
|
|
public Button HomeButton;
|
|
public Button ConfirmButton;
|
|
public Button BackButton;
|
|
|
|
public override void Init()
|
|
{
|
|
_vm = new SettingsViewModel();
|
|
InitializeUI();
|
|
}
|
|
|
|
private void InitializeUI()
|
|
{
|
|
if (BrightnessSlider != null)
|
|
{
|
|
BrightnessSlider.minValue = 10;
|
|
BrightnessSlider.maxValue = 100;
|
|
BrightnessSlider.wholeNumbers = true;
|
|
BrightnessSlider.value = Mathf.Clamp(_vm.Brightness, 10f, 100f);
|
|
|
|
BrightnessSlider.onValueChanged.AddListener(OnBrightnessChanged);
|
|
}
|
|
|
|
int initialBrightness = BrightnessSlider != null ? (int)BrightnessSlider.value : Mathf.Clamp((int)_vm.Brightness, 10, 100);
|
|
UpdateBrightnessText(initialBrightness);
|
|
Screen.brightness = initialBrightness / 100f;
|
|
|
|
if (HomeButton != null)
|
|
{
|
|
HomeButton.onClick.AddListener(() =>
|
|
{
|
|
ConfirmDialog.Show("确认", "是否返回主界面?", () =>
|
|
{
|
|
ReturnToHome();
|
|
});
|
|
|
|
});
|
|
}
|
|
if (ConfirmButton != null)
|
|
{
|
|
ConfirmButton.onClick.AddListener(ApplyAndBack);
|
|
}
|
|
if (BackButton != null)
|
|
{
|
|
BackButton.onClick.AddListener(() =>
|
|
ConfirmDialog.Show("确认", "是否返回设置界面?", () =>
|
|
{
|
|
ClosePanel();
|
|
ClosePanel();
|
|
})
|
|
);
|
|
}
|
|
}
|
|
|
|
private void OnBrightnessChanged(float value)
|
|
{
|
|
int brightnessPercent = Mathf.Clamp((int)value, 10, 100);
|
|
UpdateBrightnessText(brightnessPercent);
|
|
Screen.brightness = brightnessPercent / 100f;
|
|
}
|
|
|
|
private void UpdateBrightnessText(int brightnessPercent)
|
|
{
|
|
if (BrightnessText != null)
|
|
{
|
|
BrightnessText.text = $"{brightnessPercent}%";
|
|
}
|
|
}
|
|
|
|
private void ApplyAndBack()
|
|
{
|
|
if (BrightnessSlider != null)
|
|
{
|
|
float finalBrightness = Mathf.Clamp(BrightnessSlider.value, 10f, 100f);
|
|
_vm.Brightness = finalBrightness;
|
|
Screen.brightness = finalBrightness / 100f;
|
|
UpdateBrightnessText((int)finalBrightness);
|
|
Debug.Log($"LightSettingPanel: 应用亮度设置 {finalBrightness}%");
|
|
}
|
|
ClosePanel();
|
|
}
|
|
|
|
}
|