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

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

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;
	}

}

 运行结果:

 

 

posted on 2015-12-16 11:05  萨瓦迪卡麦兜兜  阅读(1461)  评论(0编辑  收藏  举报

导航