Unity協(xié)程(Coroutine)管理類——TaskManager工具分享 By D.S.Qiu 尊重他人的勞動,支持原創(chuàng),轉(zhuǎn)載請注明出處:http.dsqiu.
在分享 vp_Timer 中提到,沒有繼承的MonoBehaviour,沒有Update,InVoke 和StartCoroutine的機(jī)制,vp_Timer就是提供了InVoke的機(jī)制,而且還可以統(tǒng)一管理。本篇D.S.Qiu要分享的TaskManager就是一個協(xié)程 管理類。 TaskManager —— Unity3D Managed Coroutines with Start, Stop, Resume ,看著就覺得很強(qiáng)大,當(dāng)然是對于我這種對協(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.
代碼很簡單,但卻很解渴,Unity官方只聽過了StopCoroutine(string methodName)或StopAllCoroutine() 這兩個停止方法,從api就會覺得Unity的整體方法論還不完善,所以才會覺得TaskManager的難能可貴。由于源碼簡單,就不做解釋了,See source for document :
Usage Example:
小結(jié): 本文主要是分享我的收藏的一些“干貨”,TaskManager 和 vp_Timer 在項目中發(fā)揮了很大的作用,D.S.Qiu 一再覺得強(qiáng)大的東西不都是復(fù)雜的,能夠使用最簡單的本質(zhì)方法解決問題才是代碼設(shè)計的追求。 文末附上了相關(guān)的鏈接以及TaskManager的代碼。
如果您對D.S.Qiu有任何建議或意見可以在文章后面評論,或者發(fā)郵件(gd.s.qiu@gmail.com)交流,您的鼓勵和支持是我前進(jìn)的動力,希望能有更多更好的分享。 轉(zhuǎn)載請在文首注明出處:http://dsqiu./blog/2022992 更多精彩請關(guān)注D.S.Qiu的博客和微博(ID:靜水逐風(fēng))
參考: ①krockot / Unity-TaskManager:https://github.com/krockot/Unity-TaskManager ②Programmer Instincts:http:///214/ |
|
來自: kiki的號 > 《插件開發(fā)》