GFramework/GFramework.Game/coroutine/GlobalCoroutineScope.cs
GeWuYou ef2e718efe feat(coroutine): 完善协程系统接口设计与实现
- 实现ICoroutineContext和ICoroutineHandle接口,提供协程上下文和句柄的标准定义
- 重构CoroutineContext实现ICoroutineContext接口,统一协程上下文访问方式
- 修改CoroutineHandle实现ICoroutineHandle接口,标准化协程控制方法
- 扩展ICoroutineScheduler和ICoroutineScope接口,增加ActiveCount属性和Launch方法
- 优化CoroutineScheduler线程安全性,添加线程ID检查防止跨线程调用
- 为WaitForSeconds、WaitUntil、WaitWhile添加Reset方法支持状态重置
- 重构CoroutineScopeExtensions移除类型转换,使用接口方法替代具体类型
- 改进GlobalCoroutineScope添加TryGetScope方法,使用TryGet模式替代异常控制
- 优化CoroutineHandle取消逻辑,确保取消时正确触发OnComplete事件
- 统一各协程组件的XML文档注释,完善参数和返回值说明
- [skip ci]
2026-01-21 00:06:01 +08:00

50 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using GFramework.Game.Abstractions.coroutine;
namespace GFramework.Game.coroutine;
/// <summary>
/// 全局协程作用域管理器,提供全局唯一的协程执行环境
/// </summary>
public static class GlobalCoroutineScope
{
private static CoroutineScope? _instance;
/// <summary>
/// 获取当前全局协程作用域是否已初始化
/// </summary>
public static bool IsInitialized => _instance != null;
/// <summary>
/// 尝试获取当前全局协程作用域实例
/// </summary>
/// <param name="scope">输出参数如果初始化则返回协程作用域实例否则返回null</param>
/// <returns>如果全局协程作用域已初始化则返回true否则返回false</returns>
public static bool TryGetScope(out ICoroutineScope? scope)
{
scope = _instance;
return _instance != null;
}
/// <summary>
/// 初始化全局协程作用域
/// </summary>
/// <param name="scheduler">用于执行协程的调度器</param>
public static void Initialize(CoroutineScheduler scheduler)
{
_instance = new CoroutineScope(scheduler, "GlobalScope");
}
/// <summary>
/// 在全局协程作用域中启动一个协程
/// </summary>
/// <param name="routine">要执行的协程枚举器</param>
/// <returns>协程句柄,用于控制和监控协程执行</returns>
/// <exception cref="InvalidOperationException">当全局协程作用域未初始化时抛出</exception>
public static ICoroutineHandle Launch(IEnumerator routine)
{
return _instance == null
? throw new InvalidOperationException("GlobalCoroutineScope not initialized. Call Initialize() first.")
: _instance.Launch(routine);
}
}