Shinobi Life Online

Art Category => Programming => Topic started by: peterp on March 07, 2016, 16:33:18

Title: Downloading image data from server to client
Post by: peterp on March 07, 2016, 16:33:18
Syntax is Unity3d C#, networking is UNET.
This is useful for uploading and downloading images, for example custom clan banners etc.

Server side
Code: [Select]
const short DOWNLOAD_FILE = 1337;
void SendImageToClient(string path,int size, int connID)
{
    if (File.Exists(path))
     {
         byte[] fileData = File.ReadAllBytes(path);
         Texture2D tex = new Texture2D(size, size);
         tex.LoadImage(fileData);

          FileMessage fmsg = new FileMessage();
          fmsg.name = path;
          fmsg.size = size;
          fmsg.data = tex.EncodeToPNG();

         NetworkServer.SendToClient(connID, DOWNLOAD_FILE, fmsg);

     }
   
}
public class FileMessage : MessageBase
{
    public string name;
    public byte[] data;
    public int size;
}



[b]Client side[/b]



Code: [Select]
const short DOWNLOAD_FILE = 1337;
void Start()
{
    myClient.RegisterHandler(DOWNLOAD_FILE, OnDownloadFile);
}

void OnDownloadFile(NetworkMessage msg)
{

        FileMessage wmsg = msg.ReadMessage<FileMessage>();

        Texture2D tex = new Texture2D(wmsg.size, wmsg.size);
        tex.LoadImage(wmsg.data);
        //Do something with the downloaded image
}
Title: Re: Downloading image data from server to client
Post by: Fraudulent on March 07, 2016, 16:44:56
This probably means something to you @Vreg
Title: Re: Downloading image data from server to client
Post by: Nas on March 07, 2016, 17:33:22
*using my first year knowledge of coding* Uhmm Yeaa yea not bad
Title: Re: Downloading image data from server to client
Post by: Boruto050202 on March 07, 2016, 20:27:08
My brain hurts ERMAGERD