Why Getting Video from server to Raw image Component in unity doesn't work?

984 views Asked by At

I am trying to get a video from my 'Json' file which I upload it on azure server to Raw image component in unity but it looks like That : enter image description here

to get a picture it works well but for video mp4 it doesn't, this is the Code :

using UnityEngine ;
using UnityEngine.Networking ;
using UnityEngine.UI ;
using System.Collections ;

// Json data format
/*
      {
        "Name"     : "..." ,
        "VideoURL" : "..."
      }
*/
public struct Data {
   public string Name ;
   public string VideoURL ;
}

public class Demo : MonoBehaviour {
   [SerializeField] Text uiNameText ;
   [SerializeField] RawImage uiRawImage ;

   string jsonURL = "https://myserver..." ;

   void Start () {
      StartCoroutine (GetData (jsonURL)) ;
   }

   IEnumerator GetData (string url) {
      UnityWebRequest request = UnityWebRequest.Get (url) ;

      yield return request.SendWebRequest() ;

      if (request.isNetworkError || request.isHttpError) {
         // error ...

      } else {
         // success...
         Data data = JsonUtility.FromJson<Data> (request.downloadHandler.text) ;

         // print data in UI
         uiNameText.text = data.Name ;

         // Load video:
         StartCoroutine (GetVideo (data.VideoURL)) ;
      }
      
      // Clean up any resources it is using.
      request.Dispose () ;
   }

   IEnumerator GetVideo (string url) {
      UnityWebRequest request = UnityWebRequest.GetTexture (url) ;

      yield return request.SendWebRequest() ;

      if (request.isNetworkError || request.isHttpError) {
         // error ...

      } else {
         //success...
         uiRawImage.texture = ((DownloadHandlerTexture)request.downloadHandler).texture ;
      }

      // Clean up any resources it is using.
      request.Dispose () ;
   }

}

So this Code load well an Image to my unity but for video it doesn't, I tried to get all type of videos that unity support but same thing. so to Raw Image component I add video player component which i add a render texture to it and to image raw but also same thing. my unity version is 2019.4.26f1. it can be because of unity's version? I hope someone can help me really.

1

There are 1 answers

1
derHugo On

Well the reason is quite simple:

  • Image == Texture -> yes
  • Video == Texture -> Nope!

You can not directly assign a video to a RawImage component which is for a Texture

You also can not download a video using UnityWebRequest.GetTexture in the first place.

Btw that API is from Unity 5.6 .. this was years ago and I strongly recommend you port your project to an up-to-date version.

If you are really using Unity 2019.4 as you say than it would be UnityWevRequestTexture.GetTexture instead.


You basically said yourself what you have to do:

These steps you can already do in edit mode. You could also do it once on startup like e.g.

[SerializeField] private VideoPlayer _videoPlayer;
[SerializeField] private RenderTexture _renderTexture;
[SerializeField] private RawImage _rawImage;

private void Awake ()
{
    _rawImage.texture = _renderTexture;
    _videoPlayer.targetTexture = _renderTexture;
}

Then on runtime

so on runtime do e.g.

// Yes, if you make Start return IEnumerator then Unity automatically runs it as a Coroutine
IEnumerator Start () 
{
    // Use "using" instead of manually calling "Dispose"
    using(var request = UnityWebRequest.Get(jsonUrl))
    {
        yield return request.SendWebRequest();

        if (request.isNetworkError || request.isHttpError)
        {
            // error ...
        } 
        else
        {
            // success...
            Data data = JsonUtility.FromJson<Data> (request.downloadHandler.text);

            // print data in UI
            uiNameText.text = data.Name ;

            // The video player will take care of loading the video clip all by itself!
            videoPlayer.url = data.VideoURL;
            videoPlayer.Play();
        }
    }
}