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.
Well the reason is quite simple:
You can not directly assign a video to a
RawImagecomponent which is for aTextureYou also can not download a video using
UnityWebRequest.GetTexturein 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.GetTextureinstead.You basically said yourself what you have to do:
VideoPlayercomponentRenderTextureto theVideoPlayer.targetTextureso the video player renders the video into that textureRenderTexturein theRawImage.textureThese steps you can already do in edit mode. You could also do it once on startup like e.g.
Then on runtime
VideoPlayer.urlso on runtime do e.g.