Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

The Image message subscribed in ROS is displayed as red question marks in Unity

Discussion in 'Robotics' started by ThrPanEE, Jan 13, 2023.

  1. ThrPanEE

    ThrPanEE

    Joined:
    May 30, 2022
    Posts:
    1
    In fact, I subscribed to the topic type of /sensor_msgs/Image in ROS and displayed on the Texture in Unity, but the result is displayed as a red question mark. I have tried to load image data with different APIs, but that has no effect. I want to know what I should do?
    Here is my code:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Robotics.ROSTCPConnector;
    5. using RosMessageTypes.Sensor;
    6.  
    7. public class MainCameraTopicSubscriber : MonoBehaviour
    8. {
    9.     public MeshRenderer meshRenderer;
    10.     private Texture2D texture2D;
    11.     private byte[] image_Data;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         texture2D = new Texture2D(2, 2);
    17.      
    18.         meshRenderer.material = new Material(Shader.Find("Standard"));
    19.         ROSConnection.GetOrCreateInstance().Subscribe<ImageMsg>("/dji_osdk_ros/main_camera_images", ImageInput);
    20.     }
    21.  
    22.     void ImageInput(ImageMsg imageMsg)
    23.     {
    24.         image_Data = imageMsg.data;
    25.         texture2D.LoadImage(image_Data);
    26.         meshRenderer.material.SetTexture("_MainTex", texture2D);
    27.     }
    If anyone has a good solution, please tell me in time, thank you!!