🚩 Import
- 下载SKFramework框架,导入到
Unity
中;
- 在框架
Package Manager
中搜索并下载导入Socket
模块;
- Package包中包含
Server
服务端内容以及protogen
工具,将其解压到工程外;
🚀 protogen使用方法
- 编写的
.proto
文件放入proto文件夹中;
- 打开
run.bat
文件,编辑编译指令;
- 运行
run.bat
文件,生成后的.cs
脚本在cs文件夹中,将其放入到Unity中即可;
注:.proto文件编译为.cs脚本后,该脚本一般不轻易改动。
- 如果有大量的.proto文件需要编译,编辑编译指令可能会比较繁琐,因此可以使用自定义的工具
Protogen Helper
来自动创建run.bat
文件。
代码如下:
using System.IO;
using UnityEngine;
using UnityEditor;
using System.Text;
using System.Diagnostics;
namespace Mutiplayer
{
/// <summary>
/// Proto通信协议类编译工具
/// </summary>
public class ProtogenHelper : EditorWindow
{
[MenuItem("Multiplayer/Protogen Helper")]
public static void Open()
{
var window = GetWindow<ProtogenHelper>("Protogen Helper");
window.maxSize = new Vector2(1000f, 60f);
window.minSize = new Vector2(200f, 60f);
window.Show();
}
//根路径
private string rootPath;
private const string prefsKey = "Protogen.exe Path";
private void OnEnable()
{
rootPath = EditorPrefs.HasKey(prefsKey) ? EditorPrefs.GetString(prefsKey) : string.Empty;
}
private void OnGUI()
{
GUILayout.Label("protogen.exe所在路径:");
GUILayout.BeginHorizontal();
{
string path = GUILayout.TextField(rootPath);
if (path != rootPath)
{
rootPath = path;
EditorPrefs.SetString(prefsKey, rootPath);
}
if (GUILayout.Button("Browse", GUILayout.Width(55f)))
{
path = EditorUtility.OpenFolderPanel("选择路径", rootPath, null);
if (path != rootPath)
{
rootPath = path;
EditorPrefs.SetString(prefsKey, rootPath);
}
}
}
GUILayout.EndHorizontal();
if (GUILayout.Button("Create .bat"))
{
string protoPath = rootPath + "/proto";
if (!Directory.Exists(protoPath))
{
UnityEngine.Debug.Log($"<color=red>文件夹不存在</color> {
protoPath}");
return;
}
string csPath = rootPath + "/cs";
//如果cs文件夹不存在则创建
if (!Directory.Exists(csPath))
{
Directory.CreateDirectory(csPath);
}
DirectoryInfo di = new DirectoryInfo(protoPath);
//获取所有.proto文件信息
FileInfo[] protos = di.GetFiles("*.proto");
//使用StringBuilder拼接字符串
StringBuilder sb = new StringBuilder();
//遍历
for (int i = 0; i < protos.Length; i++)
{
string proto = protos[i].Name;
//拼接编译指令
sb.Append(rootPath + @"/protogen.exe -i:proto\" + proto + @" -o:cs\" + proto.Replace(".proto", ".cs") + "\r\n");
}
sb.Append("pause");
//生成".bat文件"
string batPath = $"{
rootPath}/run.bat";
File.WriteAllText(batPath, sb.ToString());
//打开该文件夹
Process.Start(rootPath);
}
}
}
}
🪐 客户端接口
Connect
: 连接服务端;
/// <summary>
/// 连接服务端
/// </summary>
/// <param name="ip">服务器IP地址</param>
/// <param name="port">端口</param>
public void Connect(string ip, int port)
Send
:发送数据;
/// <summary>
/// 发送数据
/// </summary>
/// <param name="proto">协议</param>
public void Send(IExtensible proto)
Close
:关闭与服务端的连接;
/// <summary>
/// 关闭连接
/// </summary>
public void Close()
🌈 服务端接口
- 向单个客户端发送数据;
/// <summary>
/// 向客户端发送协议(单点发送)
/// </summary>
/// <param name="client">客户端</param>
/// <param name="proto">协议</param>
public static void Send(Client client, IExtensible proto)
- 向所有客户端发送数据;
/// <summary>
/// 向所有客户端发送协议(广播)
/// </summary>
/// <param name="proto">协议</param>
public static void Send(IExtensible proto)
- 向指定客户端之外的所有客户端发送数据;
/// <summary>
/// 向指定客户端之外的所有客户端发送协议
/// </summary>
/// <param name="proto">协议</param>
/// <param name="except">不需要发送的客户端</param>
public static void Send(IExtensible proto, Client except)
- 关闭指定客户端的连接;
/// <summary>
/// 关闭客户端连接
/// </summary>
/// <param name="client">客户端</param>
public static void Close(Client client)
🧭 数据处理
根据解析出的协议名
来调用相应的处理方法:
以上是服务端对ProtoTest
类型协议的处理示例,服务端通过Send将该消息转发给所有客户端。
🎨 Example
using UnityEngine;
using SK.Framework.Sockets;
using System.Collections.Generic;
public class Example : MonoBehaviour
{
private Vector2 scroll;
private List<string> messages = new List<string>();
private string content;
private NetworkManager NetworkManager;
private void OnGUI()
{
GUI.enabled = !NetworkManager.IsConnected;
if (GUILayout.Button("Connect", GUILayout.Width(200f), GUILayout.Height(50f)))
{
NetworkManager.Connect("127.0.0.1", 8801);
}
GUI.enabled = NetworkManager.IsConnected;
if (GUILayout.Button("Disconnect", GUILayout.Width(200f), GUILayout.Height(50f)))
{
NetworkManager.Close();
}
GUILayout.BeginVertical("Box", GUILayout.Height(200f), GUILayout.Width(300f));
scroll = GUILayout.BeginScrollView(scroll);
{
for (int i = 0; i < messages.Count; i++)
{
GUILayout.Label(messages[i]);
}
}
GUILayout.EndScrollView();
GUILayout.EndVertical();
GUILayout.BeginHorizontal();
content = GUILayout.TextField(content, GUILayout.Height(50f));
if (GUILayout.Button("Send", GUILayout.Width(50f), GUILayout.Height(50f)))
{
if (!string.IsNullOrEmpty(content))
{
ProtoBuf.IExtensible proto = new proto.ProtoTest.ProtoTest() {
content = content };
NetworkManager.Send(proto);
content = string.Empty;
}
}
GUILayout.EndHorizontal();
}
private void Start()
{
NetworkManager = GetComponent<NetworkManager>();
}
public void OnProtoTestEvent(proto.ProtoTest.ProtoTest protoTest)
{
messages.Add(protoTest.content);
}
}
转载:https://blog.csdn.net/qq_42139931/article/details/128205617
查看评论