问题
I'm trying to prototype a piece of networking for a coloring book type application I'm developing that allows users to create an image and then send that image to a server that then takes the image and wraps it around an instantiated mesh. My initial problem in this iteration is buffer size. I'm getting the error:
NetworkWriter WriteBytes: buffer is too large (699064) bytes. The maximum buffer size is 64K bytes.
UnityEngine.Networking.NetworkWriter:WriteBytesFull(Byte[])
TextureMessage:Serialize(NetworkWriter)
UnityEngine.Networking.NetworkServer:SendToAll(Int16, MessageBase)
Server:SendTexture(Texture2D, String) (at Assets/Scripts/Server.cs:32)
Server:SendOnButtonPress() (at Assets/Scripts/Server.cs:19)
UnityEngine.EventSystems.EventSystem:Update()
How do I increase buffer size? My images will be in the 1.5-2mb range. My code so far is as follows:
public class MyMsgType
{
public static short texture = MsgType.Highest + 1;
}
public class TextureMessage : MessageBase
{
public byte[] textureBytes;
public string message; //Optional
}
Server-side code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class Server : MonoBehaviour {
public Texture2D textureToSend;
string messageToSend = "Test Message";
// Use this for initialization
void Start () {
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);
}
}
Client-side code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class Client : MonoBehaviour
{
NetworkClient myClient;
// Use this for initialization
void Start () {
NetworkManager.singleton.StartClient();
Debug.Log("Client Started.");
setupClient();
}
// Create a client and connect to the server port
public void setupClient()
{
//Create new client
myClient = new NetworkClient();
//Register to connect event
myClient.RegisterHandler(MsgType.Connect, OnConnected);
//Register to texture receive event
myClient.RegisterHandler(MyMsgType.texture, OnTextureReceive);
//Connect to server
myClient.Connect("127.0.0.1", 4444);
}
//Called when texture is received
public void OnTextureReceive(NetworkMessage netMsg)
{
TextureMessage msg = netMsg.ReadMessage<TextureMessage>();
//Your Received message
string message = msg.message;
Debug.Log("Texture Messsage " + message);
//Your Received Texture2D
Texture2D receivedtexture = new Texture2D(4, 4);
receivedtexture.LoadRawTextureData(msg.textureBytes);
receivedtexture.Apply();
}
public void OnConnected(NetworkMessage netMsg)
{
Debug.Log("Connected to server");
}
}
回答1:
You need to set up a custom config via setting NetworkManager.connectionConfig to a ConnectionConfig object that you have set up to have a larger packet size. The sample code should get you started:
void Start()
{
ConnectionConfig myConfig = new ConnectionConfig();
myConfig.AddChannel(QosType.Unreliable);
myConfig.AddChannel(QosType.UnreliableFragmented);
myConfig.PacketSize = 1440;
NetworkManager.connectionConfig = myConfig;
}
来源:https://stackoverflow.com/questions/46802301/networkwriter-writebytes-buffer-is-too-large