【问题标题】:Add additional quality of service channel modes to NetworkManager向 NetworkManager 添加额外的服务质量通道模式
【发布时间】:2017-10-18 15:53:41
【问题描述】:

我需要向 NetworkManager 添加一些额外的服务质量通道模式,我正在尝试以这种方式进行:

NetworkManager netMan;

void Start () {
ConnectionConfig cc = new ConnectionConfig();

reliableChannel = cc.AddChannel(QosType.Reliable);
reliableSeqChannel = cc.AddChannel(QosType.ReliableSequenced);
reliableFragChannel = cc.AddChannel(QosType.ReliableFragmented);
unreliableChannel = cc.AddChannel(QosType.Unreliable);
unreliableSeqChannel = cc.AddChannel(QosType.UnreliableSequenced);
cc.PacketSize = 1440;

netMan.connectionConfig = cc;
}

但我收到错误消息:Property or indexer 'NetworkManager.connectionConfig' cannot be assigned to -- it is readonly

如果该属性是只读的,那么为 NetworkManager 创建额外通道的正确方法是什么?

...下面的完整功能

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class Server : MonoBehaviour {

    public Texture2D textureToSend;
    string messageToSend = "Test Message";

    NetworkManager netMan;

    private int reliableChannel;
    private int reliableSeqChannel;
    private int reliableFragChannel;
    private int unreliableChannel;
    private int unreliableSeqChannel;

    // Use this for initialization
    void Start () {

        ConnectionConfig cc = new ConnectionConfig();

        reliableChannel = cc.AddChannel(QosType.Reliable);
        reliableSeqChannel = cc.AddChannel(QosType.ReliableSequenced);
        reliableFragChannel = cc.AddChannel(QosType.ReliableFragmented);
        unreliableChannel = cc.AddChannel(QosType.Unreliable);
        unreliableSeqChannel = cc.AddChannel(QosType.UnreliableSequenced);
        cc.PacketSize = 1440;

        netMan.connectionConfig = cc;

        NetworkManager.singleton.StartHost();
        Debug.Log("Server Started.");
    }

    public void SendOnButtonPress()
    {
        SendTexture(textureToSend, messageToSend);
    }

    //Call to send the Texture and a simple string message
    public void SendTexture(Texture2D texture, string message)
    {
        TextureMessage msg = new TextureMessage();

        //Convert Texture2D to byte array

        msg.textureBytes = texture.GetRawTextureData();
        msg.message = message;

        NetworkServer.SendToAll(MyMsgType.texture, msg);
    }
}

【问题讨论】:

    标签: c# unity3d networking


    【解决方案1】:

    NetworkManager.connectionConfig 是只读属性,因为仅实现了其get{} 属性。 set{} 属性未实现。

    不过,您可以通过以下方式添加更多频道:

    NetworkManager.connectionConfig.AddChannel(QosType).
    

    在你的情况下:

    NetworkManager netMan;
    
    void Start()
    {
        netMan.connectionConfig.AddChannel(QosType.Reliable);
        netMan.connectionConfig.AddChannel(QosType.ReliableSequenced);
        netMan.connectionConfig.AddChannel(QosType.ReliableFragmented);
        netMan.connectionConfig.AddChannel(QosType.Unreliable);
        netMan.connectionConfig.AddChannel(QosType.UnreliableSequenced);
    }
    

    请注意,netMan 尚未初始化。在使用上述Start 函数中的代码之前,您必须先使用GetComponentAddComponent 函数执行此操作,否则会出现运行时错误。

    【讨论】:

      猜你喜欢
      • 2021-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多