一场游戏对局中,和队友进行友好的有礼貌的有效的沟通可以极大提升对局胜率。在LOL国服,打字和队友聊天沟通战术是非常常见的行为:
语言的力量是无限的,甚至可以和外国小伙伴效沟通战术:
聊天系统制作起来非常简单,只要熟练掌握C#的TCP通讯协议即可,然后在服务器和客户端做好分发,流程就是:
某个客户端定义文字数据,发送给服务器(开喷) ==> 服务器接收消息分发给所有客户端 ==> 所有客户端接收消息判断消息归属IP,调用UI显示内容 ==> 回喷……
嗯,对,就是这样,剩下的就交给上帝吧......
第一步,定义Socket通讯协议,定义收发消息的方法
/// <summary>
/// Socket协议
/// </summary>
public enum MessageType
{
/// <summary>
/// 链接协议
/// </summary>
Connect = 0,
/// <summary>
/// 游戏逻辑协议
/// </summary>
GameLogic = 1,
/// <summary>
/// 游戏环境设置协议
/// </summary>
GameSetting = 2,
/// <summary>
/// 玩家操作协议
/// </summary>
Player = 3,
}
/// <summary>
/// 连接Socket协议
/// </summary>
public enum ConnectMessage
{
/// <summary>
/// 加入房间
/// </summary>
Join = 0,
/// <summary>
/// 请求断开连接
/// </summary>
Exit = 1,
/// <summary>
/// 中途离开游戏
/// </summary>
ExitGame = 2,
/// <summary>
/// 中途加入游戏
/// </summary>
JoinGame = 3,
/// <summary>
/// 关闭服务器
/// </summary>
Close = 4,
}
/// <summary>
/// 玩家操作Socket协议
/// </summary>
public enum PlayerMessage
{
/// <summary>
/// 发送聊天数据
/// </summary>
SendChat = 0,
/// <summary>
/// 操作移动
/// </summary>
Move = 1,
/// <summary>
/// 释放技能
/// </summary>
Skill = 2,
/// <summary>
/// 执行英雄动画
/// </summary>
Animator = 3,
/// <summary>
/// 同步英雄transform数据
/// </summary>
SyncTransform = 4,
/// <summary>
/// 击杀判定
/// </summary>
KillJudge = 5,
/// <summary>
/// 属性变化
/// </summary>
AttributeJudge = 6,
/// <summary>
/// 属性面板变化
/// </summary>
PropertyPanel = 7,
/// <summary>
/// 背包内容变化
/// </summary>
PackageChange = 8,
}
第二步,定义客户端发送消息的方法:
using System.Net.Sockets;
/// <summary>
/// 客户端Socket
/// </summary>
public Socket clientSocket;
/// <summary>
/// 发送一条文字信息给服务器
/// </summary>
/// <param name="msg"></param>
public void PutMessageToQueue(MessageData msgdata)
{
//将对象序列化发过去
byte[] msgBytes = ToolsUtil.StringToByteArray(JsonMapper.ToJson(msgdata));
if (msgBytes.Length > 2048)
{
iDebug.YiYan("数据长度大于2048,请减少数据");
}
else
{
SendBytesMessageToServer(msgBytes, 0, msgBytes.Length);
}
}
/// <summary>
/// 发送消息的方法
/// </summary>
/// <param name="sendMsgContent">消息内容</param>
/// <param name="offset">从消息内容第几个开始发送</param>
/// <param name="size">发送的长度</param>
public void SendBytesMessageToServer(byte[] sendMsgContent, int offset, int size)
{
// Debug.Log("开始发送!!准备发送长度=" + size);
if (clientSocket.Connected == true)
{
clientSocket.BeginSend(sendMsgContent, offset, size, SocketFlags.None, SendMessageCallback, "");
}
else
{
EventManager.playerControl_C.ClearData();
}
}
第三步,定义客户端接收消息并显示的方法:
/// <summary>
/// 接收消息的方法
/// </summary>
/// <param name="byteArray"></param>
/// <param name="offset"></param>
/// <param name="length"></param>
public void ReceiveMsgData(byte[] byteArray, int offset, int length)
{
string msg = ToolsUtil.ByteArrayToString(byteArray, offset, length);
// Debug.Log("收到信息:" + msg);
if (msg.Length > 0)
{
//对信息进行处理
MessageData data = JsonMapper.ToObject<MessageData>(msg);
Action_Talk(data);
}
else
{
Debug.LogError("连接中断……");
EventManager.playerControl_C.ClearData();
}
}
/// <summary>
/// 执行对话
/// </summary>
/// <param name="value"></param>
public void Action_Talk(MessageData value)
{
PlayerSocketData playerSocketData = JsonMapper.ToObject<PlayerSocketData>(value.socketData);
if (localMacID == playerSocketData.MacID)
{
UIManager.console_C.ReceiveMessage("我:" + value.msg);
}
else
{
UIManager.console_C.ReceiveMessage(playerSocketData.Name + "说:" + value.msg);
}
DataManager.playerData_C.GetData(playerSocketData.UserID).heroAI.heroCanvas.StartTalk(value.msg);
//发送弹幕
BulletScreen.Single.PushComments(playerSocketData.Name + ":" + value.msg);
}
在“执行对话”Action_Talk 方法里,通过socket接收到的消息需要在UI上实时显示即可,这里就不多做赘述啦~~ 感兴趣的小伙伴,可以做成弹幕哦~~
原理很简单,就是以服务器作为中转,看了第一篇socket帧同步的小伙伴很容易实现聊天系统
下一篇开始开始游戏AI的制作,尽请期待哈~ ~ ~
文末福利:↓↓↓