按键绑定和重置以及加载功能

This commit is contained in:
bob
2025-06-20 17:56:49 +08:00
parent 8a8821c2e6
commit ac946f41ed
27 changed files with 291 additions and 47 deletions

View File

@@ -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;
}
}
}

View File

@@ -64,12 +64,13 @@ namespace NBF
{
InputCursorExtension.InputInit();
DontDestroyOnLoad(gameObject);
PlayerInputControl = new PlayerInputControl();
PlayerInputControl.Enable();
}
private void Start()
{
PlayerInputControl = new PlayerInputControl();
PlayerInputControl.Enable();
AddEvent();
}

View File

@@ -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;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 49041e6a08b744038368d7010acf61ea
timeCreated: 1750408186

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}

View File

@@ -18,6 +18,9 @@ namespace NBF
[InputIcon("icon_controller_129", "icon_controller_1")]
public const string Enter = "Enter";
[InputIcon("icon_controller_129", "icon_controller_1")]
public const string Reset = "Reset";
[InputIcon("icon_controller_127", "icon_controller_19")]
public const string Tab = "Tab";

View File

@@ -83,5 +83,19 @@ namespace NBF
[InspectorName("订购市场")] ReserveBazaar = 22
}
public class ID
{
public const int Loading = 1;
public const int Home = 2;
public const int SettingPanel = 3;
public const int HelpPanel = 4;
public const int MapPanel = 5;
public const int ShopPanel = 10;
public const int MakePanel = 11;
public const int BagPanel = 12;
public const int FishBagPanel = 13;
}
}
}

View File

@@ -1106,6 +1106,15 @@ namespace NBF
""processors"": """",
""interactions"": ""Hold"",
""initialStateCheck"": false
},
{
""name"": ""Reset"",
""type"": ""Button"",
""id"": ""b8805b50-7544-4249-b3b8-f6554dfb7a93"",
""expectedControlType"": """",
""processors"": """",
""interactions"": """",
""initialStateCheck"": false
}
],
""bindings"": [
@@ -1438,6 +1447,17 @@ namespace NBF
""action"": ""Up"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""3069f356-c73e-4bac-88f5-88bd6d746bc2"",
""path"": ""<Keyboard>/r"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Reset"",
""isComposite"": false,
""isPartOfComposite"": false
}
]
}
@@ -1556,6 +1576,7 @@ namespace NBF
m_UI_Left = m_UI.FindAction("Left", throwIfNotFound: true);
m_UI_Down = m_UI.FindAction("Down", throwIfNotFound: true);
m_UI_Up = m_UI.FindAction("Up", throwIfNotFound: true);
m_UI_Reset = m_UI.FindAction("Reset", throwIfNotFound: true);
}
~@PlayerInputControl()
@@ -2129,6 +2150,7 @@ namespace NBF
private readonly InputAction m_UI_Left;
private readonly InputAction m_UI_Down;
private readonly InputAction m_UI_Up;
private readonly InputAction m_UI_Reset;
/// <summary>
/// Provides access to input actions defined in input action map "UI".
/// </summary>
@@ -2185,6 +2207,10 @@ namespace NBF
/// </summary>
public InputAction @Up => m_Wrapper.m_UI_Up;
/// <summary>
/// Provides access to the underlying input action "UI/Reset".
/// </summary>
public InputAction @Reset => m_Wrapper.m_UI_Reset;
/// <summary>
/// Provides access to the underlying input action map instance.
/// </summary>
public InputActionMap Get() { return m_Wrapper.m_UI; }
@@ -2243,6 +2269,9 @@ namespace NBF
@Up.started += instance.OnUp;
@Up.performed += instance.OnUp;
@Up.canceled += instance.OnUp;
@Reset.started += instance.OnReset;
@Reset.performed += instance.OnReset;
@Reset.canceled += instance.OnReset;
}
/// <summary>
@@ -2287,6 +2316,9 @@ namespace NBF
@Up.started -= instance.OnUp;
@Up.performed -= instance.OnUp;
@Up.canceled -= instance.OnUp;
@Reset.started -= instance.OnReset;
@Reset.performed -= instance.OnReset;
@Reset.canceled -= instance.OnReset;
}
/// <summary>
@@ -2729,6 +2761,13 @@ namespace NBF
/// <seealso cref="UnityEngine.InputSystem.InputAction.performed" />
/// <seealso cref="UnityEngine.InputSystem.InputAction.canceled" />
void OnUp(InputAction.CallbackContext context);
/// <summary>
/// Method invoked when associated input action "Reset" is either <see cref="UnityEngine.InputSystem.InputAction.started" />, <see cref="UnityEngine.InputSystem.InputAction.performed" /> or <see cref="UnityEngine.InputSystem.InputAction.canceled" />.
/// </summary>
/// <seealso cref="UnityEngine.InputSystem.InputAction.started" />
/// <seealso cref="UnityEngine.InputSystem.InputAction.performed" />
/// <seealso cref="UnityEngine.InputSystem.InputAction.canceled" />
void OnReset(InputAction.CallbackContext context);
}
}
}

View File

@@ -129,7 +129,7 @@ namespace NBC
_uiArray.Add(uiName, panel);
}
public IUIPanel GetUI<T>()
public T GetUI<T>() where T : class
{
IUIPanel wind = null;
Type type = typeof(T);
@@ -141,7 +141,7 @@ namespace NBC
break;
}
return wind;
return wind as T;
}
public IUIPanel GetUI(Type type)

View File

@@ -19,12 +19,10 @@ namespace NBF
public GImage box;
[AutoFind(Name = "TextTitle")]
public GTextField TextTitle;
[AutoFind(Name = "TextContent")]
public GTextField TextContent;
[AutoFind(Name = "BtnConfirm")]
public BtnTitleInputControl BtnConfirm;
[AutoFind(Name = "BtnCancel")]
public BtnTitleInputControl BtnCancel;
[AutoFind(Name = "BtnConfirm")]
public BtnTitleInputControl BtnConfirm;
public override string[] GetDependPackages(){ return new string[] {}; }

View File

@@ -59,10 +59,10 @@ namespace NBF
protected override void OnShow()
{
TextTitle.SetLanguage(_title);
// TextTitle.SetLanguage(_title);
BtnConfirm.SetLanguage(_confirmText);
BtnCancel.SetLanguage(_cancelText);
TextContent.SetLanguage(_content);
TextTitle.SetLanguage(_content);
MessageStyle.selectedIndex = _style;
}

View File

@@ -1,10 +1,8 @@
// 本脚本只在不存在时会生成一次。组件逻辑写在当前脚本内。已存在不会再次生成覆盖
using UnityEngine;
using FairyGUI;
using NBC;
using NBF.Setting;
using UnityEngine.InputSystem;
namespace NBF
{
@@ -20,7 +18,7 @@ namespace NBF
if (option is KeyBoardOption)
{
TextKeyboard.visible = true;
TextKeyboard.text = option.GetDisplayString();
TextKeyboard.text = option.GetDisplayString(); // + "- " + string.Join(",", list);
}
}
}

View File

@@ -1,36 +1,80 @@
// 本脚本只在不存在时会生成一次。组件逻辑写在当前脚本内。已存在不会再次生成覆盖
using System.Globalization;
using UnityEngine;
using FairyGUI;
using NBC;
using NBF.Setting;
using UnityEngine.InputSystem;
namespace NBF
{
public partial class SettingInputItem : GButton
{
public OptionBase Option;
public InputOption Option;
private InputActionRebindingExtensions.RebindingOperation rebindingOperation;
private void OnInited()
{
BtnKeyboard.onClick.Set(OnClickRebind);
onChanged.Add(SelfChanged);
}
public void SetData(OptionBase option)
public void SetData(InputOption option)
{
Option = option;
TextName.SetLanguage(Option.Name);
SetShow();
UpdateValueText();
}
private void SetShow()
public void OnClickRebind()
{
// InputWaitingPanel.Show(true);
// UI.Inst.OpenUI<InputWaitingPanel>();
// 取消当前绑定
var settingPanel = UI.Inst.GetUI<SettingPanel>();
settingPanel.InputWait.visible = true;
Option.InputAction.Disable();
// 开始重绑定操作
rebindingOperation = Option.InputAction.PerformInteractiveRebinding(Option.BindingIndex)
.OnMatchWaitForAnother(0.1f)
.OnComplete(operation => RebindComplete())
.OnCancel(operation => RebindCanceled())
.Start();
}
private void RebindComplete()
{
rebindingOperation.Dispose();
UpdateValueText();
Option.InputAction.Enable();
var settingPanel = UI.Inst.GetUI<SettingPanel>();
settingPanel.InputWait.visible = false;
// UI.Inst.HideUI<InputWaitingPanel>();
// startRebindObject.SetActive(true);
// waitingForInputObject.SetActive(false);
}
private void RebindCanceled()
{
rebindingOperation.Dispose();
Option.InputAction.Enable();
// UI.Inst.HideUI<InputWaitingPanel>();
}
private void SelfChanged(EventContext context)
{
if (!selected)
{
// RebindOver();
}
}
private void UpdateValueText()
{
BtnKeyboard.SetData(Option as InputOption);
BtnKeyboard.SetData(Option);
}
}
}

View File

@@ -23,6 +23,8 @@ namespace NBF
public GImage BottomLine;
[AutoFind(Name = "Mask")]
public GLabel Mask;
[AutoFind(Name = "InputWait")]
public GComponent InputWait;
public override string[] GetDependPackages(){ return new string[] {"Common"}; }

View File

@@ -10,6 +10,7 @@ using UIPanel = NBC.UIPanel;
namespace NBF
{
[UIBind(UIDef.ID.SettingPanel)]
public partial class SettingPanel : UIPanel
{
public override string UIPackName => "Main";
@@ -64,7 +65,7 @@ namespace NBF
{
// TextTitle.text = Lan.Get(_currentGroup);
_nowSelectIndex = -1;
if (string.IsNullOrEmpty(_currentTab)) return;
var options = Settings.Instance.GetOptionsByTab(_currentTab);
Dictionary<string, List<OptionBase>> groupOptions = new Dictionary<string, List<OptionBase>>();
@@ -82,8 +83,8 @@ namespace NBF
_canSelectIndex.Clear();
List.RemoveChildrenToPool();
var url = UIPackage.GetItemURL(UIPackName, "SettingSubTitleItem");
foreach (var key in groupOptions.Keys)
{
@@ -99,7 +100,7 @@ namespace NBF
{
if (List.AddItemFromPool(SettingInputItem.URL) is SettingInputItem item)
{
item.SetData(option);
item.SetData(option as InputOption);
var index = List.GetChildIndex(item);
_canSelectIndex.Add(index);
}
@@ -125,7 +126,7 @@ namespace NBF
private void OnApplySettings()
{
var options = Settings.Instance.GetOptionsByTab(_currentTab);
Log.Info("OnApplySettings");
foreach (var option in options)
{
option.Apply();
@@ -140,6 +141,8 @@ namespace NBF
{
option.Reset();
}
ResetSettingList();
}
private void OnUICanceled(string action)
@@ -174,6 +177,13 @@ namespace NBF
{
OnApplySettings();
}
else if (action == InputDef.UI.Reset)
{
MessageBox.Show("是否重置为默认?", (ret) =>
{
if (ret) OnResetSettings();
});
}
}