[英]Generic<T> Unity C#
如果我有一些經理(單身),其中有一些協程,我想用一個簡單的方法StopAllCorountines。 是否有可能使通用類型停止傳入的所有經理?
我無法使它工作,但它是這樣的嗎?
void StopAllCorountinesInAllManager<T>(T manager)
{
manager.instance.StopAllCorountines();
}
StopAllCoruntinesInAllManager(manager1);
StopAllCoruntinesInAllManager(manager2);
StopAllCoruntinesInAllManager(manager3);
為您的泛型方法提供類型提示:
void StopAllCoroutinesInAllManager<T>(T manager) where T : GameObject
{
manager.StopAllCorountines();
}
通過傳遞實例來調用它:
StopAllCoroutinesInManager(manager1.Instance);
或者,如果您的經理不是GameObjects,請創建一個界面,例如:
public interface IManager
{
void StopAllCoroutines();
}
並改變您的通用方法:
void StopAllCoroutinesInAllManager(IManager manager)
{
manager.StopAllCorountines();
}
然后在你的經理類中實現接口並讓實現停止所有協同程序。 但在這種情況下,您可以直接在管理器上調用StopAllCoroutines。
這應該允許您遍歷管理器列表並停止所有協同程序。
將經理的實例傳遞給您的函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.