/// <summary> /// Returns an object to the pool /// </summary> /// <param name="poolable">The PoolableObject to return to the pool</param> public void Return(PoolableObject poolable) { poolable.PoolReturned(); if (poolable.InUse == false) { return; } var item = (T)poolable; item.InUse = false; OnReturn?.Invoke(item); InUse.Remove(item); // This object is no longer inuse // When returning an object and the pool is larger than it wants to be destroy it if (PreferredPoolSize > 0 && Pool.Count > PreferredPoolSize) { Pool.Remove(item); Object.Destroy(item.gameObject); return; } // Object is available Available.Push(item); item.transform.SetParent(PoolParent); }
/// <summary> /// Creates a new instance of the object pool and initializes it. /// </summary> /// <param name="maxPoolSize">The maximum amount of objects that should be pooled maximum.</param> public ObjectPool(Constructor constructor, int maxPoolSize = 1000, OnReturn onReturn = null, OnGet onGet = null) { this.constructor = constructor; this.onReturn = onReturn; this.onGet = onGet; this.maxPoolSize = maxPoolSize; }
public void Return(T item) { unused.Push(item); used.Remove(item); item.OnReturned(this); OnReturn?.Invoke(item); }
private void Update() { #if UNITY_ANDROID // Android's return button is mapped to KeyCode.Escape if (Input.GetKeyDown(KeyCode.Escape)) { OnReturn?.Invoke(); } #endif }
void BackButtonClicked(object sender, EventArgs args) { if (IsBackBtnLocked) { IsBackBtnLocked = false; return; } CanDoFullScreen = false; OnReturn?.Invoke(); }
/// <summary> /// Returns an item to the pool to be reused /// </summary> /// <param name="item">The item to be returned</param> /// <param name="destroy">Should this item be destroyed</param> public virtual void Return(T item, bool destroy = false) { // Notify the object it has been returned to the pool try { if (item is IPoolNotifiable notifiable) { notifiable.PoolInUse(false); } } catch (Exception ex) { if (item is Object unityItem) { Debug.LogError(ex, unityItem); } else { Debug.LogError(ex); } } OnReturn?.Invoke(this, item); // Notify the object it has been returned to the pool try { if (item is IPoolNotifiablePost notifiable) { notifiable.PoolInUsePost(false); } } catch (Exception ex) { if (item is Object unityItem) { Debug.LogError(ex, unityItem); } else { Debug.LogError(ex); } } InUse.Remove(item); if (SetAvailableOnReturn) { SetAvailable(item, destroy); } }
protected void OnReturnCommit(object pb, ReturnEventArgs args) { // Call the Event given subscribers OnReturn?.Invoke(pb, args); }
protected virtual void OnReturnProc(System.EventArgs args) { OnReturn?.Invoke(this, args); }
private void GoBack() { OnReturn?.Invoke(); }
public void OnBackButtonTapped(object sender, EventArgs args) { OnReturn?.Invoke(); }
public void LoadMenu() { GameIsPause = false; OnReturn.Invoke(); }
public void LoadMenu() { OnReturn.Invoke(); }
private void GoBack(object sender, ElapsedEventArgs e) { OnReturn?.Invoke(); }