Entitas  0.35.0
Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
ObjectCache.cs
1 using System;
2 using System.Collections.Generic;
3 
4 namespace Entitas {
5 
6  public class ObjectCache {
7 
8  Dictionary<Type, object> _objectPools;
9 
10  public ObjectCache() {
11  _objectPools = new Dictionary<Type, object>();
12  }
13 
14  public ObjectPool<T> GetObjectPool<T>() where T : new() {
15  object objectPool;
16  var type = typeof(T);
17  if(!_objectPools.TryGetValue(type, out objectPool)) {
18  objectPool = new ObjectPool<T>(() => new T());
19  _objectPools.Add(type, objectPool);
20  }
21 
22  return ((ObjectPool<T>)objectPool);
23  }
24 
25  public T Get<T>() where T : new() {
26  return GetObjectPool<T>().Get();
27  }
28 
29  public void Push<T>(T obj) {
30  ((ObjectPool<T>)_objectPools[typeof(T)]).Push(obj);
31  }
32 
33  public void RegisterCustomObjectPool<T>(ObjectPool<T> objectPool) {
34  _objectPools.Add(typeof(T), objectPool);
35  }
36 
37  public void Reset() {
38  _objectPools.Clear();
39  }
40  }
41 }