Unity
中;Package Manager
中搜索并下载导入Socket
模块;Server
服务端内容以及protogen
工具,将其解压到工程外;.proto
文件放入proto文件夹中;run.bat
文件,编辑编译指令;run.bat
文件,生成后的.cs
脚本在cs文件夹中,将其放入到Unity中即可;注:.proto文件编译为.cs脚本后,该脚本一般不轻易改动。
Protogen Helper
来自动创建run.bat
文件。代码如下:
using System.IO;
using UnityEngine;
using UnityEditor;
using System.Text;
using System.Diagnostics;
namespace Mutiplayer
{
///
/// Proto通信协议类编译工具
///
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($"文件夹不存在 {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
: 连接服务端;///
/// 连接服务端
///
/// 服务器IP地址
/// 端口
public void Connect(string ip, int port)
Send
:发送数据;///
/// 发送数据
///
/// 协议
public void Send(IExtensible proto)
Close
:关闭与服务端的连接;///
/// 关闭连接
///
public void Close()
///
/// 向客户端发送协议(单点发送)
///
/// 客户端
/// 协议
public static void Send(Client client, IExtensible proto)
///
/// 向所有客户端发送协议(广播)
///
/// 协议
public static void Send(IExtensible proto)
///
/// 向指定客户端之外的所有客户端发送协议
///
/// 协议
/// 不需要发送的客户端
public static void Send(IExtensible proto, Client except)
///
/// 关闭客户端连接
///
/// 客户端
public static void Close(Client client)
根据解析出的协议名
来调用相应的处理方法:
以上是服务端对ProtoTest
类型协议的处理示例,服务端通过Send将该消息转发给所有客户端。
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);
}
}
Unity Multiplayer 多人在线示例工程:https://github.com/136512892/Unity-Multiplayer