// Update is called once per frame void Update() { if (syncClient != null) { if (syncClient.Running) { while (syncClient.SyncQueue.GetCount() > 0) { byte[] messageBytes = syncClient.SyncQueue.Dequeue(); // 处理消息 LiveMessage msg = LiveMessageManager.ParseMessage(messageBytes); //Debug.Log("msg type=" + msg.type); switch (msg.type) { case LiveMessageConstant.BEV_MESSAGE_TYPE_START: synchronizing = true; break; case LiveMessageConstant.BEV_MESSAGE_TYPE_STOP: synchronizing = false; break; case LiveMessageConstant.BEV_MESSAGE_TYPE_SET_ANCHOR: LiveMessageSetAnchor msgSetAnchor = msg as LiveMessageSetAnchor; // 这里也不能再同步了 synchronizing = false; SetAnchors(msgSetAnchor); // 重置同步消息序号 syncIndex = 0; break; case LiveMessageConstant.BEV_MESSAGE_TYPE_SAVE_ANCHOR: LiveMessageSaveAnchor msgSaveAnchor = msg as LiveMessageSaveAnchor; // 这里也不能再同步了 synchronizing = false; SaveAnchors(msgSaveAnchor); break; case LiveMessageConstant.BEV_MESSAGE_TYPE_DOWNLOAD_ANCHOR: DownloadAnchor(); break; case LiveMessageConstant.BEV_MESSAGE_TYPE_REQUEST_SPATIAL_MAPPING: SendSpatialMapping(); break; } } } } }
/// <summary> /// 将anchor信息初始化到Hololens端 /// </summary> public void SetAnchorToHololens(bool isInit) { if (!hololensConnected) { return; } StratLog(); LiveMessageSetAnchor msg = new LiveMessageSetAnchor(); msg.anchorData = new LiveMessageSetAnchor.LiveMessageSetAnchorData(); msg.anchorData.serverHost = anchorController.serverHost; msg.anchorData.serverPort = anchorController.serverPort; msg.anchorData.appId = anchorController.appId; msg.anchorData.roomId = anchorController.roomId; msg.anchorData.useUDP = this.useUDP; msg.anchorData.serverPortUDP = this.listenPortUDP; msg.anchorData.sendRotation = true; msg.anchorData.logIndex = currentLogIndex; for (int i = 0; i < anchorController.anchorObjectList.Count; i++) { AnchorObjectInfo info = anchorController.anchorObjectList[i]; msg.anchorData.anchorNameList.Add(info.anchorName); msg.anchorData.anchorPosition.Add(info.rootObject.transform.position); msg.anchorData.anchorForward.Add(info.rootObject.transform.eulerAngles); } Debug.Log("Send Set Anchor msg type=" + msg.type); if (handler != null) { handler.SendMessage(msg.Serialize(), null); } waiting = true; waitingString = "Waiting for anchor init..."; }
/// <summary> /// 初始化anchor /// </summary> /// <param name="msgSetAnchor"></param> private void SetAnchors(LiveMessageSetAnchor msgSetAnchor) { //Debug.Log("Init Anchor!"); anchorController.ClearAllAnchorInfo(true); anchorController.serverHost = msgSetAnchor.anchorData.serverHost; anchorController.serverPort = msgSetAnchor.anchorData.serverPort; anchorController.appId = msgSetAnchor.anchorData.appId; anchorController.roomId = msgSetAnchor.anchorData.roomId; this.useUDP = msgSetAnchor.anchorData.useUDP; this.udpPort = msgSetAnchor.anchorData.serverPortUDP; // 开始记录日志 if (currentLogName != null) { Debug.Log("Has Old Log! " + currentLogName); logManager.StopLog(currentLogName); } currentLogName = "SendSync_" + msgSetAnchor.anchorData.logIndex; for (int i = 0; i < msgSetAnchor.anchorData.anchorNameList.Count; i++) { string anchorName = msgSetAnchor.anchorData.anchorNameList[i]; Vector3 pos = msgSetAnchor.anchorData.anchorPosition[i].ToVector3(); Vector3 forward = msgSetAnchor.anchorData.anchorForward[i].ToVector3(); // 创建新anchor GameObject obj = new GameObject(anchorName); obj.transform.position = pos; if (msgSetAnchor.anchorData.sendRotation) { obj.transform.eulerAngles = forward; } else { obj.transform.forward = forward; } //Debug.Log("Add Anchor[" + anchorName + "] at " + pos + " | " + forward); anchorController.AddAnchorObject(anchorName, obj); } anchorController.ShowAllMark(false); if (useUDP) { if (sender != null) { sender.Dispose(); } sender = new UdpSender(udpPort, UdpMode.Unicast, liveIp, null); sender.Init(); } // 设置完毕之后,回传结果给PC SendSetAnchorResult(); }
public static LiveMessage ParseMessage(byte[] bytes) { LiveMessage msg = null; if (bytes.Length == 0) { Debug.LogError("Error: Message length=0!"); return(msg); } switch (bytes[0]) { case LiveMessageConstant.BEV_MESSAGE_TYPE_START: msg = new LiveMessageStart(); break; case LiveMessageConstant.BEV_MESSAGE_TYPE_STOP: msg = new LiveMessageStop(); break; case LiveMessageConstant.BEV_MESSAGE_TYPE_DOWNLOAD_ANCHOR: msg = new LiveMessageDownload(); break; case LiveMessageConstant.BEV_MESSAGE_TYPE_SET_ANCHOR: msg = new LiveMessageSetAnchor(); break; case LiveMessageConstant.BEV_MESSAGE_TYPE_SYNCHRONIZE_ALL: msg = new LiveMessageSynchronizeAll(); break; case LiveMessageConstant.BEV_MESSAGE_TYPE_DOWNLOAD_ANCHOR_FINISH: msg = new LiveMessageDownloadFinish(); break; case LiveMessageConstant.BEV_MESSAGE_TYPE_SET_ANCHOR_FINISH: msg = new LiveMessageSetAnchorFinish(); break; case LiveMessageConstant.BEV_MESSAGE_TYPE_SAVE_ANCHOR: msg = new LiveMessageSaveAnchor(); break; case LiveMessageConstant.BEV_MESSAGE_TYPE_SAVE_ANCHOR_FINISH: msg = new LiveMessageSaveAnchorFinish(); break; case LiveMessageConstant.BEV_MESSAGE_TYPE_REQUEST_SPATIAL_MAPPING: msg = new LiveMessageRequestSpatialMapping(); break; case LiveMessageConstant.BEV_MESSAGE_TYPE_RESPONSE_SPATIAL_MAPPING: msg = new LiveMessageResponseSpatialMapping(); break; default: Debug.LogError("No such type message!"); return(msg); } try { msg.Deserialize(bytes); } catch (Exception e) { msg = null; Debug.LogError("Parse Message Error! " + e); } return(msg); }