protected void Network_SendCameraData() { if (UdpClient.IsExisted == false || UdpClient.Instance.Connected == false || marker == null) { return; } Vector2 pos = MathUtil.Screen2Normalize(marker.position); string dataJson = JsonUtility.ToJson(new CameraData() { cameraPos = new Vector3(pos.x, heightSlider.value, pos.y), cameraEuler = new Vector3(-camViewDrag.rotateAngle, marker.angle, 0) }); CmdPackage cmd = new CmdPackage() { name = "Camera", data = dataJson, }; //Debug.Log(JsonUtility.ToJson(cmd)); UdpClient.Instance.SocketSend(JsonUtility.ToJson(cmd)); }
/// <summary> /// 从byte数组解析出CmdPackage /// </summary> /// <param name="data"></param> /// <param name="start"></param> /// <param name="cmdLen"></param> /// <param name="msg"></param> /// <returns></returns> public static bool UnpackProtobuf(byte[] data, int start, int cmdLen, out CmdPackage msg) { msg = new CmdPackage { serviceType = data.ReadUShortLe(start), cmdType = data.ReadUShortLe(start + 2) }; var bodyLen = cmdLen - HeadSize; msg.body = new byte[bodyLen]; Array.Copy(data, start + HeadSize, msg.body, 0, bodyLen); return(true); }
//处理接收到的消息 void OnCmdReceive(string cmdJson) { //Debug.Log("Cmd Json = " + cmdJson); if (string.IsNullOrEmpty(cmdJson)) { return; } try { CmdPackage cmd = JsonUtility.FromJson <CmdPackage>(cmdJson); if (cmd == null) { return; } switch (cmd.name) { case "Camera": HandleCamera(JsonUtility.FromJson <CameraData>(cmd.data)); break; case "Menu": HandleMenu(JsonUtility.FromJson <MenuData>(cmd.data)); break; case "Slider": HandleSlider(JsonUtility.FromJson <SliderData>(cmd.data)); break; default: break; } } catch (System.Exception e) { Debug.LogException(e); } }
protected void Network_SendMenuData() { if (UdpClient.IsExisted == false || UdpClient.Instance.Connected == false || marker == null) { return; } string dataJson = JsonUtility.ToJson(new MenuData() { menuName = uiName, selectedItemIndex = menu.currentItemIndex }); CmdPackage cmd = new CmdPackage() { name = "Menu", data = dataJson, }; //Debug.Log(JsonUtility.ToJson(cmd)); UdpClient.Instance.SocketSend(JsonUtility.ToJson(cmd)); }
protected void NetWork_SendSliderData() { if (UdpClient.IsExisted == false || UdpClient.Instance.Connected == false || marker == null) { return; } string dataJson = JsonUtility.ToJson(new SliderData() { sliderName = uiName, sliderValue = radialSlider.rawValue01 }); CmdPackage cmd = new CmdPackage() { name = "Slider", data = dataJson, }; //Debug.Log(JsonUtility.ToJson(cmd)); UdpClient.Instance.SocketSend(JsonUtility.ToJson(cmd)); }