Unity協(xié)程(Coroutine)管理類——TaskManager工具分享
By D.S.Qiu
尊重他人的勞動(dòng),支持原創(chuàng),轉(zhuǎn)載請(qǐng)注明出處:http.dsqiu.iteye.com
在分享 vp_Timer 中提到,沒有繼承的MonoBehaviour,沒有Update,InVoke 和StartCoroutine的機(jī)制,vp_Timer就是提供了InVoke的機(jī)制,而且還可以統(tǒng)一管理。本篇D.S.Qiu要分享的TaskManager就是一個(gè)協(xié)程 管理類。 TaskManager —— Unity3D Managed Coroutines with Start, Stop, Resume ,看著就覺得很強(qiáng)大,當(dāng)然是對(duì)于我這種對(duì)協(xié)程理解不深的來說。下面貼出 The Motivation of the author:
/// The motivation for this is twofold:
///
/// 1. The existing coroutine API provides no means of stopping specific
/// coroutines; StopCoroutine only takes a string argument, and it stops
/// all coroutines started with that same string; there is no way to stop
/// coroutines which were started directly from an enumerator. This is
/// not robust enough and is also probably pretty inefficient.
///
/// 2. StartCoroutine and friends are MonoBehaviour methods. This means
/// that in order to start a coroutine, a user typically must have some
/// component reference handy. There are legitimate cases where such a
/// constraint is inconvenient. This implementation hides that
/// constraint from the user.
代碼很簡(jiǎn)單,但卻很解渴,Unity官方只聽過了StopCoroutine(string methodName)或StopAllCoroutine() 這兩個(gè)停止方法,從api就會(huì)覺得Unity的整體方法論還不完善,所以才會(huì)覺得TaskManager的難能可貴。由于源碼簡(jiǎn)單,就不做解釋了,See source for document :
Usage Example:
小結(jié):
本文主要是分享我的收藏的一些“干貨”,TaskManager 和 vp_Timer 在項(xiàng)目中發(fā)揮了很大的作用,D.S.Qiu 一再覺得強(qiáng)大的東西不都是復(fù)雜的,能夠使用最簡(jiǎn)單的本質(zhì)方法解決問題才是代碼設(shè)計(jì)的追求。 文末附上了相關(guān)的鏈接以及TaskManager的代碼。
如果您對(duì)D.S.Qiu有任何建議或意見可以在文章后面評(píng)論,或者發(fā)郵件(gd.s.qiu@gmail.com)交流,您的鼓勵(lì)和支持是我前進(jìn)的動(dòng)力,希望能有更多更好的分享。
轉(zhuǎn)載請(qǐng)?jiān)谖氖鬃⒚鞒鎏帲?/span>http://dsqiu.iteye.com/blog/2022992
更多精彩請(qǐng)關(guān)注D.S.Qiu的博客和微博(ID:靜水逐風(fēng))
參考:
①krockot / Unity-TaskManager:https://github.com/krockot/Unity-TaskManager
②Programmer Instincts:http://programmerinstincts.com/214/
聯(lián)系客服