- Home /
Question by
hongpai · Mar 23, 2014 at 03:37 PM ·
wwwresources.loadfilestream
save FileStream with www.movie doesn't work very well for Resources.load
I tried to download a www.movie to Resources folder, then I can play later.The file was already download,but didn't appear on Resources.it was appeared after I hit th console window.Somebody can help me.The code like this.
using UnityEngine;
using System.Collections;
using System.IO;
public class moviePlayScript : MonoBehaviour {
private string videoFile;
public GUITexture ttt;
public WWW www;
public MovieTexture m1;
void Start()
{
videoFile = Application.dataPath + "/StreamingAssets/Resources/sample.ogg";
print (videoFile);
}
IEnumerator DownloadAndPlayVideo()
{
if(System.IO.File.Exists(videoFile))
{
print ("find");
m1 = Resources.Load("sample") as MovieTexture;
if(m1!=null && !m1.isPlaying && m1.isReadyToPlay)
{
ttt.texture = m1;
m1.Play();
audio.clip = m1.audioClip;
audio.Play();
}
yield return null;
}
else{
string playVideoFile = videoFile;//for pc
www = new WWW("http://www.unity3d.com/webplayers/Movie/sample.ogg");
if(!www.isDone )
print( "Download Progress: " + 100*www.progress + "% ..." );
yield return www;
if (www != null && www.isDone && www.error == null)
{
FileStream stream = new FileStream(videoFile, FileMode.Create);
stream.Write(www.bytes, 0, www.bytes.Length);
print ("stream");
stream.Flush();
stream.Close();
m1 = www.movie;
if(!m1.isPlaying && m1.isReadyToPlay)
{
ttt.texture = m1;
m1.Play();
audio.clip = m1.audioClip;
audio.Play();
}
yield break;
}
print (playVideoFile);
}
}
void OnGUI()
{
if(GUI.Button(new Rect(0,0,100,90), "StartPlay"))
{
StartCoroutine(DownloadAndPlayVideo());
}
if(GUI.Button(new Rect(200,0,100,90), "StopPlay"))
{
StopCoroutine("DownloadAndPlayVideo");
m1.Stop();
}
}
}
Comment
Try using https://docs.unity3d.com/Documentation/ScriptReference/AssetDatabase.Refresh.html after it's done downloading.. Unity probably needs to "import" the file (or generate some meta data) to be able to use the file