概述
接Unity网络开发基础
导入基础知识中的代码
需求分析
手动写Handler类
手动书写消息池
using GamePlayer;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 消息池中 主要是用于 注册 ID和消息类型以及消息处理器类型的映射关系
/// 方便我们获取对象 进行反序列化和消息逻辑处理
/// </summary>
public class MsgPool
{
//记录消息类型和ID的映射关系
private Dictionary<int, Type> messages = new Dictionary<int, Type>();
//记录消息处理器类型和ID的映射关系
private Dictionary<int, Type> handlers = new Dictionary<int, Type>();
public MsgPool()
{
//在构造函数中进行 注册 注册映射关系
Register(1001, typeof(PlayerMsg), typeof(PlayerMsgHandler));
//后面继续添加消息类
}
private void Register(int id, Type messageType, Type handlerType)
{
messages.Add(id, messageType);
handlers.Add(id, handlerType);
}
/// <summary>
/// 根据ID 得到一个指定的消息类对象
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public BaseMsg GetMessage(int id)
{
if (!messages.ContainsKey(id))
return null;
return Activator.CreateInstance(messages[id]) as BaseMsg;
}
/// <summary>
/// 根据ID 得到一个指定的消息处理类对象
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public BaseHandler GetHandler(int id)
{
if (!handlers.ContainsKey(id))
return null;
return Activator.CreateInstance(handlers[id]) as BaseHandler;
}
}
工具生成Handler
工具生成消息池
源码
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using UnityEngine;
namespace GamePlayer
{
public enum E_Player_Type
{
Main,
Other,
}
public class PlayerTest : BaseData
{
public List<int> list;
public Dictionary<int, string> dic;
public int[] arrays;
public E_Player_Type type;
public PlayerData player;
public override int GetByteNum()
{
int num = 0;
num += 4; //list.Count
for (int i = 0; i < list.Count; i++)
{
num += 4;
}
num += 4; //dic.Count
foreach (int key in dic.Keys)
{
num += 4;//key所占的字节数
num += 4;//value 字符串长度 占的字节数
num += Encoding.UTF8.GetByteCount(dic[key]);
}
num += 4; //arrays.Length 数组长度
for (int i = 0; i < arrays.Length; i++)
{
num += 4;
}
num += 4; //枚举 用int来存
num += player.GetByteNum(); //PlayerData
return num;
}
public override int Reading(byte[] bytes, int beginIndex = 0)
{
int index = beginIndex;
//反序列化 list
list = new List<int>();
short listCount = ReadShort(bytes, ref index);
for (int i = 0; i < listCount; i++)
{
list.Add(ReadInt(bytes, ref index));
}
//dic
dic = new Dictionary<int, string>();
short dicCount = ReadShort(bytes, ref index);
for (int i = 0; i < dicCount; i++)
{
dic.Add(ReadInt(bytes, ref index), ReadString(bytes, ref index));
}
//arrays
int arrayLength = ReadShort(bytes, ref index);
arrays = new int[arrayLength];
for (int i = 0; i < arrayLength; i++)
{
arrays[i] = ReadInt(bytes, ref index);
}
//枚举
type = (E_Player_Type)ReadInt(bytes, ref index);
//自定义类型
player = ReadData<PlayerData>(bytes, ref index);
return index - beginIndex;
}
public override byte[] Writing()
{
//固定内容
int index = 0;
byte[] bytes = new byte[GetByteNum()];
//可变的 是根据成员变量来决定如何拼接的
//存储 list的长度
WriteShort(bytes, (short)list.Count, ref index);
for (int i = 0; i < list.Count; i++)
{
WriteInt(bytes, list[i], ref index);
}
//存储 dic
//先长度
WriteShort(bytes, (short)dic.Count, ref index);
foreach (int key in dic.Keys)
{