++1.1、什么是协程?程序员
++++1、Unity的协程系统是基于C#的一个简单而强大的接口。web
++++2、简单讲,协程就是能够把一个方法拆分红屡次执行的一种接口。数据库
++++简单示例:编程
IEnumerator ShowTime(){设计模式
Debug.Log(“First Frame”); //第一帧执行数组
yield return 0; //等待下一帧网络
Debug.Log(“Second Frame”); //第二帧执行多线程
yield return 0; //等待下一帧架构
Debug.Log(“Third Frame”); //第三帧执行并发
}
++1.2、如何开启协程?
StartCoroutine(ShowTime()); //经过传入方法开启协程
StartCoroutine(“ShowTime”); //经过传入字符串类型的方式名称开启协程
++1.3、如何中止协程?
StartCoroutine(“ShowTime”);
StopCoroutine(“ShowTime”); //中止协程
++++注意: StopCoroutine只能中止以字符串方式开启的协程。
++1.4、Yield
++++1、当你“yield”一个方法时,你至关于说,“如今中止这个方法,而后在下一帧中从这里继续开始!”
++++2、用0或者null来yield的意思是告诉协程等待下一帧,直到继续执行为止。
++1.5、模拟Update
//在Update调用后,每帧调用一次
IEnumerator MyUpdate(){
//死循环
while(true){
Debug.Log(“MyUpdate”);
//方法在这个地方挂起,等下一帧来了,方法从这个位置继续往下执行
yield return 0;
}
}
++1.6、yield以后还能够加一些其余有意思的东西
++++yield return new WaitForSeconds(2f);
++++yield return StartCoroutine(otherIEnumerator());
....
++1.7、制做定时器
//时间间隔
float timeInterval =2;
IEnumerator Timer(){
while(true){
yield return new WaitForSeconds(timeInterval);
Debug.Log(“延迟两秒”);
}
}
++1.8、协程注意事项
++++1、在程序中调用StopCoroutine()方法只能终止以字符串形式启动(开始)的协程。
++++2、多个协程能够同时运行,它们会根据各自的启动顺序来更新。
++++3、协程能够嵌套任意多层。
++++4、协程不是多线程(尽管它们看上去是这样的),它们运行在同一线程中,跟普通的脚本同样。
++++5、IEnumerator类型的方法不能带ref或者out型的参数,但能够带被传递的引用。
++1.9、协程,线程的区别
++++1、线程拥有本身独立的栈和共享的堆,共享堆,不共享栈,线程亦由操做系统调度。
++++2、协程和线程同样共享堆,不共享栈,协程由程序员在协程的代码里显示调度。
++++3、协程避免了无心义的调度,由此能够提升性能,但也所以,程序员必须本身承担调度的责任,同时,协程也失去了标准线程使用多CPU的能力。
++1.10、协程优缺点
++++1、优势
--1、跨平台
--2、跨体系架构
--3、无需线程上下文切换的开销
--4、无需原子操做锁定及同步的开销
--5、方便切换控制流,简化编程模型
--6、高并发+高扩展性+低成本: 一个CPU支持上万的协程都不是问题,因此很适合用于高并发处理。
++++2、缺点
--1、没法利用多核资源: 协程的本质是个单线程,它不能同时将单个CPU的多个核用上,协程须要和进程配合才能运行在多CPU上。固然咱们平常所编写的绝大部分应用都没有这个必要,除非是cpu密集型应用。
--2、进行阻塞(Blocking)操做(如IO时)会阻塞掉整个程序:这一点和事件驱动同样,可使用异步IO操做来解决。
++2.1、WWW类
++++1、能够简单的访问web页面。
++++2、这是一个小工具模块检索url的内容。
++++3、你开始在后台下载经过调用WWW(url),返回一个新的WWW对象。
++++4、你能够检查isDone属性来查看是否已经下载完成,或者yield自动等待下载物体,直到它被下载完成(不会影响游戏的其他部分)。
++2.2、WWW基本使用
public class ExampleClass : MonoBehaviour{
public string url =“http://...../lovezuanzuan.jpg”;
IEnumerator Start(){
WWW www = new WWW(url);
yield return www;
renderer.material.mainTexture= www.texture;
}
}
++2.3、WWW经常使用属性
++++texture :下载到的图片
++++audioClip :下载到的音频
++++movie : 下载到的视频
++++bytes : 下载到的比特数组
++++text : 下载到的Web文字
++++isDone : 下载是否完毕
++++progress : 当前下载进度
++++url : 下载地址
++2.4、下载网络资源
//url资源地址
IEnumerator Download(string url){
WWW www =new WWW(url); //www保存了下载的资源
yield return www;
}
++2.5、获取下载后的文本
IEnumerator DownloadText(string url){
WWW task = new WWW(url);
yield return task;
string text = task.text; //下载的文本
transform.Find(“Text”).GetComponent<Text>().text= text;
StartCoroutine(Save(task)); //保存
}
++2.6、获取下载后的音频
WWW Task; //下载音频
IEnumerator DownloadAudio(string url){
Task = new WWW(url);
while(Task.isDone == false){
Debug.Log(Task.progress);
yield return 0;
}
Debug.Log(“完成!” + Task.bytesDownloaded); //下载文件的大小
}
string path = Application.dataPath+ “/Resources/” +“1.mp3”;
File.WriteAllBytes(path,Task.bytes);
while(File.Exists(path) == false){
yield return 0;
}
AssetDatabase.Refresh();
aud.clip = Resource.Load<AudioClip>(“1”); //经过Resources读取音频
aud.Play();
#立钻哥哥Unity 学习空间: http://blog.csdn.net/VRunSoftYanlz/
++立钻哥哥推荐的拓展学习连接(Link_Url):
++++立钻哥哥Unity 学习空间: http://blog.csdn.net/VRunSoftYanlz/
++++Unity引擎基础:http://www.javashuo.com/article/p-beommoeb-ka.html
++++Unity面向组件开发:http://www.javashuo.com/article/p-eigmuvut-dt.html
++++Unity物理系统:http://www.javashuo.com/article/p-nqvvciwv-kd.html
++++Unity2D平台开发:http://www.javashuo.com/article/p-ycaagdtj-hs.html
++++UGUI基础:http://www.javashuo.com/article/p-rukxwckw-mc.html
++++UGUI进阶:http://www.javashuo.com/article/p-wcatruhq-gt.html
++++UGUI综合:http://www.javashuo.com/article/p-dkccmqii-gg.html
++++Unity动画系统基础:http://www.javashuo.com/article/p-mbrdouxy-dq.html
++++Unity动画系统进阶:http://www.javashuo.com/article/p-aqaqpbkh-bp.html
++++Navigation导航系统:http://www.javashuo.com/article/p-dswwllas-t.html
++++Unity特效渲染:http://www.javashuo.com/article/p-ckojjyfj-bp.html
++++Unity数据存储:http://www.javashuo.com/article/p-bvlzynso-m.html
++++Unity中Sqlite数据库:http://www.javashuo.com/article/p-ejutsbxl-ca.html
++++WWW类和协程:http://www.javashuo.com/article/p-dbwmhsav-cy.html
++++Unity网络:http://www.javashuo.com/article/p-sqrlntgh-dw.html
++++C#事件:http://www.javashuo.com/article/p-zmwruvql-gm.html
++++C#委托:http://www.javashuo.com/article/p-uozpymaf-gh.html
++++C#集合:http://www.javashuo.com/article/p-sfqfdqsf-ex.html
++++C#泛型:http://www.javashuo.com/article/p-xrttqngo-ee.html
++++C#接口:http://www.javashuo.com/article/p-vhlfplgv-dm.html
++++C#静态类:https://blog.csdn.net/vrunsoftyanlz/article/details/78630979
++++C#中System.String类:http://www.javashuo.com/article/p-olslkfao-cq.html
++++C#数据类型:http://www.javashuo.com/article/p-hmabbtmc-ba.html
++++Unity3D默认的快捷键:http://www.javashuo.com/article/p-wuwcrclr-s.html
++++游戏相关缩写:http://www.javashuo.com/article/p-mwacxwca-gm.html
++++设计模式简单整理:http://www.javashuo.com/article/p-rngqugib-hg.html
++++U3D小项目参考:https://blog.csdn.net/vrunsoftyanlz/article/details/80141811
++++UML类图:http://www.javashuo.com/article/p-sxberuew-bm.html
++++Unity知识点0001:http://www.javashuo.com/article/p-ryvdxxjr-ep.html
++++U3D_Shader编程(第一篇:快速入门篇):http://www.javashuo.com/article/p-kyppgrac-gz.html
++++U3D_Shader编程(第二篇:基础夯实篇):http://www.javashuo.com/article/p-qkyowtli-hv.html
++++立钻哥哥Unity 学习空间: http://blog.csdn.net/VRunSoftYanlz/
--_--VRunSoft : lovezuanzuan--_--