设置相关功能脚本呢
This commit is contained in:
85
Assets/Scripts/Common/Services/Settings/Base/MultiOption.cs
Normal file
85
Assets/Scripts/Common/Services/Settings/Base/MultiOption.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NBF.Setting
|
||||
{
|
||||
public interface IMultiOption : IOptionBase
|
||||
{
|
||||
List<string> GetOptionNames();
|
||||
int GetIndex();
|
||||
void SetIndex(int index);
|
||||
string GetName();
|
||||
}
|
||||
|
||||
public abstract class MultiOption<T> : OptionBase, IMultiOption
|
||||
{
|
||||
private int _index;
|
||||
|
||||
protected OptionTable<T> OptionTable = new OptionTable<T>();
|
||||
|
||||
/// <summary>
|
||||
/// 默认值
|
||||
/// </summary>
|
||||
protected abstract T DefaultValue { get; }
|
||||
|
||||
protected override void OnLoad()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected void AddOption(string name, T value)
|
||||
{
|
||||
OptionTable.Add(name, value);
|
||||
}
|
||||
|
||||
public List<string> GetOptionNames() => OptionTable.GetNames();
|
||||
|
||||
public string GetName()
|
||||
{
|
||||
return OptionTable.GetName(_index);
|
||||
}
|
||||
|
||||
public int GetIndex()
|
||||
{
|
||||
return _index;
|
||||
}
|
||||
|
||||
public void SetIndex(int index)
|
||||
{
|
||||
_index = index;
|
||||
}
|
||||
|
||||
protected void SelectOption(T value, int defaultIndex = 0)
|
||||
{
|
||||
SetIndex(TryGetIndex(value, out var index) ? index : defaultIndex);
|
||||
}
|
||||
|
||||
protected void SelectOption(Predicate<T> predicate, int defaultIndex = 0)
|
||||
{
|
||||
SetIndex(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(_index);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5b25650e359243468131db1f9b951ae7
|
||||
timeCreated: 1748571045
|
||||
49
Assets/Scripts/Common/Services/Settings/Base/OptionBase.cs
Normal file
49
Assets/Scripts/Common/Services/Settings/Base/OptionBase.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
namespace NBF.Setting
|
||||
{
|
||||
public interface IOptionBase
|
||||
{
|
||||
string Name { get; }
|
||||
void Initialize();
|
||||
void Apply();
|
||||
}
|
||||
|
||||
public abstract class OptionBase : IOptionBase
|
||||
{
|
||||
protected string SaveKey => $"Setting_{Group}_{Name}";
|
||||
|
||||
public abstract string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 所在组
|
||||
/// </summary>
|
||||
public abstract string Group { get; }
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
OnInitialize();
|
||||
Load();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载用户的设置
|
||||
/// </summary>
|
||||
public void Load()
|
||||
{
|
||||
OnLoad();
|
||||
}
|
||||
|
||||
public void Apply()
|
||||
{
|
||||
OnApply();
|
||||
}
|
||||
|
||||
protected abstract void OnInitialize();
|
||||
|
||||
|
||||
protected abstract void OnApply();
|
||||
|
||||
protected abstract void OnReset();
|
||||
|
||||
protected abstract void OnLoad();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dd2d6f346b1145debf370bc3ac71bd7e
|
||||
timeCreated: 1748570612
|
||||
34
Assets/Scripts/Common/Services/Settings/Base/OptionTable.cs
Normal file
34
Assets/Scripts/Common/Services/Settings/Base/OptionTable.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
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) => entries[index].Value;
|
||||
|
||||
public string GetName(int index) => entries != null ? entries[index].Name : string.Empty;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a7241b5524db446eb4f73ae3a8d7b0ec
|
||||
timeCreated: 1748571519
|
||||
55
Assets/Scripts/Common/Services/Settings/Base/RangeOption.cs
Normal file
55
Assets/Scripts/Common/Services/Settings/Base/RangeOption.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
namespace NBF.Setting
|
||||
{
|
||||
/// <summary>
|
||||
/// 范围设置
|
||||
/// </summary>
|
||||
public abstract class RangeOption : OptionBase
|
||||
{
|
||||
private int _value;
|
||||
public abstract int MinValue { get; }
|
||||
public abstract int MaxValue { get; }
|
||||
|
||||
public int GetValue()
|
||||
{
|
||||
return _value;
|
||||
}
|
||||
|
||||
public void SetValue(int value)
|
||||
{
|
||||
if (value > MaxValue) value = MaxValue;
|
||||
else if (value < MinValue) value = MinValue;
|
||||
_value = value;
|
||||
}
|
||||
|
||||
protected override void OnLoad()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 范围设置
|
||||
/// </summary>
|
||||
public abstract class RangeOptionFloat : OptionBase
|
||||
{
|
||||
private float _value;
|
||||
public abstract float MinValue { get; }
|
||||
public abstract float MaxValue { get; }
|
||||
|
||||
public float GetValue()
|
||||
{
|
||||
return _value;
|
||||
}
|
||||
|
||||
public void SetValue(float value)
|
||||
{
|
||||
if (value > MaxValue) value = MaxValue;
|
||||
else if (value < MinValue) value = MinValue;
|
||||
_value = value;
|
||||
}
|
||||
|
||||
protected override void OnLoad()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3531567c76fc48d5939644966fe22057
|
||||
timeCreated: 1748572844
|
||||
15
Assets/Scripts/Common/Services/Settings/Base/ToggleOption.cs
Normal file
15
Assets/Scripts/Common/Services/Settings/Base/ToggleOption.cs
Normal file
@@ -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);
|
||||
}
|
||||
|
||||
public bool IsEnabled() => GetSelectedOption();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dbcf52430d8b483497f040fe7cbcfa62
|
||||
timeCreated: 1748572278
|
||||
Reference in New Issue
Block a user