- Home /
Adding movie texture at runtime
Hi Unity Community
I’m trying to add a MovieTexture to a UI Raw Image component at runtime using the WWW class but it is not adding to the texture; anybody know where I am going wrong?
Many thanks in advance
public function ContextVidImp(){
Debug.Log("Context Img Located at: " + texLocation);
var wwwDirectory = "file://" + texLocation;
var www : WWW = new WWW(wwwDirectory);
var videoTex = www.movie;
while(!videoTex.isReadyToPlay){
yield www;
Debug.Log("Video Progress: " + www.progress);
if (videoTex.isReadyToPlay){
Debug.Log("Video is ready to play");
break; //if done downloading image break loop
}
}
//contVid = contextVid.GetComponent(RawImage);
var contVidTex = contextVid.GetComponent(RawImage).texture as MovieTexture;
contVidTex = www.movie;
contVidTex.Play();
Weird that this went to moderation; I thought posts only went to moderation if you have below 15 reputation on this site?
Anyhow, I have made progress with this issue after reading that the www.movie.isReadToPlay is really only applicable to strea$$anonymous$$g assets and not from file I changed this to www.isDone (this isn't really ideal for larger video files I intend to account for, so what are my other options?). However this still returned a blank texture.
I also read that Unity prefers the .ogg format so after a trial in this format I have got the script to work. Does anyone know how this could be extended to account for other video codecs/forrmats? Say .mp4, .avi and other Quicktime codecs? The .ogg formats is not very appropriate for my use case.
Updated code included below
public function ContextVidImp(){
Debug.Log("Context Img Located at: " + texLocation);
var wwwDirectory = "file://" + texLocation;
var www : WWW = new WWW(wwwDirectory);
var videoTex = www.movie;
while(!www.isDone){
yield www;
Debug.Log("Done Downloading Texture");
if (www.isDone){
break; //if done downloading image break loop
}
}
contVid.texture = videoTex;
var contVidTex = contVid.texture as $$anonymous$$ovieTexture;
Debug.Log("Ready to play");
contVidTex.Play();