Ending Learning for Day 14
2017-08-14 19:05
351 查看
Unity 协程
协程:协程(Coroutine)不同于线程(Thread),他目标解决的不是线程阻塞一类的问题。以我个人的理解,它主要解决有某些必须在游戏循环中运行的代码,而你又不想把它写到Update()中的情况。游戏与一般应用程序的区别在于,一般应用程序是事件驱动,而游戏则拥有游戏循环这一特性。
游戏中自然也会有”事件”,基本都可以写在Update()中,但是由于游戏循环的存在,Update()会风雨无阻的在每一帧调用,为了让代码适时的执行,一般都会设置几个bool或int/float来控制Update()中的代码。
这时你会发现,如果你的Update()中有太多这样的代码时,代码的可读性会越来越糟。
http://gypsumlabs.github.io/2015/12/08/unity-tips-coroutine/
C# Lamdba表达式
我们在C#2.0之前就有委托了,在2.0之后又引入了匿名方法,C#3.0之后,又引入了Lambda表达式,他们三者之间的顺序是:委托->匿名变量->Lambda表达式http://www.cnblogs.com/chenwolong/p/5669440.html
C# (事件触发)回调函数
//事件发送者 class Dog { //1.声明关于事件的委托; public delegate void AlarmEventHandler(object sender, EventArgs e); //2.声明事件; public event AlarmEventHandler Alarm; //3.编写引发事件的函数; public void OnAlarm() { if (this.Alarm != null) { Console.WriteLine("\n狗报警: 有小偷进来了,汪汪~~~~~~~"); this.Alarm(this, new EventArgs()); //发出警报 } } } //事件接收者 class Host { //4.编写事件处理程序 void HostHandleAlarm(object sender, EventArgs e) { Console.WriteLine("主人: 抓住了小偷!"); } //5.注册事件处理程序 public Host(Dog dog) { dog.Alarm += new Dog.AlarmEventHandler(HostHandleAlarm); } } //6.现在来触发事件 class Program { static void Main(string[] args) { Dog dog = new Dog(); Host host = new Host(dog); //当前时间,从2008年12月31日23:59:50开始计时 DateTime now = new DateTime(2015, 12, 31, 23, 59, 50); DateTime midnight = new DateTime(2016, 1, 1, 0, 0, 0); //等待午夜的到来 Console.WriteLine("时间一秒一秒地流逝... "); while (now < midnight) { Console.WriteLine("当前时间: " + now); System.Threading.Thread.Sleep(1000); //程序暂停一秒 now = now.AddSeconds(1); //时间增加一秒 } //午夜零点小偷到达,看门狗引发Alarm事件 Console.WriteLine("\n月黑风高的午夜: " + now); Console.WriteLine("小偷悄悄地摸进了主人的屋内... "); dog.OnAlarm(); Console.ReadLine(); } }
相关文章推荐
- Ending Learning for Day 11
- Ending Learning for Day 5
- Unity Learning for Day 14
- Crazy Learning for Day 14
- Ending Learning for Day 4
- Ending Learning for Day 6
- Minigame Learning for Day 3
- Day-14 JS中的JSON,for..in..与数组对象的操作
- Minigame Learning for Day 7
- Unity Learning for Day 21
- Unity Learning for Day 22
- 机器学习中的神经网络Neural Networks for Machine Learning:Lecture 14 Quiz
- Minigame Learning for Day 8
- Unity Learning for Day 5
- Crazy Learning for Day 7
- Crazy Learning for Day 18
- Unity Learning for Day 9
- Unity Learning for Day 15
- Minigame Learning for Day 4
- Crazy Learning for Day 16