public PoolHandle(Type classT, int size, int allocGran) { _length = size; _allocGran = allocGran; _classT = classT; if (_length > 0) { _dataObjList = new ISmartObj[_length]; _stateList = new byte[_length]; for (int ii = 0; ii < _length; ++ii) { ISmartObj obj = (ISmartObj)Activator.CreateInstance(classT); if (obj == null) { _dataObjList = null; _length = 0; _stateList = null; return; } _stateList[ii] = 0; _dataObjList[ii] = obj; obj.index = ii; obj.handle = this; } } }
private void Grow() { int len = _length; _length += _allocGran; ISmartObj[] t = new ISmartObj[_length]; byte[] s = new byte[_length]; Array.Copy(_dataObjList, t, len); Array.Copy(_stateList, s, len); _fetchIndex = len; for (int ii = len; ii < _length; ++ii) { ISmartObj obj = (ISmartObj)Activator.CreateInstance(_classT); t[ii] = obj; s[ii] = 0; obj.index = ii; obj.handle = this; } _stateList = s; _dataObjList = t; #if UNITY_EDITOR Debug.LogWarning(string.Format("ObjectCachePool: {0}.PoolSize has been enlarged from {1} to {2}", _classT.ToString(), len, _length)); #endif }
public static void Release(this ISmartObj obj) { if (obj != null) { if (obj.handle == null) { Debug.LogError("PoolHandle: target object is not initialized from ObjectCachePool type:" + obj.GetType()); return; } obj.handle.Release(obj.index); obj.OnRelease(); } }