1. 程式人生 > >轉載 利用WWW類獲取圖片並且在unityUGUI的Image中顯示

轉載 利用WWW類獲取圖片並且在unityUGUI的Image中顯示

 https://www.cnblogs.com/chiwang/p/7665938.html

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GetSprite : MonoBehaviour {
    //利用WWW類獲取圖片並且在unityUGUI的Image中顯示
    public Image image;
    void Start () {
        string path = "http://pic.nipic.com/2007-11-09/2007119122519868_2.jpg";
        StartCoroutine(LoadImage(path));
    }
    IEnumerator LoadImage(string path)
    {
        WWW www = new WWW(path);
        yield return www;
        Texture2D texture = www.texture;
        Sprite sprites = Sprite.Create(texture,new Rect(0,0,texture.width,texture.height),new Vector2(0.5f,0.5f));
        image.sprite = sprites;
    }
 
}