public static void Free(IPool _object) { if (_object == null) { return; } ObjectPool objectPool; Type type = _object.GetType(); if (!poolDict.TryGetValue(type, out objectPool)) { objectPool = new ObjectPool(); objectPool.capacity = defaultCapacity; objectPool.objectQueue = new Queue <IPool>(); poolDict.Add(type, objectPool); } if (objectPool.objectQueue.Count < objectPool.capacity && !_object.GetInPool()) { objectPool.objectQueue.Enqueue(_object); _object.Recycle(); _object.SetInPool(true); } else { _object.OnDestroy(); } }
public void Recycle(IPool iPool) { Type type = iPool.GetType(); if (!_poolDic.ContainsKey(type)) { _poolDic[type] = new Queue <IPool>(); } iPool.Reset(); _poolDic[type].Enqueue(iPool); }
private void DrawEntitySelection() { _entyScroll = GUILayout.BeginScrollView(_entyScroll, false, true); { if (_selectedPool != null) { var type = _selectedPool.GetType(); if (type.BaseType != null && !type.BaseType.IsInterface && type.BaseType != typeof(object)) { type = type.BaseType; } if (!_entityFields.ContainsKey(type)) { var fieldInfo = type.GetField("_entities", BindingFlags.NonPublic | BindingFlags.Instance); _entityFields.Add(type, fieldInfo); } FieldInfo field = _entityFields[type]; var entities = field?.GetValue(_selectedPool) as IEnumerable; if (entities != null) { var index = 0; foreach (var entity in entities) { if (!string.IsNullOrEmpty(_searchString) && !entity.ToString().Contains(_searchString)) { continue; } if (GUILayout.Button($"{entity.ToString()}")) { _selectedEntity = entity; } if (index++ > 50) { break; } } } } } GUILayout.EndScrollView(); }