/// <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; } } } }