unity www下載

可以在程序運行後,下載圖片、文本、視頻等並顯示在ui上或者存到本地等...

也可以在攝像機或者物體上播放視頻........///////下載速度可能巨慢,不建議下載視頻

建一個簡單的UI,畫布加兩個圖片.......

注意:下載⽅法必須是協程⽅法!!

 

 

 

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.IO;
using UnityEngine.Video;

public class WWWDemo : MonoBehaviour {

    [Header("URL")]
    public string url = "、、、、、、、、填個地址";
    [Header("展示圖片")]
    public RawImage rawImage;
    [Header("展示圖片")]
    public Image image;

    private AudioSource aud;

    private VideoPlayer videoPlayer;

    private void Awake()
    {
        aud = GetComponent<AudioSource>();
        videoPlayer = GetComponent<VideoPlayer>();
    }

    private void Start()
    {
        //StartCoroutine(DownloadText());
        //StartCoroutine(DownloadTexture());
        //StartCoroutine(DownloadAudio());
        //StartCoroutine(DownloadMovieTexture());
        //StartCoroutine(DownloadBytes());
        StartCoroutine(DownloadAndPlayVideo());
    }

    IEnumerator DownloadText()
    {
        //創建WWW對象,並傳入URL
        WWW www = new WWW(url);
        //等待www下載完畢
        yield return www;
        Debug.Log("<color=blue>"  + www.text + "</color>");
    }

    IEnumerator DownloadTexture()
    {
        WWW www = new WWW(url);
        yield return www;

        Texture texture = www.texture;

        texture.name = "Gril";

        //Image:Sprite  RawImage:Texture
        //將下載好的圖片,用RawImage渲染出來
        rawImage.texture = texture;

        //將紋理轉換爲精靈並設置到Image
        image.sprite = Sprite.Create(
            www.texture,
            new Rect(0, 0,
                www.texture.width,
                www.texture.height),
            Vector2.zero);

        //調整RawImage的寬高
        rawImage.GetComponent<
            RectTransform>().sizeDelta
            = new Vector2(
            www.texture.width,
            www.texture.height);
        //調整Image的寬高
        image.GetComponent<
            RectTransform>().sizeDelta
            = new Vector2(
            www.texture.width,
            www.texture.height);
    }

    IEnumerator DownloadAudio()
    {
        WWW www = new WWW(url);
        yield return www;
        //獲取下載的聲音片段
        AudioClip clip = www.GetAudioClip();
        //添加到AudioSource
        aud.clip = clip;
        //播放
        aud.Play();
    }///////DownloadAudio()裏面播放視頻和聲音現在應該是用不了的

    IEnumerator DownloadMovieTexture()
    {
        WWW www = new WWW(url);

        while(!www.isDone)
        {
            //打印進度
            Debug.Log("進度: " + www.progress);
            //沒有在線完畢,繼續等待
            yield return null;
        }

        //獲取下載好的MovieTexture
        MovieTexture movie = www.GetMovieTexture();
        //將視頻放置在RawImage
        rawImage.texture = movie;
        //將聲音放置到AudioSource
        aud.clip = movie.audioClip;
        //播放畫面
        movie.Play();
        //播放聲音
        aud.Play();
    }


    IEnumerator DownloadBytes()
    {
        WWW www = new WWW(url);

        while (!www.isDone)
        {
            //打印進度
            Debug.Log("進度: " + www.progress);
            //沒有在線完畢,繼續等待
            yield return null;
        }
        //將比特流寫入本地
        File.WriteAllBytes(Application.dataPath + "/Resources/demo.mp4", www.bytes);
    }

    IEnumerator DownloadAndPlayVideo()
    {
        //從Resource里加載視頻文件
        VideoClip video = Resources.Load<VideoClip>("demo");

        if(video == null)
        {
            //下載該視頻到Resources文件夾
            yield return StartCoroutine(DownloadBytes());
            //等一幀
            yield return new WaitForEndOfFrame();
            //重新Load
            video = Resources.Load<VideoClip>("demo");
        }

        //設置視頻片段
        videoPlayer.clip = video;
        //播放
        videoPlayer.Play();
    }////////////這個可以用
}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章