当前位置:   article > 正文

Unity从网上加载图片(视频)_unity访问网络多图加载

unity访问网络多图加载

1------------------------------

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.UI;
public class LoadFromWeb : MonoBehaviour
{
    public Image image;
    string img_url = "https://pic.baike.soso.com/ugc/baikepic2/6749/20210720102025-811299437_jpeg_3968_2976_3786803.jpg/0";
    void Start()
    {
        StartCoroutine(RYVACBT_iadufa(img_url));
    }
    IEnumerator RYVACBT_iadufa(string url)
    {
        WWW www = new WWW(url);
        yield return www;
        if (www.error == null)
        {
            print("---load Succeed---");
            Texture2D t = www.texture;
            Sprite csprite = Sprite.Create(t, new Rect(0, 0, t.width, t.height), new Vector2(0, 0));

            byte[] bytedata = t.EncodeToPNG();
            //将下载的图片动态保存到本地
            File.WriteAllBytes(Application.dataPath + "/a", bytedata);
            image.sprite = csprite;
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

2--------------------------------------------

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

public class LoadFromWeb : MonoBehaviour
{
    public Image image;
    public GameObject cube;
    string img_url = "https://pic.baike.soso.com/ugc/baikepic2/6749/20210720102025-811299437_jpeg_3968_2976_3786803.jpg/0";
    void Start()
    {
        StartCoroutine(LoadImage());
    }

    /// <summary>
    /// 加载图片
    /// </summary>
    /// <returns></returns>
    IEnumerator LoadImage()
    {
        using (UnityWebRequest webRequest = new UnityWebRequest())
        {
            //设置URL
            webRequest.url = img_url;
            //设置访问方式
            webRequest.method = UnityWebRequest.kHttpVerbGET;
            //设置下载类型
            webRequest.downloadHandler = new DownloadHandlerTexture();

            //协程等待
            yield return webRequest.SendWebRequest();

            if (webRequest.isNetworkError || webRequest.isHttpError)
            {
                Debug.Log(webRequest.error);
            }
            else
            {
                //将图片加载到3D平面
                cube.GetComponent<Renderer>().material.mainTexture =
                    DownloadHandlerTexture.GetContent(webRequest);
                //将图片加载到UI图片
                Texture2D texture2D =
                    DownloadHandlerTexture.GetContent(webRequest);
                image.sprite = Sprite.Create(
                    texture2D,
                    new Rect(0, 0, texture2D.width, texture2D.height),
                    new Vector2(0.5f, 0.5f));
            }

        }
    }
    /// <summary>
    /// 加载视频
    /// </summary>
    public void LoadVideo()
    {
        //videoPlayer.url = inputField.text;
        //videoPlayer.Play();

    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号