Sending Bytes Using Networking (Mirror) SOLVED

Hi,

I’m totally stuck with how to accomplish sending images from a client to be seen by all other clients on the server. I’m using Mirror and it’s completely new to me as I’m still trying to wrap my head around how I can get the desired effect. Here’s what I have so far:

foreach (string card in FilePaths)
        {

            Cards = Instantiate(CardPrefab);
            byte[] bytes = File.ReadAllBytes(card);
            Texture2D texture = new Texture2D(1, 1);
            texture.LoadImage(bytes);
            CardFace = Cards.GetComponent<Image>();
            CardFace.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
            Cards.GetComponent<CardFlipper>().Cardfront = CardFace.sprite;
            Cards.GetComponent<EditorDrag>().enabled = false;
            NetworkServer.Spawn(Cards, connectionToClient);
            RpcLoadCards(Cards, "Loaded");
           
        }

So this code works perfectly for the host, uploading images from a folder of pngs on prefabs of card gameobjects, however it doesn’t show the uploaded cards to the other plays/clients. is there a way to alter the above method to accomplish this? Please keep in mind i’m fairly new to coding, so any example scripts would be greatly appreciated :slight_smile:

I’ve looked at this before and I’m still a bit confused how to execute this. Are you saying I just have to break my current code into commands and rpcs or is there more to it?

I was trying to signal you that it is an asset, which is made and supported by the creator. On its support forum you probably would get better answers than here. Most of the users who knows the package and the author are there.

Right. I’ll post something there then. Thanks.

What does RpcLoadCards do? Any of the changes you make to objects on the host, other than those which would be automatically transferred by NetworkTransform or things like SyncVars, will need to be duplicated with your own code on all the clients or they won’t see the changes.

I’m going to take a guess you will need to actually send the images to all the clients prior to loading them. Are you doing that anywhere else? Unet had some pretty strict message size limits which would make this a hassle. I don’t know if Mirror using the TCP based transport would have those same limits.

RpcLoadCards loads the cards into a deckspace depending on authority. So in other words I would need to use an RpcClient method to update the image for all clients? Would I need to send bytes over the server or is it much simpler than that?

Well you would need to send the image data to all the clients some way. I don’t know how they are generated, so can’t say if there is an easier method.

Currently the images are generated by executing a loop for each file in a directory/folder. the code above reads the string file path, loads the bytes, sticks it onto a texture then applies it to an instantiated playable gameobject. I believe Mirror does have upgrades to utilise TCP based transport, but it’s not in the free version. Is it something worth looking into? The alternative to sending bytes over a server is to have each player have the folder and its contents in their application’s file path, which completely removes any sort of elegance or simplicity of using the app.

So apparently my code already works with mirror, putting the code in Command and copying the upload and application of image in ClientRpc, however the file size is too big and needs to be broken down and sent as chunks (I tried with a 60 byte image I quickly scribbled in paint.net and it worked). I’m looking at this Answers topic as a reference.

update: Silly me, I don’t have to send the bytes in chunks as the Network Manager in the editor has a field to increase/decrease the send/receive byte limit. I increased this to 4,000,000 bytes and it works now haha

Has this field been removed? … I don’t see it.

Hello,
I found it in the transport component.(KCP transport → Allowed max message size)