# CoroutineUtil
**Repository Path**: Wnity/coroutineutil
## Basic Information
- **Project Name**: CoroutineUtil
- **Description**: coroutineutil
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2025-01-22
- **Last Updated**: 2025-11-04
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# CoroutineUtil
#### 介绍
使协程可返回值,减少callback,提高代码的可维护性,使协程对于yield return 返回的值不是null,IEnumerator,YieldInstruction时,协程就会结束并把返回值传到上层调用处
https://zhuanlan.zhihu.com/p/691536822
# 集成到自己的项目
- 把下面的package添加到项目的Packages/manifest.json
```
"com.wnity.coroutineutil": "https://gitee.com/Wnity/coroutineutil.git?path=Assets/CoroutineUtil",
```
# 用法
```
using cframework_coroutineutil;
using System.Collections;
using UnityEngine;
public class TestCoroutineWithResult: MonoBehaviour
{
public IEnumerator Test4()
{
yield return null; //停一帧
yield return new WaitForSeconds(1); //停一秒
yield return "hellow world!"; //返回值 hello world!
}
public IEnumerator Start()
{
var b = Test4().WithResult(); //调用WithResult使协程可通过yield return "hellow world!"停止协程的运行,并能以hello world!作为返回值
yield return b; //异步等待返回值
Debug.LogError("test4 result: " + b.Current); //这里输出协程返回值,是hello world!
}
}
```
# 为什么用这个库,用UniTask不香吗
- UniTask的确很强大,不过UniTask的代码量比coroutineUtil多,coroutineUtil只是为了实现最简单的异步编程
- UniTask的异步方法返回值是要依赖模板的,对于Hybridclr的热更来说不够方便
# coroutineUtil的缺点
- 使yield return的意义和用法和之前有区别,需要注意,但好在并不是太大的问题
- 需要依赖定义一个IEnumerator以通过它的Current成员变量才能获得返回值
# 测试场景
- Assets/test/TestCoroutineWithResult.unity
# qq 技术讨论群
951129597