void PlayerMoveHandler(Codecs cs, byte[] data) { MoveProto proto = cs.Decode <MoveProto>(data); if (proto.id == player.GetComponent <Player>().GetID()) { return; } string line = "[" + proto.id + "] move " + proto.x.ToString() + ", " + proto.z.ToString(); print(line); if (!_players.ContainsKey(proto.id)) { GameObject e = Instantiate(preplayer, new Vector3(proto.x, 0, proto.z), Quaternion.identity); e.GetComponent <Renderer>().material.color = RandomColor(); _players.Add(proto.id, e); Transform PlayerMapIconTransform = e.transform.GetChild(0); PlayerMapIconTransform.GetComponent <Renderer>().material.color = Color.red; } else { GameObject e = _players[proto.id]; Rigidbody rg = e.GetComponent <Rigidbody>(); rg.MovePosition(new Vector3(proto.x, 0, proto.z)); e.GetComponent <Player>().SetScore(proto.score); } }
void LateUpdate() { MoveProto proto = new MoveProto(); proto.id = player.GetComponent <Player>().GetID(); proto.x = player.transform.position.x; proto.z = player.transform.position.z; proto.score = player.GetComponent <Player>().GetScore(); _net.Send <MoveProto>(proto); }
// 处理接收到的消息 public void OnReceiveData() { // 半包的先不处理 if (readbuff.Length <= 2) { return; } Int16 bodyLen = BitConverter.ToInt16(readbuff.bytes, 0); //UILog.log.Add("收到信息长度:" + bodyLen); if (readbuff.Length < 2 + bodyLen) { return; } // 处理消息 string message = Encoding.UTF8.GetString(readbuff.bytes, 2, bodyLen); readbuff.readIdx += 2 + bodyLen; readbuff.CheckAndMoveBytes(); Int16 typeLen = BitConverter.ToInt16(Encoding.UTF8.GetBytes(message), 0); string type = typeLen.ToString(); message = message.Substring(2, message.Length - 2); //UILog.log.Add("收到信息类型:" + ((netEventEnum)(int.Parse(type))).ToString()); string protoType = "Net.Proto." + ((netEventEnum)(int.Parse(type))).ToString() + "Proto"; if ((netEventEnum)(int.Parse(type)) != netEventEnum.Ping) { Console.WriteLine("收到信息类型:" + ((netEventEnum)(int.Parse(type))).ToString()); if ((netEventEnum)(int.Parse(type)) == netEventEnum.Move) { MoveProto mp = Packet.Decode(((netEventEnum)(int.Parse(type))).ToString() + "Proto", message) as MoveProto; pos[0] = mp.x.ToString(); pos[1] = mp.y.ToString(); pos[2] = mp.z.ToString(); } Server.broadcast((netEventEnum)(int.Parse(type)), message); // 广播 } else { lastPingTime = Tool.GetTimestamp(); PongProto pp = new PongProto(); pp.Timestamp = Tool.GetTimestamp(); Send(netEventEnum.Pong, Encoding.UTF8.GetString(Packet.Encode(pp))); } // 粘包的继续解析 if (readbuff.Length > 2) { OnReceiveData(); } }
private void FixedUpdate() { if (isUpdateMove) { MoveProto mp = new MoveProto(); mp.id = NetDispose.id; mp.Timestamp = Tool.GetTimestamp(); mp.x = (int)(transform.position.x * 10000); mp.y = (int)(transform.position.y * 10000); mp.z = (int)(transform.position.z * 10000); isUpdateMove = false; string str = JsonUtility.ToJson(mp); NetManager.Instance.Send(netEventEnum.Move, str); } }
void Update() { NetManager.Update(); // 临时按键控制 if (Input.GetKeyDown(KeyCode.X)) { net.Close(); } if (Input.GetKeyDown(KeyCode.A)) { var mp = new MoveProto(); System.Random ran = new System.Random(); mp.x = ran.Next(-10, 10); mp.id = id; mp.Timestamp = Tool.GetTimestamp(); string str = JsonUtility.ToJson(mp); net.Send(netEventEnum.Move, str); } }