2017-10-18 95 views
0

我需要補充的服務渠道模式,NetworkManager的一些額外的質量,我試圖做這樣:添加的服務渠道模式的其他質量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); 
    } 
} 

回答

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函數執行此操作,否則您將收到運行時錯誤。