按键绑定和重置以及加载功能
This commit is contained in:
@@ -6,6 +6,17 @@ namespace NBF
|
||||
{
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class UIBindAttribute : BaseAttribute
|
||||
{
|
||||
public int Id;
|
||||
|
||||
public UIBindAttribute(int id)
|
||||
{
|
||||
this.Id = id;
|
||||
}
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Field)]
|
||||
public class InputIconAttribute : BaseAttribute
|
||||
{
|
||||
@@ -57,5 +68,4 @@ namespace NBF
|
||||
Sort = sort;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -64,12 +64,13 @@ namespace NBF
|
||||
{
|
||||
InputCursorExtension.InputInit();
|
||||
DontDestroyOnLoad(gameObject);
|
||||
PlayerInputControl = new PlayerInputControl();
|
||||
PlayerInputControl.Enable();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
PlayerInputControl = new PlayerInputControl();
|
||||
PlayerInputControl.Enable();
|
||||
|
||||
AddEvent();
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -1,21 +1,84 @@
|
||||
using UnityEngine.InputSystem;
|
||||
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()
|
||||
{
|
||||
// 保存绑定
|
||||
PlayerPrefs.SetString(SaveKey, InputAction.SaveBindingOverridesAsJson());
|
||||
SaveValue = Value;
|
||||
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 string GetDisplayString()
|
||||
{
|
||||
if (InputAction != null)
|
||||
{
|
||||
return InputAction.GetBindingDisplayString();
|
||||
return InputAction.GetBindingDisplayString(BindingIndex);
|
||||
}
|
||||
|
||||
return base.GetDisplayString();
|
||||
}
|
||||
|
||||
protected virtual bool IsBindingContains(InputBinding binding)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,15 @@ 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -45,7 +45,7 @@ namespace NBF.Setting
|
||||
/// </summary>
|
||||
protected int SaveValue;
|
||||
|
||||
public bool HaveNotApple()
|
||||
public virtual bool HaveNotApple()
|
||||
{
|
||||
return !Value.Equals(SaveValue);
|
||||
}
|
||||
@@ -64,21 +64,21 @@ namespace NBF.Setting
|
||||
/// <summary>
|
||||
/// 加载用户的设置
|
||||
/// </summary>
|
||||
public void Load()
|
||||
public virtual void Load()
|
||||
{
|
||||
var value = PlayerPrefs.GetInt(SaveKey, DefaultValue);
|
||||
Value = value;
|
||||
SaveValue = value;
|
||||
}
|
||||
|
||||
public void Apply()
|
||||
public virtual void Apply()
|
||||
{
|
||||
PlayerPrefs.SetInt(SaveKey, Value);
|
||||
SaveValue = Value;
|
||||
OnApply();
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
public virtual void Reset()
|
||||
{
|
||||
Value = DefaultValue;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user