向NetworkManager添加其他服务质量的服务通道模式

时间:2017-10-18 15:53:41

标签: c# unity3d networking

我需要为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 个答案:

答案 0 :(得分:1)

Option Explicit Option Compare Database 'Public Type containing the geocoding of the postal address Public Type tGeocodeResult dLatitude As Double dLongitude As Double sRetAddress As String sAccuracy As String sStatus As String End Type '--------------------------------------------------------------------------------------- ' Procedure : Geocode with Google Geocoding API v3 ' Version : 1.01 ' DateTime : 03/03/2011 ' Author : Philben ' Purpose : converting addresses into geographic coordinates ' Parameter : No mandatory. string format or NULL ' Reference : http://code.google.com/intl/fr-FR/apis/maps/documentation/geocoding/index.html ' Remark : Query limit of 2,500 geolocation requests per day ' : A good accuracy is different of a good geocoding !!! ' : Minimum delay between two queries : >= 200 ms '--------------------------------------------------------------------------------------- Public Function Geocode(Optional ByVal vAddress As Variant = Null, _ Optional ByVal vTown As Variant = Null, _ Optional ByVal vPostCode As Variant = Null, _ Optional ByVal vRegion As Variant = Null, _ Optional ByVal sCountry As String = "UNITED STATES+") As tGeocodeResult On Error GoTo catch Dim oXmlDoc As Object Dim sUrl As String, sFormatAddress As String If Not IsNull(vAddress) Then vAddress = Replace(vAddress, ",", " ") sFormatAddress = (vAddress + ",") & _ (vTown + ",") & _ (vRegion + ",") & _ (vPostCode + ",") & _ sCountry 'To create the URL sUrl = "http://maps.googleapis.com/maps/api/geocode/xml?address=" & sFormatAddress & "&sensor=false" ''XMLDOM to get the XML response Set oXmlDoc = CreateObject("Microsoft.XMLDOM") With oXmlDoc .Async = False If .Load(sUrl) And Not .selectSingleNode("GeocodeResponse/status") Is Nothing Then 'Status code Geocode.sStatus = .selectSingleNode("GeocodeResponse/status").Text 'If a result is returned If Not .selectSingleNode("GeocodeResponse/result") Is Nothing Then 'formatted_address Geocode.sRetAddress = .selectSingleNode("//formatted_address").Text 'Accuracy Geocode.sAccuracy = .selectSingleNode("//location_type").Text 'Latitude and longitude Geocode.dLatitude = Val(.selectSingleNode("//location/lat").Text) Geocode.dLongitude = Val(.selectSingleNode("//location/lng").Text) End If End If End With Set oXmlDoc = Nothing Exit Function catch: Set oXmlDoc = Nothing Err.Raise Err.Number, , Err.Description End Function 是一个只读属性,因为只实现了它的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函数中的代码之前,您必须使用GetComponentAddComponent函数执行此操作,否则会出现运行时错误。