Load RawImage in Unity from byte data

413 Views Asked by At

I am receiving byte data via an Udp connection in Unity, I want to render the byte data as rawImage in unity. If I try to load image from a local disk, I am able to render the image.

In the below code, If I run the uncommented code where I am reading data from local disk, I am able to render the image.

If I run the code using Udp client, I am not able to render the image. I have checked the byte data received by converting that to a string and I am able to see the data.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Text;
using System.Net;
using System.Net.Sockets;

public class Image : MonoBehaviour  
{
    // Start is called before the first frame update
    public RawImage image;
    UdpClient client;
    private RenderTexture renderTexture;
    float interval = 0.05f;
    float nextTime = 0;
    private int count;
    void Start()
    {
        count = 0;
        client = new UdpClient(8000);

    }


 
    //Update is called once per frame
    void Update()
    {

        //if (Time.time >= nextTime)
        //{
        //    Texture2D texture = new Texture2D(2, 2);
        //    string path = "C:/Users/Desktop/frames20/frame_" + count.ToString() + ".jpg";
        //    byte[] imageData = File.ReadAllBytes(path);
        //    texture.LoadImage(imageData);
        //    image.texture = texture;
        //    Debug.Log(path);
        //    //Destroy(texture);
        //    nextTime += interval;
        //    count += 1;
        //}

        if (client.Available > 0)
        {
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
            byte[] data = client.Receive(ref endPoint);
            string text = Encoding.UTF8.GetString(data);
            Debug.Log(text);
            if (data != null)
            {
                Texture2D texture = new Texture2D(2, 2);
                texture.LoadImage(data);
                image.texture = texture;
            }
        }

    }
}

1

There are 1 best solutions below

1
Morion On

Check the length of the data you are trying to show. Perhaps it is not the whole image, but only different parts of the full data in every frame. You can easily check it by adding Debug.Log to your if(data != null) section.

Also, UDP seems not a very good option for sending such data. Why do you use it?