转载 利用WWW类获取图片并且在unityUGUI的Image中显示

因为最进遇到过这样问题。怎么动态来修改UGUI中的image呢,怎么来获取这个组件呢 。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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;
    }
 
}

 运行结果:

 转载地址:http://www.cnblogs.com/unitySPK/p/5050478.html

 

posted @ 2017-10-14 10:38  痴王  阅读(1185)  评论(0编辑  收藏  举报