Important News

We have released Shinobi Life Online Pre-Alpha Version 1.1.0.0! This update features Earth Release: Earth Dome Jutsu, Aiming Mode and more! Try it out and tell us what you think.

User

Welcome, Guest.
Please login or register.
 
 
 
Forgot your password?

Discord

Statistics

Members
Total Members: 64472
Latest: best tadalafil prices
New This Month: 341
New This Week: 17
New Today: 6
Stats
Total Posts: 76983
Total Topics: 22498
Most Online Today: 1099
Most Online Ever: 4232
(January 14, 2020, 07:47:33)
Users Online
Members: 0
Guests: 988
Total: 995
Google (7)

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - peterp

Pages: [1]
1
Programming / Downloading image data from server to client
« 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
}

Pages: [1]