当前位置:   article > 正文

Unity加载网页上的图片_unity加载网络图片

unity加载网络图片
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class TestImage : MonoBehaviour
{
    [SerializeField]
    private Image image;

    public void LoadImage(string url)
    {
        StartCoroutine(LoadTexture(url));
    }

    IEnumerator LoadTexture(string url)
    {
        UnityWebRequest uwr = UnityWebRequest.Get(url);
        DownloadHandlerTexture tex = new DownloadHandlerTexture(true);
        uwr.downloadHandler = tex;

        yield return uwr.SendWebRequest();
        if (uwr.result == UnityWebRequest.Result.ConnectionError)
        {
            Debug.LogError(uwr.error);
        }
        else
        {
            Texture2D t2d = new Texture2D(1, 1);
            t2d = tex.texture;
            Sprite spr = Sprite.Create(t2d, new Rect(0, 0, t2d.width, t2d.height), new Vector2(0.5f, 0.5f));
            image.sprite = spr;
        }
    }
}

  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/103490?site
推荐阅读
相关标签
  

闽ICP备14008679号