T2FGame.Samples.ClientSdk
1.0.9
dotnet add package T2FGame.Samples.ClientSdk --version 1.0.9
NuGet\Install-Package T2FGame.Samples.ClientSdk -Version 1.0.9
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="T2FGame.Samples.ClientSdk" Version="1.0.9" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="T2FGame.Samples.ClientSdk" Version="1.0.9" />
<PackageReference Include="T2FGame.Samples.ClientSdk" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add T2FGame.Samples.ClientSdk --version 1.0.9
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: T2FGame.Samples.ClientSdk, 1.0.9"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package T2FGame.Samples.ClientSdk@1.0.9
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=T2FGame.Samples.ClientSdk&version=1.0.9
#tool nuget:?package=T2FGame.Samples.ClientSdk&version=1.0.9
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
T2FGame.Samples.ClientSdk
演示如何使用 T2FGame.Tools 库为客户端(Unity/TypeScript)生成类型安全的 SDK 代码。
前置条件
- 已安装 .NET 9 SDK
- 已构建 T2FGame.Samples.GameServer 项目
使用方法
1. 构建服务器项目
dotnet build ../T2FGame.Samples.GameServer -c Release
2. 运行示例
dotnet run
程序会自动:
- 分析 T2FGame.Samples.GameServer 程序集
- 提取 Action 控制器、广播消息、错误码等元数据
- 生成 Unity 客户端 SDK 代码到
bin/Debug/net9.0/output/目录
代码说明
// 创建生成器
var generator = new ClientSdkGenerator();
// 配置选项
var options = new ClientSdkOptions
{
ClientNamespace = "T2FGame.Client",
ProtoNamespace = "T2FGame.Client.Proto"
};
// 执行生成
var result = generator.Generate(assemblyPath, outputDir, options);
if (result.Success)
{
Console.WriteLine($"生成成功!共 {result.GeneratedFiles.Count} 个文件");
}
输出结果
执行后,输出目录将包含:
output/
├── UserAction.cs # 用户模块 Action
├── BattleAction.cs # 战斗模块 Action
├── ChatAction.cs # 聊天模块 Action
├── Listener.cs # 广播消息监听器
└── GameCode.cs # 错误码定义
生成的代码示例
Action 类
// UserAction.cs
namespace T2FGame.Client
{
/// <summary>
/// 用户模块 Action
/// </summary>
public static class UserAction
{
/// <summary>
/// 用户登录 (回调方式)
/// </summary>
public static void OfLogin(LoginRequest request, Action<ActionResult> callback)
{
NetworkManager.Send(CmdMerge.User_Login, request, callback);
}
/// <summary>
/// 用户登录 (async/await 方式)
/// </summary>
public static Task<ActionResult> OfAwaitLogin(LoginRequest request)
{
return NetworkManager.SendAsync(CmdMerge.User_Login, request);
}
}
}
Listener 类
// Listener.cs
namespace T2FGame.Client
{
/// <summary>
/// 广播消息监听器
/// </summary>
public static class Listener
{
/// <summary>
/// 监听用户信息更新
/// </summary>
public static void ListenUserInfoUpdate(Action<ActionResult> callback)
{
NetworkManager.Listen(BroadcastCmd.UserInfoUpdate, callback);
}
}
}
在 Unity 中使用
1. 复制生成的代码
将输出目录中的文件复制到 Unity 项目的 Assets/Scripts/Gen/ 目录。
2. 调用示例
using T2FGame.Client;
using UnityEngine;
public class GameManager : MonoBehaviour
{
void Start()
{
// 监听广播
Listener.ListenUserInfoUpdate(result =>
{
var userInfo = result.GetValue<UserInfo>();
Debug.Log($"用户信息更新: {userInfo.Nickname}");
});
}
public async void Login(string username, string password)
{
var request = new LoginRequest
{
Username = username,
Password = password
};
// async/await 方式
var result = await UserAction.OfAwaitLogin(request);
if (result.IsSuccess)
{
var response = result.GetValue<LoginResponse>();
Debug.Log($"登录成功: UserId={response.UserId}");
}
}
}
高级用法
使用 CLI 工具
也可以使用 T2FGame.Tools.Cli 命令行工具:
# 源码方式执行
dotnet run --project ../../src/T2FGame.Tools.Cli -- sdk \
-a ../../artifacts/bin/T2FGame.Samples.GameServer/Release/T2FGame.Samples.GameServer.dll \
-o ./output \
-n T2FGame.Client \
-p T2FGame.Client.Proto
# 或安装全局工具后执行
t2f-proto sdk -a ./MyGame.dll -o ./output -n MyGame.Client
常见问题
Q: 生成的代码中 NetworkManager 类找不到
NetworkManager 是客户端网络层的封装,需要您自行实现或使用现有的网络库。 生成的代码只负责定义 API 接口,不包含网络通信实现。
Q: 如何自定义命名空间
修改 Program.cs 中的配置:
var options = new ClientSdkOptions
{
ClientNamespace = "MyGame.Client",
ProtoNamespace = "MyGame.Client.Proto"
};
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net9.0
- T2FGame.Tools (>= 1.0.9)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.