WWW类和协程----个人学习

使用WWW类和协程完成简单的图片和视频下载播放功能。

一、WWW类

WWW是一个Unity开发中非常常用到的工具类,主要提供一般Http访问的功能,以及动态从网上下载图片、声音、视频、Unity资源等。

注意:iPhone支持http://, https://和 file://协议;ftp://协议的支持仅限于**下载。其他协议不被支持。

当在windows和Windows Store Apps使用文件协议来访问本地文件,需要使用file:///(带有三个斜线)。

WWW类的主要常用属性有:

 

Note: URLs passed to WWW class must be '%' escaped. 

属性

assetBundleStreams一个AssetBundle,它可以包含项目文件夹中的任何类型的资产。
字节以字节数组的形式返回获取的网页的内容(只读)。
bytesDownloaded此WWW查询下载的字节数(只读)。
错误如果下载过程中出现错误,则返回错误消息(只读)。
已经完成了下载已经完成了吗?(只读)
进展下载进度有多远(只读)。
responseHeaders响应请求返回的标头字典。
文本以字符串形式返回获取的网页的内容(只读)。
质地返回从下载的数据生成的Texture2D(只读)。
textureNonReadable返回从下载数据生成的不可读Texture2D(只读)。
threadPriority已过时,无效。
上传进度上传进度有多远(只读)。
网址此WWW请求的URL(只读)。

构造函数

万维网使用给定的URL创建WWW请求。

公共方法

部署处置现有WWW对象。
GetAudioClip返回从下载的数据生成的AudioClip(只读)。
GetAudioClipCompressed返回从内存中压缩的下载数据生成的AudioClip(只读)。
LoadImageIntoTexture用下载数据中的图像替换现有Texture2D的内容。

静态方法

EscapeURL转义字符串中的字符以确保它们对URL友好。
LoadFromCacheOrDownload从缓存中加载具有指定版本号的AssetBundle。如果当前没有缓存AssetBundle,它将自动下载并存储在缓存中,以便将来从本地存储中检索。
UnEscapeURL将URL友好的转义序列转换回普通文本。

继承的成员

属性

继续等待指示协程是否应该保持挂起状态。

 

Properties

assetBundleStreams an AssetBundle that can contain any kind of asset from the project folder.
bytesReturns the contents of the fetched web page as a byte array (Read Only).
bytesDownloadedThe number of bytes downloaded by this WWW query (read only).
errorReturns an error message if there was an error during the download (Read Only).
isDoneIs the download already finished? (Read Only)
progressHow far has the download progressed (Read Only).
responseHeadersDictionary of headers returned by the request.
textReturns the contents of the fetched web page as a string (Read Only).
textureReturns a Texture2D generated from the downloaded data (Read Only).
textureNonReadableReturns a non-readable Texture2D generated from the downloaded data (Read Only).
threadPriorityObsolete, has no effect.
uploadProgressHow far has the upload progressed (Read Only).
urlThe URL of this WWW request (Read Only).

Constructors

WWWCreates a WWW request with the given URL.

Public Methods

DisposeDisposes of an existing WWW object.
GetAudioClipReturns an AudioClip generated from the downloaded data (Read Only).
GetAudioClipCompressedReturns an AudioClip generated from the downloaded data that is compressed in memory (Read Only).
LoadImageIntoTextureReplaces the contents of an existing Texture2D with an image from the downloaded data.

Static Methods

EscapeURLEscapes characters in a string to ensure they are URL-friendly.
LoadFromCacheOrDownloadLoads an AssetBundle with the specified version number from the cache. If the AssetBundle is not currently cached, it will automatically be downloaded and stored in the cache for future retrieval from local storage.
UnEscapeURLConverts URL-friendly escape sequences back to normal text.

Inherited Members

Properties

keepWaitingIndicates if coroutine should be kept suspended.

www类加载图片的官方的示例代码如下:

using UnityEngine;

using System.Collections;

public class ExampleClass: MonoBehaviour{

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

    IEnumerator Start() {

        WWW www = new WWW(url);

        yield return www;

        renderer.material.mainTexture = www.texture;

    }

}

二、协程

1、什么是协程?

       协程是一个分部执行,遇到条件(yield return 语句)会挂起,直到条件满足才会被唤醒继续执行后面的代码。

        Unity在每一帧都会去处理对象上的协程。Unity主要是在Update后去处理协程(检查协程的条件是否满足)。协程跟Update()其实一样的,都是Unity每帧都会去处理的函数。如果在一个对象的前期调用协程,协程会立即运行到第一个 yield return 语句处,如果是 yield return null ,就会在同一帧再次被唤醒。      

2、协程的写法

       Unity的协程系统是基于C#的一个简单而强大的接口 ,IEnumerator,它允许你为自己的集合类型编写枚举器。也就是一个 IEnumerator 返回值,一个

yield return null的语句。

   IEnumerator SayHi() 
{

          //协程中必须有返回值 yield return
yield return null;
}

   

   //带参数的:

   IEnumeratorSayHello(string name){
print (name);
yield return null;
}

3.*协程中的细节*
1.协程与普通方法一样,可以被多次调用, 
2.协程一旦被开启之后,总是试图将方法中的代码执行完,之后停止。
3.在协程内,如果遇到yield return null ,0 , 1 ...;表示剩余代码将在下一帧继续执行,
4.在协程中遇到 yield return new WaitForSeconds (n);代码;表示剩余代码将在 n秒之后执行。 
5.在协程中如果遇到 yield return StartCoroutine (Son ()), 表示剩余代码将在子协程执行完成之后继续执行。子协程仍然满足协程基本规则。
6.在协程中如果遇到 yield return new WaitForFixedUpdate (), 表示剩余代码将在FixedUpdate执行完毕之后继续向下执行。
7. 在协程中如果遇到 yield return new WaitForEndOfFrame (), 表示剩余代码将在ONGUI执行完毕之后继续向下执行。
8.在协程中如果遇到 yield return WWW,表示剩余代码将在 www下载文件之后继续向下执行。
9.在协程中如果遇到  yield return obj ,表示剩余代码将在obj部位空的时候继续向下执行。
10.在协程中如果遇到  yield return new WaitForSeconds (n) ,会受到Time.timeScale的影响。
11.协程方法,可以当做普通方法,在两个脚本之间自由调用。 
12.生命周期内的方法,也可以改造成协程方法。

 

4、启动协程的写法。

       //3.1 启动协程
StartCoroutine(SayHi());
//3.2 方法名开启
StartCoroutine("SayHi");
//3.3 启动带参数的协程
StartCoroutine(SayHello("hello"));
StartCoroutine ("SayHello","你好");

        //关闭协程 ,注意只能通过字符串去关闭一个协程。
StopCoroutine("SayHi");

 

代码示例:利用一个协程和www类实现简单的图片下载展示:

1.在unity中搭建好一个场景需要一个RawImage。

2.创建一个脚本,完成以下代码:

 

//  1) 定义一个RawImage用于展示图片
RawImage l_rawImage;
//  2) 图片的网址
string imgUrl;
  void Start ()
{
// 代码获取我们的RawImage
l_rawImage =

     GameObject.Find("RawImage").GetComponent<RawImage> ();
// 网址赋值
imgUrl = "http://g.hiphotos.baidu.com/image/h%3D360/sign" +
      "=07ad353ef403738dc14a0a24831ab073/08f790529822720eb2"           +"5fa86479cb0a46f31fab9f.jpg";
// 开启下载图片的协程。
StartCoroutine (LoadImage ());

}

// 实现加载协程的方法
IEnumerator LoadImage()
{

// 根据连接下载
WWW www =new WWW (imgUrl);
// 等待WWW代码执行完毕之后后面的代码才会执行。
yield return www;
// 将下载的textrue在RawImage上展示
l_rawImage.texture = www.texture;

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值