C# 将大约1mb的RawTextureData从客户端发送到服务器

C# 将大约1mb的RawTextureData从客户端发送到服务器,c#,unity3d,networking,C#,Unity3d,Networking,我正在编写一个测试脚本,它可以向我的服务器发送各种内容,包括一个当前约700k的图像字节数组。因此,我得到了一个错误: NetworkWriter WriteBytes:缓冲区太大(699064)字节。最大缓冲区大小为64K字节。 UnityEngine.Networking.NetworkWriter:WriteBytes完整(字节[]) 播放器数据:CallCmdSendServerData(字节[],Int32,Int32,字符串,字符串,Int32,Int32) c__迭代器0:Move

我正在编写一个测试脚本,它可以向我的服务器发送各种内容,包括一个当前约700k的图像字节数组。因此,我得到了一个错误:

NetworkWriter WriteBytes:缓冲区太大(699064)字节。最大缓冲区大小为64K字节。 UnityEngine.Networking.NetworkWriter:WriteBytes完整(字节[]) 播放器数据:CallCmdSendServerData(字节[],Int32,Int32,字符串,字符串,Int32,Int32) c__迭代器0:MoveNext()(位于Assets/Scripts/Player_Data.cs:45)

我已经开发了一个脚本,可以发送到通道2,其中相应的通道在NetworkManager中设置为可靠的分段。我意识到,仅仅设置通道并不能使大数据块的所有操作都能进行,但需要进行其他编程层来移动这些数据,但我不确定如何准确地完成。如何将这种大小的数据从客户端移动到服务器?谢谢大家!

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.Collections;

[NetworkSettings(channel = 2, sendInterval = 1.0f)]

public class Player_Data : NetworkBehaviour
{
    public Texture2D tex;
    public string type; 
    public string id;
    public int strength;
    public int hitpoints;

    private byte[] texBytes;
    private int texWidth;
    private int texHeight;


    private void Start()
    {
        if (isLocalPlayer && !isServer)
        {
            SendData(); // Convert raw texture to jpg byte array
        } 
    }

    [Client]
    void SendData()
    {
        texWidth = tex.width;
        texHeight = tex.height;
        type = "Deer";
        strength = 10;
        hitpoints = 120;

        StartCoroutine(DoSendData());
    }
    IEnumerator DoSendData()
    {
        yield return new WaitForEndOfFrame();
        texBytes = tex.GetRawTextureData();

        CmdSendServerData(texBytes, texWidth, texHeight, type, id, strength, hitpoints);
    }

    [Command]
    void CmdSendServerData(byte[] texBytes, int tWidth, int tHeight, string type, string id, int strength, int hitpoints)
    {
        GameObject infoDisplayText = GameObject.Find("InfoDisplay");
        infoDisplayText.GetComponent<Text>().text += "Image size : " + texBytes.Length.ToString() + "\n";
        infoDisplayText.GetComponent<Text>().text += "Image width : " + tWidth.ToString() + "\n";
        infoDisplayText.GetComponent<Text>().text += "Image height : " + tHeight.ToString() + "\n";
        infoDisplayText.GetComponent<Text>().text += "Animal type : " + type + "\n";
        infoDisplayText.GetComponent<Text>().text += "Animal strength : " + strength + "\n";
        infoDisplayText.GetComponent<Text>().text += "Animal hit points : " + hitpoints + "\n";

        RpcDataReceivedConfirmation();
    }

    [ClientRpc]
    void RpcDataReceivedConfirmation()
    {
        if (isLocalPlayer)
        {
            DisplayDataOnClientScreen("CmdSendServerData Received Confirmation!");
        }   
    }

    [Client]
    void DisplayDataOnClientScreen(string data)
    {
        GameObject infoDisplayText = GameObject.Find("InfoDisplay");
        infoDisplayText.GetComponent<Text>().text += data + "\n";
    }
}
使用UnityEngine;
使用UnityEngine.UI;
使用UnityEngine。联网;
使用系统集合;
[网络设置(通道=2,发送间隔=1.0f)]
公共类玩家数据:网络行为
{
公共纹理2d-tex;
公共字符串类型;
公共字符串id;
公共综合实力;
公共int生命点;
专用字节[]个字节;
私有宽度;
私家车高度;
私有void Start()
{
if(isLocalLayer&&!isServer)
{
SendData();//将原始纹理转换为jpg字节数组
} 
}
[客户]
void SendData()
{
texWidth=tex.width;
tex高度=tex高度;
type=“鹿”;
强度=10;
生命值=120;
start例程(DoSendData());
}
IEnumerator DoSendData()
{
返回新的WaitForEndOfFrame();
texBytes=tex.GetRawTextureData();
CmdSendServerData(texBytes、texWidth、texHeight、类型、id、强度、生命值);
}
[命令]
void CmdSendServerData(字节[]texBytes,整数tWidth,整数thight,字符串类型,字符串id,整数强度,整数生命点)
{
GameObject infoDisplayText=GameObject.Find(“InfoDisplay”);
infoDisplayText.GetComponent().text+=“图像大小:”+texBytes.Length.ToString()+“\n”;
infoDisplayText.GetComponent().text+=“图像宽度:”+tWidth.ToString()+“\n”;
infoDisplayText.GetComponent().text+=“图像高度:”+tHeight.ToString()+“\n”;
infoDisplayText.GetComponent().text+=“动物类型:”+type+“\n”;
infoDisplayText.GetComponent().text+=“动物力量:”+strength+“\n”;
infoDisplayText.GetComponent().text+=“动物生命值:”+生命值+“\n”;
RPCDataReceivedConfiguration();
}
[客户端RPC]
void rpcdatareceivedconfimation()
{
if(IsLocalLayer)
{
DisplayDataOnClientScreen(“CmdSendServerData已收到确认!”);
}   
}
[客户]
void DisplayDataOnClientScreen(字符串数据)
{
GameObject infoDisplayText=GameObject.Find(“InfoDisplay”);
infoDisplayText.GetComponent().text+=数据+“\n”;
}
}

对UNet施加这种限制真是太糟糕了。这就是为什么我使用原始套接字来完成我所有的网络工作,我根本不浪费时间在UNet上。无论如何,将字节剪切成多个“是”,然后使用
NetworkServer.SendBytesToPlayer
NetworkConnection.SendBytes
发送它们。这应该行。@Programmer您知道有什么好的资源可以让我了解这个过程吗?@Programmer正在使用原始套接字作为一个全功能命题,或者我可以使用它来发送这个特定的数据吗?我的大部分工作还可以使用networkmanager吗?是的,你可以使用原始套接字发送大数据,然后使用普通的Unity网络API(例如networkmanager)处理你的其他网络内容。对UNet施加这种限制真糟糕。这就是为什么我使用原始套接字来完成我所有的网络工作,我根本不浪费时间在UNet上。无论如何,将字节剪切成多个“是”,然后使用
NetworkServer.SendBytesToPlayer
NetworkConnection.SendBytes
发送它们。这应该行。@Programmer您知道有什么好的资源可以让我了解这个过程吗?@Programmer正在使用原始套接字作为一个全功能命题,或者我可以使用它来发送这个特定的数据吗?我的大部分工作还可以使用networkmanager吗?是的,您可以使用原始套接字发送大数据,然后使用普通的Unity网络API(例如networkmanager)处理其他网络内容。