Unity 框架核心

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Framework {
	/// 
	/// 框架核心
	/// 
	public sealed class Core : MonoSingleton {
		/// 
		/// 调用队列
		/// 
		private static Queue m_InvokeQueue;

		protected override void Awake () {
			base.Awake ();

			DontDestroyOnLoad (gameObject);

			m_InvokeQueue = new Queue ();
		}

		private void OnDestroy () {
			m_InvokeQueue = null;
		}

		// Update is called once per frame
		void Update () {
			while (m_InvokeQueue.Count > 0) {
				lock (m_InvokeQueue) {
					InvokeObject excuteObject = m_InvokeQueue.Dequeue ();
					excuteObject.callback.Invoke (excuteObject.arg);
				}
			}
		}

		/// 
		/// 子线程调用
		/// 
		/// 回调函数
		/// 回调参数
		/// 返回调用结果
		public static bool Invoke (Action callback, object arg = null) {
			if (m_InvokeQueue == null)
				return false;

			lock (m_InvokeQueue) {
				InvokeObject excuteObject = new InvokeObject {
					callback = callback,
						arg = arg
				};
				m_InvokeQueue.Enqueue (excuteObject);
			}
			return true;
		}

		/// 
		/// 调用对象
		/// 
		private class InvokeObject {
			/// 
			/// 调用回调
			/// 
			public Action callback;

			/// 
			/// 调用参数
			/// 
			public object arg;
		}
	}

} 
  

 

你可能感兴趣的:(Unity基础框架)