Hololens共享环境custommessage

时间:2017-03-22 13:06:14

标签: c# unity3d callback server hololens

我正在共享环境中工作,并且我已经成功地从用户a发送消息到b。现在我试图发送改变游戏对象位置的向量,例如,人移动立方体,人b看到移动。目前这对我不起作用。消息已发送但未收到。我测试了很多。我使用微软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);
    }
}

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

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

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

将方法订阅到messageID:

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

subcibed方法,目前只是使用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);
    }
}

欢迎所有建议

0 个答案:

没有答案