#Best ways to transfer large pieces of data?

9 messages · Page 1 of 1 (latest)

opaque nymph
#

Hi everyone! I was working on a test where the player can choose a png as a character and a sound to play when they win. However as we know Mirror isn't really intended for large files. So I was wondering how to tackle this issue?

I have read to maybe run some kind of rest server, but I am unsure if this would be "easy" to do since the big plus from something like the steam relay seems to have a Host & Client kind of architecture. Another option I've read about said to transfer X amount of chunks with the data and stitch these together again, but that was not recommended since I heard that could cause network lag.

So I was looking for some pointers, ideas & best practices for transferring a "big" data when taking Mirror into account. Please note that I was planning on not doing this too often. Maybe in a place like a lobby this would happen X amount of times

wind mica
#

If Steam doesn't have a file share service then really the best option is a public time-limited file share service where players can upload and get the URL to pass around for others to download. You only need the files to live for a few minutes or so. Even the free paste bins hold files for at least an hour, if not days.

#

Unity Web Request to push and pull them.

unique knoll
#

Depending on how large your files are, you can just convert them to bytes and transfer over the bytestream to the other client as well

#

I've made a feature before where you would take screenshots on runtime and sync it onto a whiteboard, sending the files was obviously too large for network to handle, but if you convert it to a bytestream it's considerably smaller to the point where a single command can fit it over without issues

opaque nymph
opaque nymph
unique knoll
#
 
using UnityEngine;

public static class TextureConverter
{
    // Convert Texture2D to byte[]
    public static byte[] TextureToBytes(Texture2D texture)
    {
        if (texture == null)
        {
            Debug.LogError("Texture2D is null, cannot convert to byte array.");
            return null;
        }

        // Convert the Texture2D to a PNG byte array
        byte[] byteArray = texture.EncodeToJPG(); // You can use EncodeToJPG() if preferred

        return byteArray;
    }

    // Convert byte[] to Texture2D
    public static Texture2D BytesToTexture(byte[] byteArray)
    {
        if (byteArray == null || byteArray.Length == 0)
        {
            Debug.LogError("Byte array is null or empty, cannot convert to Texture2D.");
            return null;
        }

        // Create a new empty Texture2D
        Texture2D texture = new Texture2D(2, 2); // Size is overridden by LoadImage()

        // Load the byte array into the texture
        texture.LoadImage(byteArray); // Automatically resizes the texture

        return texture;
    }
}```
#

consider this ^^