Unity资源管理系统 ——YooAsset学习笔记(三)异步操作系统OperationSystem

YooAsset学习笔记系列

什么是YooAsset:https://github.com/tuyoogame/YooAsset/tree/main
事件管理器EventManager
有限状态机管理器FsmManager
异步操作系统OperationSystem


文章目录

  • YooAsset学习笔记系列
  • 前言
  • 异步操作基类AsyncOperationBase
  • OperationSystem异步操作系统


前言

异步操作基类AsyncOperationBase

所有异步操作的抽象基类

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace YooAsset
{
	public abstract class AsyncOperationBase : IEnumerator
	{
		// 用户请求的回调,在操作完成时会被调用。通过Completed事件注册、移除事件
		private Action<AsyncOperationBase> _callback;

		/// 
		/// 状态
		/// 
		public EOperationStatus Status { get; protected set; } = EOperationStatus.None;

		/// 
		/// 错误信息
		/// 
		public string Error { get; protected set; }

		/// 
		/// 处理进度
		/// 
		public float Progress { get; protected set; }

		/// 
		/// 是否已经完成
		/// 
		public bool IsDone
		{
			get
			{
				return Status == EOperationStatus.Failed || Status == EOperationStatus.Succeed;
			}
		}

		/// 
		/// 完成事件
		/// 
		public event Action<AsyncOperationBase> Completed
		{
			add
			{
				if (IsDone)
					value.Invoke(this);
				else
					_callback += value;
			}
			remove
			{
				_callback -= value;
			}
		}

		/// 
		/// 异步操作任务
		/// 
		public Task Task
		{
			get
			{
				if (_taskCompletionSource == null)
				{
					_taskCompletionSource = new TaskCompletionSource<object>();
					if (IsDone)
						_taskCompletionSource.SetResult(null);
				}
				return _taskCompletionSource.Task;
			}
		}

		internal abstract void Start();
		internal abstract void Update();
		internal void Finish()
		{
			Progress = 1f;
			_callback?.Invoke(this);
			if (_taskCompletionSource != null)
				_taskCompletionSource.TrySetResult(null);
		}

		#region 异步编程相关
		bool IEnumerator.MoveNext()
		{
			return !IsDone;
		}
		void IEnumerator.Reset()
		{
		}
		object IEnumerator.Current => null;
		
		// TaskCompletionSource 可以将异步操作封装成一个Task
		private TaskCompletionSource <object> _taskCompletionSource;
		#endregion
	}
}

OperationSystem异步操作系统

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

namespace YooAsset
{
	internal class OperationSystem
	{
		private static readonly List<AsyncOperationBase> _operations = new List<AsyncOperationBase>(100);

		// 计时器相关
		private static Stopwatch _watch;
		private static long _maxTimeSlice;
		private static long _frameTime;

		/// 
		/// 初始化异步操作系统
		/// 设置每帧运行最大时间片,开始计时
		/// 
		public static void Initialize(long maxTimeSlice)
		{
			_maxTimeSlice = maxTimeSlice;
			_watch = Stopwatch.StartNew();
		}

		/// 
		/// 更新异步操作系统。
		/// 由YooAssetDriver驱动YooAsset的InternalUpdate中每帧调用
		/// 每帧运行不超过设置的最大时间片,确保不会因为异步操作掉帧
		/// 
		public static void Update()
		{
			_frameTime = _watch.ElapsedMilliseconds;

			for (int i = _operations.Count - 1; i >= 0; i--)
			{
				if (_watch.ElapsedMilliseconds - _frameTime >= _maxTimeSlice)
					return;

				var operation = _operations[i];
				operation.Update();
				if (operation.IsDone)
				{
					_operations.RemoveAt(i);
					operation.Finish();
				}
			}
		}

		/// 
		/// 销毁异步操作系统
		/// 当YooAssetDriver调用OnApplicationQuit驱动YooAsset的InternalDestroy时
		/// 调用此方法
		/// 
		public static void DestroyAll()
		{
			_operations.Clear();
			_watch = null;
			_maxTimeSlice = 0;
			_frameTime = 0;
		}

		/// 
		/// 开始处理异步操作类
		/// 
		public static void StartOperation(AsyncOperationBase operationBase)
		{
			_operations.Add(operationBase);
			operationBase.Start();
		}
	}
}

你可能感兴趣的:(C#,源码学习,Unity,unity,学习,游戏引擎)