【问题标题】:Hololens Shared environment custommessageHololens 共享环境自定义消息
【发布时间】:2017-08-14 14:50:55
【问题描述】:

我正在开发共享环境,并且我已成功将消息从用户 a 发送到 b。现在我正在尝试发送改变游戏对象位置的向量,例如人 a 移动一个立方体,人 b 看到运动。这目前对我不起作用。消息已发送但未收到。我对此进行了大量测试。我使用 microsoft Hololens academy 共享环境库。

我将发布一个有效的代码示例。之后是当前无法正常工作的代码示例。工作代码:

首先,我将回调方法订阅到 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);
    }
}

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

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

所以这里是广播发送但未接收到的类的代码。如果一个人移动一个游戏对象。其他 Hololense 没有收到更改。

将方法订阅到messageID:

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

subscibed 方法,目前只是使用destroy来测试是否发生,destroy已经单独测试并且有效:

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

欢迎所有建议

【问题讨论】:

    标签: c# unity3d callback server hololens


    【解决方案1】:

    我们为同样的问题苦苦挣扎,终于找到了解决方案。

    添加:

    msg.ReadInt64();
    

    到接收消息的方法,所以整个事情看起来像这样:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-18
      • 1970-01-01
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多