/// <summary> /// 发送 /// </summary> void SocketSend() { //清空发送缓存 sendData = new byte[1024]; //数据类型转换 dpsend.set(transform); dpsend.HP = MPlayerInfo.getins().hp; sendData = ConvertHelper.StructureToByte(dpsend); //发送给指定服务端 sock.SendTo(sendData, clientEnd); }
/// <summary> /// 接收 /// </summary> void SocketReceive() { //进入接收循环 while (true) { //对data清零 recvData = new byte[1024]; //获取客户端,获取客户端数据,用引用给客户端赋值 recvLen = socket.ReceiveFrom(recvData, ref ep); //接收到一个包 dpget = ((DgramPlayer)ConvertHelper.ByteToStructure(recvData)); if (dpget.IP == MyIP) { continue; } else if (PlayerList.ContainsKey(dpget.IP)) { //伤害数据包 if (dpget.damage != 0 && MyIP == dpget.damageIP) { MPlayerInfo.getins().damage(dpget.damage); } else { //解析 OtherPlayer temp = PlayerList[dpget.IP]; GameObject p = temp.gobj; temp.pos = dpget.getpos(); temp.rot = dpget.getrot(); temp.speed = dpget.speed; //hp temp.UpdateHp(dpget.HP); //gun temp.GunManage(dpget.getgunrot(), dpget.shooting); } } else { //加入 //由于主线程才能使用unity的api,把添加用户的逻辑交给update; lock (currentjoin) { currentjoin.join = true; currentjoin.IP = dpget.IP; } } } }
public MPlayerInfo() { ins = this; //bloodpercent = new OneArg<float>(1f); }
// Use this for initialization void Start() { Info = MPlayerInfo.getins(); MPlayerInfo.getins().Revive += revive; }