From 53d0be4cac72d8beb61587e2e92ca9de773bdf9d Mon Sep 17 00:00:00 2001 From: kbengine Date: Sat, 9 Apr 2016 11:34:47 +0800 Subject: [PATCH] up --- ObjectPool.cs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 ObjectPool.cs diff --git a/ObjectPool.cs b/ObjectPool.cs new file mode 100644 index 0000000..df8198f --- /dev/null +++ b/ObjectPool.cs @@ -0,0 +1,44 @@ +using UnityEngine; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Threading; + + +namespace KBEngine +{ + /// + /// 简单的对象池 + /// + /// 对象类型 + public class ObjectPool where T : new() + { + private static LinkedList _objects = new LinkedList(); + + public static T getObject() + { + lock (_objects) + { + if (_objects.First != null) + { + T v = _objects.First.Value; + _objects.RemoveFirst(); + return v; + } + else + { + return new T(); + } + } + } + + public static void putObject(T item) + { + lock (_objects) + { + _objects.AddLast(item); + } + } + } + +}