예제 #1
0
    public async void SendTextureToServer()
    {
        var localPlayer = PlayerBehaviour.localPlayer;
        var texture     = _drawable.texture;

        byte[] bytes              = LZMA.Compress(texture.GetRawTextureData());
        int    buffer             = System.DateTime.Now.GetHashCode();
        int    byteLimit          = Mathf.Min(Transport.activeTransport.GetMaxPacketSize(), 16384) - 100;
        int    bytesLength        = bytes.Length;
        int    fullBatches        = bytesLength / byteLimit;
        int    finalBatchLength   = bytesLength % byteLimit;
        var    connectionToServer = PlayerBehaviour.localPlayer.connectionToServer;

        localPlayer.CmdBufferCreate(buffer, bytesLength, $"Transfer #{buffer} of {bytesLength} bytes in {fullBatches+(finalBatchLength!=0?1:0)} batches started");
        {
            for (int batch = 0; batch <= fullBatches; batch++)
            {
                int    batchOffset = batch * byteLimit;
                int    batchLength = batch != fullBatches ? byteLimit : finalBatchLength;
                byte[] batchBytes  = new byte[batchLength];
                System.Array.Copy(bytes, batchOffset, batchBytes, 0, batchLength);

                bool sent = false;
                while (sent == false)
                {
                    try
                    {
                        localPlayer.CmdBufferSendBytes(buffer, batchBytes, batchOffset);
                        sent = true;
                    }
                    catch (System.Exception ex)
                    {
                        sent = false;
                        Debug.LogException(ex);
                        await Task.Delay(50);
                    }
                    await Task.Delay(5);
                }
            }
        }
        localPlayer.CmdBufferCreateTexture(buffer, texture.width, texture.height, texture.format, texture.mipmapCount);
        localPlayer.CmdBufferClose(buffer, $"Transfer #{buffer} closed");
    }
    public void CmdBufferCreateTexture(int buffer, int width, int height, TextureFormat textureFormat, int mipmapCount)
    {
        #if DEBUG
        //Debug.Log($"{nameof(CmdBufferCreateTexture)} called");
        UnityEngine.Assertions.Assert.IsTrue(rawDataBuffers.ContainsKey(buffer), $"{nameof(rawDataBuffers)} contains no '{buffer}' buffer. Existing buffers: {JsonUtility.ToJson(rawDataBuffers.Keys)}");
        #endif

        //create texture from bytes:
        Texture2D tex = new Texture2D(width, height, textureFormat, mipmapCount, false);
        tex.LoadRawTextureData(LZMA.Decompress(rawDataBuffers[buffer]));
        tex.Apply();

        //save texture to file:
        tex.EncodeToPNG().WRITE_FILE(
            IO.Path.Combine(IO.Path.GetDirectoryName(Application.dataPath), $"{buffer}.png")
            );

        //downsize texture to save memory:
        tex.ScaleTexture8bit(256, 256, false);

        //instantiate scene object:
        var instance = Instantiate(_imagePrefab, FindObjectOfType <ServerBehaviour>().gallery.transform);
        instance.rawImage.texture = tex;
    }