登录和输入控件

This commit is contained in:
2025-08-20 23:57:22 +08:00
parent 818bf63d8d
commit 2f8251fe63
28 changed files with 346 additions and 70 deletions

View File

@@ -55,6 +55,7 @@ namespace NBC.Network.Interface
{
try
{
await Run(session, (T) message);
}
catch (Exception e)

View File

@@ -274,67 +274,6 @@ namespace NBC.Network.Interface
messageHandler.Handle(session, rpcId, protocolCode, message).Coroutine();
}
// 如果编译符号FANTASY_NET存在定义处理路由消息的方法
#if FANTASY_NET
/// <summary>
/// 处理路由消息,将消息分发给相应的路由消息处理器。
/// </summary>
/// <param name="session">会话对象</param>
/// <param name="type">消息类型</param>
/// <param name="entity">实体对象</param>
/// <param name="message">消息对象</param>
/// <param name="rpcId">RPC标识</param>
public async FTask RouteMessageHandler(Session session, Type type, Entity entity, object message, uint rpcId)
{
if (!_routeMessageHandlers.TryGetValue(type, out var routeMessageHandler))
{
Log.Warning($"Scene:{session.Scene.Id} Found Unhandled RouteMessage: {message.GetType()}");
if (message is IRouteRequest request)
{
FailRouteResponse(session, request.GetType(), InnerErrorCode.ErrEntityNotFound, rpcId);
}
return;
}
var runtimeId = entity.RuntimeId;
var sessionRuntimeId = session.RuntimeId;
if (entity is Scene)
{
// 如果是Scene的话、就不要加锁了、如果加锁很一不小心就可能会造成死锁
await routeMessageHandler.Handle(session, entity, rpcId, message);
return;
}
// 使用协程锁来确保多线程安全
using (await _receiveRouteMessageLock.Wait(runtimeId))
{
if (sessionRuntimeId != session.RuntimeId)
{
return;
}
if (runtimeId != entity.RuntimeId)
{
if (message is IRouteRequest request)
{
FailRouteResponse(session, request.GetType(), InnerErrorCode.ErrEntityNotFound, rpcId);
}
return;
}
await routeMessageHandler.Handle(session, entity, rpcId, message);
}
}
internal bool GetCustomRouteType(long protocolCode, out int routeType)
{
return _customRouteMap.TryGetValue(protocolCode, out routeType);
}
#endif
internal void FailRouteResponse(Session session, Type requestType, uint error, uint rpcId)
{
var response = CreateRouteResponse(requestType, error);

75
Assets/Scripts/Net/Net.cs Normal file
View File

@@ -0,0 +1,75 @@
using Assets.Scripts.Entity;
using Assets.Scripts.Hotfix;
using NBC;
using NBC.Network;
using NBC.Network.Interface;
namespace NBF
{
public class Net : EventDispatcher
{
public static Session Session { get; private set; }
public const int HeartbeatInterval = 5000;
#region
private static Net _inst;
public static Net Inst => _inst ??= new Net();
public Net()
{
}
#endregion
#region
public static Session CreateSession(string address)
{
var session = SessionHelper.CreateSession(App.Main, address, OnConnectComplete,
OnConnectFail,
OnConnectDisconnect);
Session = session;
return session;
}
private static void OnConnectComplete()
{
Log.Debug("连接成功");
Session.AddComponent<SessionHeartbeatComponent>().Start(HeartbeatInterval);
}
private static void OnConnectFail()
{
Log.Debug("连接失败");
// SetLoginState(false);
}
private static void OnConnectDisconnect()
{
Log.Debug("连接断开");
// SetLoginState(false);
}
#endregion
#region
public static FTask<IResponse> Call(IRequest request, long routeId = 0)
{
// var response = await Session.Call(request, routeId);
// response.DispatchMessage();
return Session.Call(request, routeId);;
}
public static void Send(IMessage message, uint rpcId = 0, long routeId = 0)
{
Session.Send(message, rpcId, routeId);
}
#endregion
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1fe56eeffd614eaea7055d1c65f6859f
timeCreated: 1755701226

View File

@@ -0,0 +1,12 @@
using NBC.Network.Interface;
namespace NBF
{
public static class NetExtends
{
public static void DispatchMessage(this IResponse response)
{
Net.Inst.DispatchEventWith(response.OpCode().ToString(), response);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 25d56f7c87744a199e926365a516b85f
timeCreated: 1755702255

View File

@@ -132,9 +132,11 @@ namespace NBF
// UI.Inst.OpenUI<FishingShopPanel>();
LoadData();
CommonTopPanel.Show();
LoginPanel.Show();
// FishingPanel.Show();
PreviewPanel.Show();
Fishing.Inst.Go(1);
// PreviewPanel.Show();
// Fishing.Inst.Go(1);
// HomePanel.Show();
// ChatTestPanel.Show();

View File

@@ -12,6 +12,7 @@ namespace NBF
UIObjectFactory.SetPackageItemExtension(SelectPages.URL, typeof(SelectPages));
UIObjectFactory.SetPackageItemExtension(ModelTexture.URL, typeof(ModelTexture));
UIObjectFactory.SetPackageItemExtension(BottomMenu.URL, typeof(BottomMenu));
UIObjectFactory.SetPackageItemExtension(CommonInput.URL, typeof(CommonInput));
UIObjectFactory.SetPackageItemExtension(ClassifyList.URL, typeof(ClassifyList));
UIObjectFactory.SetPackageItemExtension(CommonMenu.URL, typeof(CommonMenu));
UIObjectFactory.SetPackageItemExtension(MarqueeTag.URL, typeof(MarqueeTag));

View File

@@ -0,0 +1,29 @@
/**本脚本为自动生成每次生成会覆盖请勿手动修改生成插件文档及项目地址https://git.whoot.com/whoot-games/whoot.fguieditorplugin**/
using FairyGUI;
using FairyGUI.Utils;
using NBC;
namespace NBF
{
public partial class CommonInput
{
public const string URL = "ui://6hgkvlau9zboma";
public Controller FocusState;
public GTextInput Input;
public GImage bottomBack;
public override void ConstructFromXML(XML xml)
{
base.ConstructFromXML(xml);
FocusState = GetController("FocusState");
Input = (GTextInput)GetChild("Input");
bottomBack = (GImage)GetChild("bottomBack");
OnInited();
UILanguage.TrySetComponentLanguage(this);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: fb776407f4f25e64a8e803ba135bddef

View File

@@ -0,0 +1,47 @@
// 本脚本只在不存在时会生成一次。组件逻辑写在当前脚本内。已存在不会再次生成覆盖
using UnityEngine;
using FairyGUI;
using NBC;
namespace NBF
{
public partial class CommonInput : GComponent
{
public EventListener onChanged => Input.onChanged;
public override string text
{
get => Input.text;
set => Input.text = value;
}
public string title
{
get => text;
set => text = value;
}
public string promptText
{
get => Input.promptText;
set => Input.promptText = value;
}
private void OnInited()
{
onFocusIn.Add(OnInputFocusIn);
onFocusOut.Add(OnInputFocusOut);
}
private void OnInputFocusIn()
{
FocusState.selectedIndex = 1;
}
private void OnInputFocusOut()
{
FocusState.selectedIndex = 0;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: c7adc7972d1d401489dc796fb27d1eaa

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 12482424a8714a5fb52ce23312a0a0f6
timeCreated: 1755697902

View File

@@ -0,0 +1,54 @@
using Assets.Scripts.Entity;
using Assets.Scripts.Hotfix;
using NBC;
using NBC.Network;
namespace NBF
{
public static class LoginHelper
{
private static Session _session;
public static async FTask Login(string account)
{
_session = Net.CreateSession("127.0.0.1:20001");
var acc = account;
// 发送登录的请求给服务器
var response = (A2C_LoginResponse)await Net.Call(new C2A_LoginRequest()
{
Username = acc,
Password = acc,
LoginType = 1
});
if (response.ErrorCode != 0)
{
Log.Error($"登录发生错误{response.ErrorCode}");
return;
}
if (!App.Main.GetComponent<JWTParseComponent>().Parse(response.ToKen, out var payload))
{
return;
}
// 根据ToKen返回的Address登录到Gate服务器
_session = Net.CreateSession(payload.Address);
// 发送登录请求到Gate服务器
var loginResponse = (G2C_LoginResponse)await Net.Call(new C2G_LoginRequest()
{
ToKen = response.ToKen
});
if (loginResponse.ErrorCode != 0)
{
Log.Error($"登录发生错误{loginResponse.ErrorCode}");
return;
}
Log.Debug($"登录到Gate服务器成功ErrorCode:{loginResponse.ErrorCode}");
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 031ed023449844cdb9d6c4eb5d7fee90
timeCreated: 1755698636

View File

@@ -0,0 +1,32 @@
/**本脚本为自动生成每次生成会覆盖请勿手动修改生成插件文档及项目地址https://git.whoot.com/whoot-games/whoot.fguieditorplugin**/
using FairyGUI;
using FairyGUI.Utils;
using NBC;
using System.Collections.Generic;
namespace NBF
{
/// <summary> </summary>
public partial class LoginPanel
{
public GObject this[string aKey] => ContentPane.GetChild(aKey);
public override string UIPackName => "Main";
public override string UIResName => "LoginPanel";
[AutoFind(Name = "Back")]
public GLabel Back;
[AutoFind(Name = "InputAccount")]
public CommonInput InputAccount;
[AutoFind(Name = "BtnLogin")]
public GButton BtnLogin;
public override string[] GetDependPackages(){ return new string[] {"Common"}; }
public static void Show(object param = null){ App.UI.OpenUI<LoginPanel>(param); }
public static void Hide(){ App.UI.HideUI<LoginPanel>(); }
public static void Del(){ App.UI.DestroyUI<LoginPanel>(); }
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 3b11ed3a4a10a3549a06d7845c9c4cfd

View File

@@ -0,0 +1,25 @@
// 本脚本只在不存在时会生成一次。已存在不会再次生成覆盖
using FairyGUI;
using UnityEngine;
using NBC;
using UIPanel = NBC.UIPanel;
namespace NBF
{
public partial class LoginPanel : UIPanel
{
protected override void OnInit()
{
this.AutoAddClick(OnClick);
}
private void OnClick(GComponent btn)
{
if (btn == BtnLogin)
{
LoginHelper.Login(InputAccount.text).Coroutine();
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: b2d32e54ef5680541959b782ee8d0c7a