方向键控制
This commit is contained in:
@@ -1338,6 +1338,17 @@ namespace NBF
|
||||
""isComposite"": false,
|
||||
""isPartOfComposite"": false
|
||||
},
|
||||
{
|
||||
""name"": """",
|
||||
""id"": ""ec9eb0c2-a2e0-47eb-ac8b-52cb0ef46101"",
|
||||
""path"": ""<Keyboard>/rightArrow"",
|
||||
""interactions"": """",
|
||||
""processors"": """",
|
||||
""groups"": """",
|
||||
""action"": ""Right"",
|
||||
""isComposite"": false,
|
||||
""isPartOfComposite"": false
|
||||
},
|
||||
{
|
||||
""name"": """",
|
||||
""id"": ""031b930d-59b6-4a26-9dfe-97764c3e8b37"",
|
||||
@@ -1360,6 +1371,17 @@ namespace NBF
|
||||
""isComposite"": false,
|
||||
""isPartOfComposite"": false
|
||||
},
|
||||
{
|
||||
""name"": """",
|
||||
""id"": ""680b36f2-5797-4257-83a7-3a498891d856"",
|
||||
""path"": ""<Keyboard>/leftArrow"",
|
||||
""interactions"": """",
|
||||
""processors"": """",
|
||||
""groups"": """",
|
||||
""action"": ""Left"",
|
||||
""isComposite"": false,
|
||||
""isPartOfComposite"": false
|
||||
},
|
||||
{
|
||||
""name"": """",
|
||||
""id"": ""45f78626-cbf2-4e1f-904d-d82e4c2a2f41"",
|
||||
@@ -1382,6 +1404,17 @@ namespace NBF
|
||||
""isComposite"": false,
|
||||
""isPartOfComposite"": false
|
||||
},
|
||||
{
|
||||
""name"": """",
|
||||
""id"": ""40501ac1-9d3e-4059-8e8f-ff7035f6e32e"",
|
||||
""path"": ""<Keyboard>/downArrow"",
|
||||
""interactions"": """",
|
||||
""processors"": """",
|
||||
""groups"": """",
|
||||
""action"": ""Down"",
|
||||
""isComposite"": false,
|
||||
""isPartOfComposite"": false
|
||||
},
|
||||
{
|
||||
""name"": """",
|
||||
""id"": ""f1a47b3c-e79f-4ab3-86b6-fcc0a6470114"",
|
||||
@@ -1404,6 +1437,17 @@ namespace NBF
|
||||
""isComposite"": false,
|
||||
""isPartOfComposite"": false
|
||||
},
|
||||
{
|
||||
""name"": """",
|
||||
""id"": ""9996c4e1-f7fb-4793-ab51-da569bdbab09"",
|
||||
""path"": ""<Keyboard>/upArrow"",
|
||||
""interactions"": """",
|
||||
""processors"": """",
|
||||
""groups"": """",
|
||||
""action"": ""Up"",
|
||||
""isComposite"": false,
|
||||
""isPartOfComposite"": false
|
||||
},
|
||||
{
|
||||
""name"": """",
|
||||
""id"": ""393f1eb9-d10d-4cd1-87e7-ea299f92b2da"",
|
||||
|
||||
@@ -137,7 +137,8 @@ namespace NBF
|
||||
// UI.Inst.OpenUI<FishingShopPanel>();
|
||||
LoadData();
|
||||
UI.Inst.OpenUI<CommonTopPanel>();
|
||||
Fishing.Inst.Go(1);
|
||||
UI.Inst.OpenUI<FishingPanel>();
|
||||
// Fishing.Inst.Go(1);
|
||||
// UI.Inst.OpenUI<SettingPanel>();
|
||||
// UI.Inst.OpenUI<HomePanel>();
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace NBF
|
||||
|
||||
public HomeButtonGroups OpGroup;
|
||||
public BottomMenu BottomMenu;
|
||||
public GButton BtnTest;
|
||||
|
||||
public override void ConstructFromXML(XML xml)
|
||||
{
|
||||
@@ -20,6 +21,7 @@ namespace NBF
|
||||
|
||||
OpGroup = (HomeButtonGroups)GetChild("OpGroup");
|
||||
BottomMenu = (BottomMenu)GetChild("BottomMenu");
|
||||
BtnTest = (GButton)GetChild("BtnTest");
|
||||
OnInited();
|
||||
UILanguage.TrySetComponentLanguage(this);
|
||||
}
|
||||
|
||||
@@ -3,16 +3,29 @@
|
||||
using System.Collections.Generic;
|
||||
using FairyGUI;
|
||||
using NBC;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public partial class HomeMainPage : HomePageBase
|
||||
{
|
||||
private List<GButton> _buttons = new List<GButton>();
|
||||
private ButtonNavigate navigate;
|
||||
|
||||
private void OnInited()
|
||||
{
|
||||
_buttons.Add(OpGroup.BtnGo);
|
||||
List<GButton> buttons = new List<GButton>();
|
||||
var children = OpGroup.GetChildren();
|
||||
foreach (var child in children)
|
||||
{
|
||||
if (child is GButton button)
|
||||
{
|
||||
buttons.Add(button);
|
||||
}
|
||||
}
|
||||
|
||||
buttons.Add(BtnTest);
|
||||
|
||||
navigate = new ButtonNavigate(buttons);
|
||||
}
|
||||
|
||||
protected override void OnShow()
|
||||
@@ -21,6 +34,7 @@ namespace NBF
|
||||
UseBottomMenu();
|
||||
}
|
||||
|
||||
|
||||
protected override void OnHide()
|
||||
{
|
||||
InputManager.OnUICanceled -= OnUICanceled;
|
||||
@@ -37,9 +51,23 @@ namespace NBF
|
||||
}
|
||||
else if (action == InputDef.UI.Up)
|
||||
{
|
||||
navigate.Up();
|
||||
}
|
||||
else if (action == InputDef.UI.Down)
|
||||
{
|
||||
navigate.Down();
|
||||
}
|
||||
else if (action == InputDef.UI.Right)
|
||||
{
|
||||
navigate.Right();
|
||||
}
|
||||
else if (action == InputDef.UI.Left)
|
||||
{
|
||||
navigate.Left();
|
||||
}
|
||||
else if (action == InputDef.UI.Enter)
|
||||
{
|
||||
navigate.Click();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
164
Assets/Scripts/Utils/ButtonNavigate.cs
Normal file
164
Assets/Scripts/Utils/ButtonNavigate.cs
Normal file
@@ -0,0 +1,164 @@
|
||||
using System.Collections.Generic;
|
||||
using FairyGUI;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public class ButtonNavigate
|
||||
{
|
||||
private readonly List<GButton> _buttons = new List<GButton>();
|
||||
private int _currentIndex = 0;
|
||||
|
||||
public ButtonNavigate(List<GButton> buttons, bool selectFist = true)
|
||||
{
|
||||
_buttons.AddRange(buttons);
|
||||
if (selectFist && _buttons.Count > 0)
|
||||
{
|
||||
SetButtonSelect(buttons[0]);
|
||||
}
|
||||
}
|
||||
|
||||
public void Up()
|
||||
{
|
||||
Navigate(Vector2.up);
|
||||
}
|
||||
|
||||
public void Down()
|
||||
{
|
||||
Navigate(Vector2.down);
|
||||
}
|
||||
|
||||
public void Left()
|
||||
{
|
||||
Navigate(Vector2.left);
|
||||
}
|
||||
|
||||
public void Right()
|
||||
{
|
||||
Navigate(Vector2.right);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 点击选中按钮
|
||||
/// </summary>
|
||||
public void Click()
|
||||
{
|
||||
foreach (var button in _buttons)
|
||||
{
|
||||
if (button.selected)
|
||||
{
|
||||
button.Click();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Navigate(Vector2 direction)
|
||||
{
|
||||
GButton current = _buttons[_currentIndex];
|
||||
Rect currentRect = GetGlobalRect(current);
|
||||
|
||||
GButton bestCandidate = null;
|
||||
float bestDistance = float.MaxValue;
|
||||
|
||||
foreach (var candidate in _buttons)
|
||||
{
|
||||
if (candidate == current) continue;
|
||||
|
||||
Rect candidateRect = GetGlobalRect(candidate);
|
||||
|
||||
// 检查方向匹配性
|
||||
bool isDirectionMatch = false;
|
||||
|
||||
if (direction == Vector2.up) // 上方向
|
||||
{
|
||||
isDirectionMatch = candidateRect.yMax <= currentRect.yMin &&
|
||||
RectOverlapX(currentRect, candidateRect);
|
||||
}
|
||||
else if (direction == Vector2.down) // 下方向
|
||||
{
|
||||
isDirectionMatch = candidateRect.yMin >= currentRect.yMax &&
|
||||
RectOverlapX(currentRect, candidateRect);
|
||||
}
|
||||
else if (direction == Vector2.left) // 左方向
|
||||
{
|
||||
isDirectionMatch = candidateRect.xMax <= currentRect.xMin &&
|
||||
RectOverlapY(currentRect, candidateRect);
|
||||
}
|
||||
else if (direction == Vector2.right) // 右方向
|
||||
{
|
||||
isDirectionMatch = candidateRect.xMin >= currentRect.xMax &&
|
||||
RectOverlapY(currentRect, candidateRect);
|
||||
}
|
||||
|
||||
if (isDirectionMatch)
|
||||
{
|
||||
float distance = Vector2.Distance(currentRect.center, candidateRect.center);
|
||||
if (distance < bestDistance)
|
||||
{
|
||||
bestDistance = distance;
|
||||
bestCandidate = candidate;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (bestCandidate != null)
|
||||
{
|
||||
_currentIndex = _buttons.IndexOf(bestCandidate);
|
||||
SetButtonSelect(bestCandidate);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取按钮在根坐标空间中的矩形区域
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
private Rect GetGlobalRect(GObject obj)
|
||||
{
|
||||
// 获取对象在根组件中的全局位置
|
||||
Vector2 globalPos = obj.LocalToGlobal(Vector2.zero);
|
||||
Vector2 rootPos = GRoot.inst.GlobalToLocal(globalPos);
|
||||
|
||||
// 考虑对象的缩放
|
||||
float width = obj.width * obj.scaleX;
|
||||
float height = obj.height * obj.scaleY;
|
||||
|
||||
return new Rect(rootPos.x, rootPos.y, width, height);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查两个矩形在X轴上的重叠
|
||||
/// </summary>
|
||||
/// <param name="a"></param>
|
||||
/// <param name="b"></param>
|
||||
/// <returns></returns>
|
||||
private bool RectOverlapX(Rect a, Rect b)
|
||||
{
|
||||
return !(a.xMin >= b.xMax || a.xMax <= b.xMin);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查两个矩形在Y轴上的重叠
|
||||
/// </summary>
|
||||
/// <param name="a"></param>
|
||||
/// <param name="b"></param>
|
||||
/// <returns></returns>
|
||||
private bool RectOverlapY(Rect a, Rect b)
|
||||
{
|
||||
return !(a.yMin >= b.yMax || a.yMax <= b.yMin);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置按钮选中
|
||||
/// </summary>
|
||||
/// <param name="button"></param>
|
||||
private void SetButtonSelect(GButton button)
|
||||
{
|
||||
foreach (var btn in _buttons)
|
||||
{
|
||||
btn.selected = btn == button;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Utils/ButtonNavigate.cs.meta
Normal file
3
Assets/Scripts/Utils/ButtonNavigate.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 83f029b458614e419234114c3d90f1fc
|
||||
timeCreated: 1749009573
|
||||
13
Assets/Scripts/Utils/Extends/FairyGUIExtensions.cs
Normal file
13
Assets/Scripts/Utils/Extends/FairyGUIExtensions.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using FairyGUI;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public static class FairyGUIExtensions
|
||||
{
|
||||
public static void Click(this GButton button)
|
||||
{
|
||||
button.FireClick(true);
|
||||
button.onClick.Call();
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Utils/Extends/FairyGUIExtensions.cs.meta
Normal file
3
Assets/Scripts/Utils/Extends/FairyGUIExtensions.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6f9d43645df348cc8e3e498a7570cd2f
|
||||
timeCreated: 1749008303
|
||||
Reference in New Issue
Block a user