2017-03-22 75 views
1

我正在处理共享环境,并成功地将消息从用户a发送到b。现在我试图发送改变游戏物体位置的向量,例如人a移动立方体,人物b看到移动。这目前不适合我。该消息已发送但未收到。我严重测试了这个。我使用微软Hololens学院共享环境库。Hololens共享环境自定义消息

我会发布一个有效的代码示例。之后,一个目前无法工作的代码示例。工作代码:

首先我订阅回调方法的MESSAGEID有:

protected override void Start() 
{ 
    base.Start(); 
    CustomMessages.Instance.MessageHandlers[CustomMessages.TestMessageID.StartMachine] = this.startMachine; 
} 

的订阅方法:

private void startMachine(NetworkInMessage msg) 
{ 
// do something 
    Act(); 
} 

当按下按钮调用此方法,这反过来广播这个信息通过调用一个静态类的方法。

protected override void PerformManipulationStart(Vector3 position) 
{ 
    //broadcasting message 
    CustomMessages.Instance.SendStartMachine(); 
    //the below statement is some local action 
    MachineMovement.Instance.StartMachine(); 
} 

是广播

public void SendStartMachine() 
{ 
    // If we are connected to a session, broadcast our head info 
    if (this.serverConnection != null && this.serverConnection.IsConnected()) 
    { 
     // Create an outgoing network message to contain all the info we want to send 
     NetworkOutMessage msg = CreateMessage((byte)TestMessageID.StartMachine); 

     // Send the message as a broadcast, which will cause the server to forward it to all other users in the session. 
     this.serverConnection.Broadcast(
      msg, 
      MessagePriority.Immediate, 
      MessageReliability.Reliable, 
      MessageChannel.Avatar); 
    } 
} 

我在测试消息枚举之前发现了一个错误的方法。如果您将某些东西放在最大值以下,则不会广播该消息。我将UpdateMachinePosition放置在多个位置,但仍未调用。所有其他testmessage ID的工作。

public enum TestMessageID : byte 
{ 
    HeadTransform = MessageID.UserMessageIDStart, 
    UserAvatar, 
    UserHit, 
    ShootProjectile, 
    StageTransform, 
    ResetStage, 
    ExplodeTarget, 
    StartMachine, 
    UpdateMachinePosition, 
    StopMachine, 
    Alarm, 
    Mash, 
    Max 
} 

所以这里是广播发送但没有收到的类的代码。如果移动一个游戏对象。其他的英雄没有收到这个变化。

订阅的方法来邮件ID:

protected override void Start() { 
    base.Start(); 
    rb = GetComponent<Rigidbody>(); 
    //subscribed in the line below. 
    CustomMessages.Instance.MessageHandlers[CustomMessages.TestMessageID.UpdateMachinePosition] = this.RemoteMachinePosition; 
} 

的subscibed方法,目前只使用破坏测试,如果有事情发生,破坏已seperately测试和工程:

void RemoteMachinePosition(NetworkInMessage msg) 
{ 
    Destroy(totransform.gameObject); 

} 

的方法做当本地调用时。再次,我已经测试了几次,该消息被发送:

public override void performAction() 
{ 
     // 4.a: Calculate the moveVector as position - manipulationPreviousPosition. 
     moveVector = (ManipulationManager.Instance.ManipulationPosition - manipulationPreviousPosition) *2; 

     // 4.a: Update the manipulationPreviousPosition with the current position. 
     manipulationPreviousPosition = ManipulationManager.Instance.ManipulationPosition; 

     // 4.a: Increment this transform's position by the moveVector. 
     totransform.position += moveVector; 
     // calling custommessage class to broadcast. 
     CustomMessages.Instance.SendMachineTransform(moveVector, name); 
} 

呼吁CUSTOMMESSAGE类中的方法:

public void SendMachineTransform(Vector3 Movement, string Name) 
{ 
    // If we are connected to a session, broadcast our head info 
    if (this.serverConnection != null && this.serverConnection.IsConnected()) 
    { 
     // Create an outgoing network message to contain all the info we want to send 
     NetworkOutMessage msg = CreateMessage((byte)TestMessageID.UpdateMachinePosition); 


     // Send the message as a broadcast, which will cause the server to forward it to all other users in the session. 
     this.serverConnection.Broadcast(
      msg, 
      MessagePriority.Immediate, 
      MessageReliability.Reliable, 
      MessageChannel.Avatar); 
    } 
} 

所有建议,欢迎

回答

1

我们有同样的问题,最后挣扎找到了解决方案。

添加:

msg.ReadInt64(); 

要在被接收消息所以整个东西看起来像,该方法:

void RemoteMachinePosition(NetworkInMessage msg) 
{ 
    msg.ReadInt64(); 
    Destroy(totransform.gameObject); 
} 

该消息被以字节为单位发送。您首先阅读的是用户标识,而不是消息本身。添加readint行后,它应该跳过用户ID并接收其余的消息。