【Unity3D游戏开发】WWW工具类简单的HTTP访问 (三四)

WWW是UNITY提供的访问HTTP的简单的工具类,可以从网络上下载配置、图片、录像等资源。


支持协议有 http:// https:// file:// ftp://


官方使用例子: 从网络上下载一张图片


// Get the latest webcam shot from outside "Friday's" in Times Square
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {

	public const string url = "http://images.earthcam.com/ec_metros/ourcams/fridays.jpg";

	void Start()
	{
		StartCoroutine(StartDownload(url, (tex) =>
			{
				// 下载图片后已经是一段时间后了,需要检测控件的有效性
				Renderer renderer = GetComponent<Renderer>();
				renderer.material.mainTexture = tex;	
			}));
	}

	IEnumerator StartDownload(string url, Action<Texture2D> act) {
		// url可以是网络网址,也可以是本地网址
		WWW www = new WWW(url);

		// 检测是否下载完毕,也可以通过IsDone函数检测
		yield return www;

		if (!string.IsNullOrEmpty(www.error)) {
			if (act != null) {
				act(www.texture);
			}
		}

		// 释放资源
		www.Dispose();
	}
}


 官方文档:http://docs.unity3d.com/ScriptReference/WWW.html

参考文章:http://blog.csdn.net/mfc11/article/details/8188785


发布了106 篇原创文章 · 获赞 24 · 访问量 132万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章