www与协程

using UnityEngine;
using System.Collections;
using System.IO;//引入IO流
using UnityEngine.UI;
public class FileDemo : MonoBehaviour {

	private string url = "http://www.unity3d.com/webplayers/Movie/sample.ogg";
	public Scrollbar bar;
	public Text text;
	IEnumerator Start () {
		Application.runInBackground = true;
		//创建下载对象
		WWW www = new WWW (url);
		//等待下载
		while (!www.isDone) {
			//进度条操作
			bar.size = www.progress;
			//进度百分比显示
			text.text=Mathf.RoundToInt (www.progress*100).ToString()+"%";
			//等待
			yield return null;
		}
		//获取下载字节流
		byte[] data = www.bytes;
		//保存到本地
		File.WriteAllBytes (Application.dataPath + "/abc.ogg", data);
	}
}




using UnityEngine;
using System.Collections;

public class GameContrioller : MonoBehaviour {

	string functionName="PrintMsg";
	void Start(){
		//参数就是执行方法
		StartCoroutine (WaitSecondsDoSomething (2f));
		//参数就是协程方法的名字(字符串)
		StartCoroutine ("PrintMsg");
		//参数就是协程方法的名字(字符串),及协程方法参数
		StartCoroutine ("WaitSecondsDoSomething", 3f);
		StopCoroutine("PrintMsg");
		//StopCoroutine ("WaitSecondsDoSomething", 3f);//不管用
	}

	IEnumerator PrintMsg(){
		yield return new WaitForSeconds(2f);
		Debug.Log ("First");//第一帧
		yield return 0;//方法暂停,等待下一帧继续执行
		Debug.Log ("MSG");//第二帧
		yield return null;
		Debug.Log ("Thirid");

	}
	IEnumerator WaitSecondsDoSomething(float seconds){
		yield return new WaitForSeconds (seconds);
		yield return StartCoroutine (PrintMsg ());
		Debug.Log("DoSomething~!!");
	}
	IEnumerator NewUpdate(){
		while (true) {
			yield return null;
			Debug.Log ("Update");
		}
	}
}




using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class LoadLocalResources : MonoBehaviour {

	public RawImage img;
	//图片资源
	private Texture imgTexture;
	//影像资源
	private MovieTexture movTexture;
	//声音组件
	private AudioSource aud;
	void Awake(){
		aud = GetComponent<AudioSource> ();
	}
	/// <summary>
	/// 加载图片
	/// </summary>
	void LoadTexture(){
		//加载图片时,停止声音
		aud.Stop ();
		if (movTexture&&movTexture.isPlaying) {
			//停止视频播放
			movTexture.Stop ();
//			//暂停播放视频
//			movTexture.Pause ();
		}

		//本地资源加载(泛型加载方式) 返回泛型
		imgTexture=Resources.Load<Texture>("abc");
//		//本地资源加载(字符串转化类型加载) 返回Object
//		Resources.Load ("abc.jog")as Texture;

		//显示纹理
		img.texture = imgTexture;
	}
	void Update(){
		if (Input.GetKeyDown(KeyCode.T)) {
			LoadTexture ();
		}
		else if (Input.GetKeyDown(KeyCode.M)) {
			LoadMovie ();
		}
	}
	void LoadMovie(){
		//加载视频
		movTexture = Resources.Load<MovieTexture> ("def");
		//显示纹理
		img.texture = movTexture;

		//获取声音片段
		aud.clip=movTexture.audioClip;
		//播放视频和声音
		movTexture.Play();
		aud.Play ();
	}
}




using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PlayMovie : MonoBehaviour {

	public MovieTexture mov;
	public RawImage img;
	void Start () {
		if (mov.isReadyToPlay) {
			//显示视频纹理
			img.texture = mov;
			//播放
			mov.Play ();
			//播放声音
			AudioSource.PlayClipAtPoint (mov.audioClip, transform.position);
		}
	}
}




using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class WWWDemo : MonoBehaviour {
	/// <summary>
	///下载地址
	/// </summary>
	public string url="http://www.unity3d.com/webplayers/Movie/sample.ogg";
	public RawImage img;//用来显示下载的图片
	void Start(){
		//开启协程

		//StartCoroutine (DownloadText ());

		//StartCoroutine (DownloadTexture ());
		Application.runInBackground=true;
		StartCoroutine (DownloadMovie ());

	}
	  IEnumerator DownloadMovie(){
		WWW www = new WWW (url);
		//如果没有下载完,等待下载
		while (!www.isDone) {
			//打印当前进度
			Debug.Log (www.progress);
			yield return null;
		}
		//下载视频
		MovieTexture mov = www.movie;
		//等待视频准备完毕
		while (!mov.isReadyToPlay) {
			yield return null;
		}
		//将视频纹理显示
		img.texture = mov;
		//播放视频
		mov.Play ();
		AudioSource.PlayClipAtPoint (mov.audioClip, transform.position);
	}

	IEnumerator DownloadTexture(){
		WWW www = new WWW (url);
		yield return www;
		//下载完毕,显示图片
		img.texture=www.texture;

	}


	IEnumerator DownloadText(){
		//创建www对象,传入URL
		WWW www = new WWW (url);
		//如果没有下载完毕,继续等待
		while (!www.isDone) {
			yield return null;
		}	
		//打印文本
		Debug.Log (www.text);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值