162 lines
4.5 KiB
C#
162 lines
4.5 KiB
C#
// 本脚本只在不存在时会生成一次。组件逻辑写在当前脚本内。已存在不会再次生成覆盖
|
|
|
|
using System;
|
|
using System.Globalization;
|
|
using FairyGUI;
|
|
using NBC;
|
|
using NBF.Setting;
|
|
using UnityEngine;
|
|
|
|
namespace NBF
|
|
{
|
|
public partial class SettingItem : GButton
|
|
{
|
|
public OptionBase Option;
|
|
|
|
private void OnInited()
|
|
{
|
|
BtnPrev.onClick.Set(OnPrev);
|
|
BtnNext.onClick.Set(OnNext);
|
|
Slider.onChanged.Set(OnSliderChanged);
|
|
}
|
|
|
|
public void SetData(OptionBase option)
|
|
{
|
|
Option = option;
|
|
|
|
if (option is IMultiOption multiOption)
|
|
{
|
|
style.selectedIndex = 0;
|
|
}
|
|
else if (option is RangeOption range)
|
|
{
|
|
style.selectedIndex = 1;
|
|
Slider.min = range.MinValue;
|
|
Slider.max = range.MaxValue;
|
|
Slider.wholeNumbers = true;
|
|
}
|
|
|
|
TextName.text = Lan.Get(Option.Name);
|
|
SetShow();
|
|
}
|
|
|
|
private void SetShow()
|
|
{
|
|
if (Option is IMultiOption multiOption)
|
|
{
|
|
TextInfo.text = multiOption.GetName();
|
|
}
|
|
else if (Option is RangeOption range)
|
|
{
|
|
Slider.value = range.GetValue();
|
|
}
|
|
|
|
UpdateValueText();
|
|
}
|
|
|
|
public void OnPrev()
|
|
{
|
|
if (Option is IMultiOption multiOption)
|
|
{
|
|
var index = multiOption.GetValue();
|
|
if (index > 0)
|
|
{
|
|
index--;
|
|
}
|
|
else if (index == 0)
|
|
{
|
|
var names = multiOption.GetOptionNames();
|
|
index = names.Count - 1;
|
|
}
|
|
|
|
if (Option is ResolutionSetting resolutionSetting)
|
|
{
|
|
var res = Screen.resolutions;
|
|
Debug.LogError($"next index={index} count = {multiOption.GetOptionNames().Count} res={res.Length}");
|
|
}
|
|
|
|
multiOption.SetValue(index);
|
|
}
|
|
else if (Option is RangeOption range)
|
|
{
|
|
var value = range.GetValue();
|
|
value--;
|
|
if (value < range.MinValue)
|
|
{
|
|
value = range.MinValue;
|
|
}
|
|
|
|
range.SetValue(value);
|
|
Slider.value = value;
|
|
UpdateValueText();
|
|
}
|
|
|
|
SetShow();
|
|
}
|
|
|
|
public void OnNext()
|
|
{
|
|
if (Option is IMultiOption multiOption)
|
|
{
|
|
var names = multiOption.GetOptionNames();
|
|
var index = multiOption.GetValue();
|
|
if (index < names.Count - 1)
|
|
{
|
|
index++;
|
|
}
|
|
else if (index == names.Count - 1)
|
|
{
|
|
index = 0;
|
|
}
|
|
|
|
if (Option is ResolutionSetting resolutionSetting)
|
|
{
|
|
var res = Screen.resolutions;
|
|
Debug.LogError($"next index={index} count = {names.Count} res={res.Length}");
|
|
}
|
|
|
|
multiOption.SetValue(index);
|
|
}
|
|
else if (Option is RangeOption range)
|
|
{
|
|
var value = range.GetValue();
|
|
value++;
|
|
if (value > range.MaxValue)
|
|
{
|
|
value = range.MaxValue;
|
|
}
|
|
|
|
range.SetValue(value);
|
|
Slider.value = value;
|
|
UpdateValueText();
|
|
}
|
|
|
|
|
|
SetShow();
|
|
}
|
|
|
|
public void OnSliderChanged()
|
|
{
|
|
if (Option is RangeOption range)
|
|
{
|
|
range.SetValue((int)Slider.value);
|
|
UpdateValueText();
|
|
}
|
|
// else if (Option is RangeOptionFloat rangeOptionFloat)
|
|
// {
|
|
// rangeOptionFloat.SetValue((float)Math.Round(Slider.value, 1));
|
|
// // Slider
|
|
// SliderTitle.text = Math.Round(rangeOptionFloat.GetValue(), 1).ToString(CultureInfo.InvariantCulture);
|
|
// Debug.LogError(Slider.value);
|
|
// }
|
|
}
|
|
|
|
private void UpdateValueText()
|
|
{
|
|
if (Option is RangeOption range)
|
|
{
|
|
TextSliderValue.text = Math.Round(range.GetValue() / 10f, 1).ToString(CultureInfo.InvariantCulture);
|
|
}
|
|
}
|
|
}
|
|
} |