调整目录结构
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
namespace NBF.Setting
|
||||
{
|
||||
public abstract class ControllerOption : OptionBase
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 75e11d5298cd48718d39a3e2af82472f
|
||||
timeCreated: 1749634909
|
||||
@@ -0,0 +1,18 @@
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace NBF.Setting
|
||||
{
|
||||
public abstract class GamepadOption : InputOption
|
||||
{
|
||||
protected override bool IsBindingContains(InputBinding binding)
|
||||
{
|
||||
var path = binding.path;
|
||||
if (path.Contains("<Gamepad>"))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 49041e6a08b744038368d7010acf61ea
|
||||
timeCreated: 1750408186
|
||||
@@ -0,0 +1,12 @@
|
||||
using System.Collections.Generic;
|
||||
using NBF.Setting;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public interface ISettings
|
||||
{
|
||||
List<OptionBase> GetOptionsByTab(string group);
|
||||
IEnumerable<string> GetAllTabs();
|
||||
T GetSettingOption<T>() where T : OptionBase;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d2cb28b8c7664cc28fe0b0a0a4516704
|
||||
timeCreated: 1748678126
|
||||
@@ -0,0 +1,96 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace NBF.Setting
|
||||
{
|
||||
public abstract class InputOption : OptionBase
|
||||
{
|
||||
private int _bindingIndex = 0;
|
||||
public abstract InputAction InputAction { get; }
|
||||
|
||||
public int BindingIndex => _bindingIndex;
|
||||
|
||||
protected override int DefaultValue => 0;
|
||||
|
||||
/// <summary>
|
||||
/// 保存的值
|
||||
/// </summary>
|
||||
protected string InputSaveValue;
|
||||
|
||||
public override bool HaveNotApple()
|
||||
{
|
||||
return !InputAction.SaveBindingOverridesAsJson().Equals(InputSaveValue);
|
||||
}
|
||||
|
||||
public override void Apply()
|
||||
{
|
||||
// 保存绑定
|
||||
InputSaveValue = InputAction.SaveBindingOverridesAsJson();
|
||||
PlayerPrefs.SetString(SaveKey, InputSaveValue);
|
||||
OnApply();
|
||||
}
|
||||
|
||||
public override void Load()
|
||||
{
|
||||
for (int i = 0; i < InputAction.bindings.Count; i++)
|
||||
{
|
||||
var binding = InputAction.bindings[i];
|
||||
if (IsBindingContains(binding))
|
||||
{
|
||||
_bindingIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var value = PlayerPrefs.GetString(SaveKey, string.Empty);
|
||||
InputSaveValue = value;
|
||||
if (!string.IsNullOrEmpty(InputSaveValue))
|
||||
{
|
||||
InputAction.LoadBindingOverridesFromJson(InputSaveValue);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
if (InputAction.bindings[BindingIndex].isComposite)
|
||||
{
|
||||
// It's a composite. Remove overrides from part bindings.
|
||||
for (var i = BindingIndex + 1;
|
||||
i < InputAction.bindings.Count && InputAction.bindings[i].isPartOfComposite;
|
||||
++i)
|
||||
InputAction.RemoveBindingOverride(i);
|
||||
}
|
||||
else
|
||||
{
|
||||
InputAction.RemoveBindingOverride(BindingIndex);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Cancel()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(InputSaveValue))
|
||||
{
|
||||
InputAction.LoadBindingOverridesFromJson(InputSaveValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
}
|
||||
|
||||
public override string GetDisplayString()
|
||||
{
|
||||
if (InputAction != null)
|
||||
{
|
||||
return InputAction.GetBindingDisplayString(BindingIndex);
|
||||
}
|
||||
|
||||
return base.GetDisplayString();
|
||||
}
|
||||
|
||||
protected virtual bool IsBindingContains(InputBinding binding)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0df4bee5d9b54ada8be3757d7fe02e30
|
||||
timeCreated: 1749634814
|
||||
@@ -0,0 +1,18 @@
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace NBF.Setting
|
||||
{
|
||||
public abstract class KeyBoardOption : InputOption
|
||||
{
|
||||
protected override bool IsBindingContains(InputBinding binding)
|
||||
{
|
||||
var path = binding.path;
|
||||
if (path.Contains("<Keyboard>") || path.Contains("<Mouse>"))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 509d2fb0106b42b79854cca29707207d
|
||||
timeCreated: 1749634771
|
||||
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBF.Setting
|
||||
{
|
||||
public interface IMultiOption : IOptionBase
|
||||
{
|
||||
List<string> GetOptionNames();
|
||||
}
|
||||
|
||||
public abstract class MultiOption<T> : OptionBase, IMultiOption
|
||||
{
|
||||
protected OptionTable<T> OptionTable = new OptionTable<T>();
|
||||
|
||||
|
||||
protected void AddOption(string name, T value)
|
||||
{
|
||||
OptionTable.Add(name, value);
|
||||
}
|
||||
|
||||
public List<string> GetOptionNames() => OptionTable.GetNames();
|
||||
|
||||
|
||||
protected void SelectOption(T value, int defaultIndex = 0)
|
||||
{
|
||||
SetValue(TryGetIndex(value, out var index) ? index : defaultIndex);
|
||||
}
|
||||
|
||||
protected void SelectOption(Predicate<T> predicate, int defaultIndex = 0)
|
||||
{
|
||||
SetValue(TryGetIndex(predicate, out var index) ? index : defaultIndex);
|
||||
}
|
||||
|
||||
protected bool TryGetIndex(T option, out int index)
|
||||
{
|
||||
return TryGetIndex(entry => entry.Equals(option), out index);
|
||||
}
|
||||
|
||||
protected bool TryGetIndex(Predicate<T> predicate, out int index)
|
||||
{
|
||||
index = -1;
|
||||
|
||||
var entries = OptionTable.GetValues();
|
||||
|
||||
for (var i = 0; i < entries.Count; i++)
|
||||
{
|
||||
if (!predicate(entries[i])) continue;
|
||||
index = i;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public T GetSelectedOption() => OptionTable.GetValue(Value);
|
||||
|
||||
public override string GetDisplayString()
|
||||
{
|
||||
return OptionTable.GetName(Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5b25650e359243468131db1f9b951ae7
|
||||
timeCreated: 1748571045
|
||||
115
Assets/Scripts/Model/Common/Services/Settings/Base/OptionBase.cs
Normal file
115
Assets/Scripts/Model/Common/Services/Settings/Base/OptionBase.cs
Normal file
@@ -0,0 +1,115 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBF.Setting
|
||||
{
|
||||
public interface IOptionBase
|
||||
{
|
||||
string Name { get; }
|
||||
void Initialize(ISettings root);
|
||||
void Apply();
|
||||
int GetValue();
|
||||
void SetValue(int index);
|
||||
bool HaveNotApple();
|
||||
ISettings Root { get; }
|
||||
string GetDisplayString();
|
||||
}
|
||||
|
||||
public abstract class OptionBase : IOptionBase
|
||||
{
|
||||
protected string SaveKey => $"Setting_{Group}_{Name}";
|
||||
|
||||
public abstract string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 所在组
|
||||
/// </summary>
|
||||
public abstract string Group { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 所在分切页
|
||||
/// </summary>
|
||||
public abstract string Tab { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 默认值
|
||||
/// </summary>
|
||||
protected abstract int DefaultValue { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前的值
|
||||
/// </summary>
|
||||
protected int Value;
|
||||
|
||||
/// <summary>
|
||||
/// 保存的值
|
||||
/// </summary>
|
||||
protected int SaveValue;
|
||||
|
||||
public virtual bool HaveNotApple()
|
||||
{
|
||||
return !Value.Equals(SaveValue);
|
||||
}
|
||||
|
||||
public ISettings Root { get; private set; }
|
||||
|
||||
|
||||
public void Initialize(ISettings root)
|
||||
{
|
||||
Root = root;
|
||||
Load();
|
||||
OnInitialize();
|
||||
Apply();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载用户的设置
|
||||
/// </summary>
|
||||
public virtual void Load()
|
||||
{
|
||||
var value = PlayerPrefs.GetInt(SaveKey, DefaultValue);
|
||||
Value = value;
|
||||
SaveValue = value;
|
||||
}
|
||||
|
||||
public virtual void Apply()
|
||||
{
|
||||
PlayerPrefs.SetInt(SaveKey, Value);
|
||||
SaveValue = Value;
|
||||
OnApply();
|
||||
}
|
||||
|
||||
public virtual void Reset()
|
||||
{
|
||||
Value = DefaultValue;
|
||||
}
|
||||
|
||||
public virtual void Cancel()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
public virtual string GetDisplayString()
|
||||
{
|
||||
return GetValue().ToString();
|
||||
}
|
||||
|
||||
public virtual int GetValue()
|
||||
{
|
||||
return Value;
|
||||
}
|
||||
|
||||
public virtual void SetValue(int value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
|
||||
protected virtual void OnInitialize()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
protected abstract void OnApply();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dd2d6f346b1145debf370bc3ac71bd7e
|
||||
timeCreated: 1748570612
|
||||
@@ -0,0 +1,44 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace NBF.Setting
|
||||
{
|
||||
public class OptionTable<T>
|
||||
{
|
||||
public struct OptionEntry
|
||||
{
|
||||
public string Name;
|
||||
public T Value;
|
||||
|
||||
public OptionEntry(string name, T value)
|
||||
{
|
||||
this.Name = name;
|
||||
this.Value = value;
|
||||
}
|
||||
}
|
||||
|
||||
List<OptionEntry> entries = new List<OptionEntry>();
|
||||
|
||||
public void Add(string name, T value)
|
||||
{
|
||||
entries.Add(new OptionEntry(name, value));
|
||||
}
|
||||
|
||||
public List<string> GetNames() => entries.Select(x => x.Name).ToList();
|
||||
public List<T> GetValues() => entries.Select(x => x.Value).ToList();
|
||||
|
||||
public T GetValue(int index)
|
||||
{
|
||||
if (entries == null) return default;
|
||||
if (index < 0 || index >= entries.Count) return default;
|
||||
return entries[index].Value;
|
||||
}
|
||||
|
||||
public string GetName(int index)
|
||||
{
|
||||
if (entries == null) return string.Empty;
|
||||
if (index < 0 || index >= entries.Count) return string.Empty;
|
||||
return entries[index].Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a7241b5524db446eb4f73ae3a8d7b0ec
|
||||
timeCreated: 1748571519
|
||||
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace NBF.Setting
|
||||
{
|
||||
/// <summary>
|
||||
/// 范围设置
|
||||
/// </summary>
|
||||
public abstract class RangeOption : OptionBase
|
||||
{
|
||||
public abstract int MinValue { get; }
|
||||
public abstract int MaxValue { get; }
|
||||
|
||||
public virtual int ShowRate => 0;
|
||||
|
||||
public override void SetValue(int value)
|
||||
{
|
||||
if (value > MaxValue) value = MaxValue;
|
||||
else if (value < MinValue) value = MinValue;
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public override string GetDisplayString()
|
||||
{
|
||||
if (ShowRate > 0)
|
||||
{
|
||||
return Math.Round(GetValue() / (ShowRate * 1f), 1).ToString(CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
return base.GetDisplayString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3531567c76fc48d5939644966fe22057
|
||||
timeCreated: 1748572844
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace NBF.Setting
|
||||
{
|
||||
public abstract class ToggleOption : MultiOption<bool>
|
||||
{
|
||||
protected override void OnInitialize()
|
||||
{
|
||||
AddOption("Off", false);
|
||||
AddOption("On", true);
|
||||
|
||||
SelectOption(DefaultValue == 1);
|
||||
}
|
||||
|
||||
public bool IsEnabled() => GetSelectedOption();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dbcf52430d8b483497f040fe7cbcfa62
|
||||
timeCreated: 1748572278
|
||||
Reference in New Issue
Block a user