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

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

@@ -1018,6 +1018,15 @@
"processors": "",
"interactions": "Hold",
"initialStateCheck": false
},
{
"name": "Reset",
"type": "Button",
"id": "b8805b50-7544-4249-b3b8-f6554dfb7a93",
"expectedControlType": "",
"processors": "",
"interactions": "",
"initialStateCheck": false
}
],
"bindings": [
@@ -1350,6 +1359,17 @@
"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
}
]
}

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();
});
}
}

View File

@@ -11,22 +11,19 @@
<image id="n4_r03u" name="box" src="r03ui7" fileName="Images/Panels/BasicStroke.png" xy="475,423" size="970,233" alpha="0.3" color="#8bf3ff">
<relation target="n8_r03u" sidePair="center-center,middle-middle"/>
</image>
<text id="n6_r03u" name="TextTitle" xy="878,464" pivot="0.5,0.5" size="163,35" font="ui://6hgkvlaugkm7v" fontSize="25" color="#8bf3ff" text="你确定删除吗?">
<text id="n6_r03u" name="TextTitle" xy="878,490" pivot="0.5,0.5" size="163,35" font="ui://6hgkvlaugkm7v" fontSize="25" color="#8bf3ff" text="你确定删除吗?">
<relation target="n8_r03u" sidePair="center-center,middle-middle"/>
</text>
<text id="n7_r03u" name="TextContent" xy="864,517" pivot="0.5,0.5" size="190,30" font="ui://6hgkvlaugkm7w" fontSize="21" color="#d8fbff" align="center" vAlign="middle" text="这个操作将不能恢复">
<relation target="n8_r03u" sidePair="center-center,middle-middle"/>
</text>
<component id="n9_r03u" name="BtnConfirm" src="r03uj0" fileName="Com/Buttons/BtnTitleInputControl.xml" xy="811,604" size="85,32" group="n12_oivj">
<relation target="n8_r03u" sidePair="center-center,bottom-bottom"/>
<Button title="继续"/>
</component>
<component id="n11_r03u" name="BtnCancel" src="r03uj0" fileName="Com/Buttons/BtnTitleInputControl.xml" xy="1023,604" size="85,32" group="n12_oivj">
<component id="n11_r03u" name="BtnCancel" src="r03uj0" fileName="Com/Buttons/BtnTitleInputControl.xml" xy="790,604" size="85,32" group="n12_oivj">
<gearDisplay controller="MessageStyle" pages="0"/>
<relation target="n8_r03u" sidePair="center-center,bottom-bottom"/>
<Button title="退出" icon="ui://6hgkvlau9mf1jw"/>
</component>
<group id="n12_oivj" name="n12" xy="811,604" size="297,32" advanced="true" layout="hz" colGap="127" excludeInvisibles="true">
<component id="n9_r03u" name="BtnConfirm" src="r03uj0" fileName="Com/Buttons/BtnTitleInputControl.xml" xy="1045,604" size="85,32" group="n12_oivj">
<relation target="n8_r03u" sidePair="center-center,bottom-bottom"/>
<Button title="继续"/>
</component>
<group id="n12_oivj" name="n12" xy="790,604" size="340,32" advanced="true" layout="hz" colGap="170" excludeInvisibles="true">
<relation target="" sidePair="center-center,middle-middle"/>
</group>
</displayList>

View File

@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<component size="874,70" extention="Button">
<controller name="button" pages="2,up,3,down,4,over,5,selectedOver" selected="2"/>
<controller name="button" pages="2,up,3,down,4,over,5,selectedOver" selected="3"/>
<displayList>
<image id="n5_e9z8" name="back" src="kryob" fileName="Images/Square.png" pkg="6hgkvlau" xy="0,0" size="868,70" alpha="0.2" color="#8bf3ff">
<image id="n5_e9z8" name="back" src="kryob" fileName="Images/Square.png" pkg="6hgkvlau" xy="0,0" size="868,70" alpha="0.5" color="#5c748b">
<gearLook controller="button" pages="3,5" values="0.5,0,0,0|0.5,0,0,0" default="0.2,0,0,0"/>
<gearColor controller="button" pages="2,4" values="#5c748b|#8bf3ff" default="#5c748b"/>
<relation target="" sidePair="width-width,height-height"/>

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<component size="1920,1080">
<displayList>
<graph id="n0_5dtx" name="n0" xy="0,0" size="1920,1080" alpha="0.8" type="rect" lineSize="0" fillColor="#ff000000"/>
<text id="n1_5dtx" name="n1" xy="838,522" size="249,35" fontSize="25" color="#d8fbff" align="center" vAlign="middle" text="请输入要绑定的按键...">
<relation target="" sidePair="center-center,middle-middle"/>
</text>
</displayList>
</component>

View File

@@ -35,5 +35,8 @@
<relation target="n10_e9z8" sidePair="bottom-bottom,rightext-right,leftext-left"/>
</image>
<component id="n13_j6rn" name="Mask" src="j6rn18" fileName="Com/SettingMask.xml" xy="0,0" visible="false"/>
<component id="n15_5dtx" name="InputWait" src="5dtx1d" fileName="InputWaitingCom.xml" xy="0,0" visible="false">
<relation target="" sidePair="width-width,height-height%"/>
</component>
</displayList>
</component>

View File

@@ -45,6 +45,7 @@
<component id="j6rn18" name="SettingMask.xml" path="/Com/"/>
<component id="5dtx1b" name="KeyboardInput.xml" path="/Com/Items/"/>
<component id="5dtx1c" name="SettingInputItem.xml" path="/Com/Items/" exported="true"/>
<component id="5dtx1d" name="InputWaitingCom.xml" path="/" exported="true"/>
</resources>
<publish name="" path="../Assets/Resources/Fgui/Main" packageCount="2" genCode="true"/>
</packageDescription>

View File

@@ -0,0 +1 @@
{"url":"ui://hxr7rc7p5dtx1d","name":"InputWaitingCom","scriptType":null,"isCustomName":false,"customName":"","annotation":"","member":{}}

View File

@@ -18,5 +18,6 @@
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ASphereCollider_002Ecs_002Fl_003AC_0021_003FUsers_003F60527_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F03ef825315384b1cab81c4b53eb03d922ac00_003F31_003F871dbbe1_003FSphereCollider_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ASystemLanguage_002Ecs_002Fl_003AC_0021_003FUsers_003Fbob_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F021f30a9a92b48ce98ae6b39956dd76a1df600_003F90_003F0e6861e6_003FSystemLanguage_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AVector2Int_002Ecs_002Fl_003AC_0021_003FUsers_003Fbob_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F021f30a9a92b48ce98ae6b39956dd76a1df600_003F23_003F52710087_003FVector2Int_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AVoid_002Ecs_002Fl_003AC_0021_003FUsers_003Fbob_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F314938d17f3848e8ac683e11b27f62ee46ae00_003F6f_003F6fe2b5e6_003FVoid_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
</wpf:ResourceDictionary>