Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
 /// <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;
 }
Beispiel #3
0
        public void Return(T item)
        {
            unused.Push(item);
            used.Remove(item);

            item.OnReturned(this);

            OnReturn?.Invoke(item);
        }
Beispiel #4
0
        private void Update()
        {
#if UNITY_ANDROID
            // Android's return button is mapped to KeyCode.Escape
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                OnReturn?.Invoke();
            }
#endif
        }
Beispiel #5
0
        void BackButtonClicked(object sender, EventArgs args)
        {
            if (IsBackBtnLocked)
            {
                IsBackBtnLocked = false;
                return;
            }

            CanDoFullScreen = false;
            OnReturn?.Invoke();
        }
Beispiel #6
0
        /// <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);
 }
Beispiel #8
0
 protected virtual void OnReturnProc(System.EventArgs args)
 {
     OnReturn?.Invoke(this, args);
 }
 private void GoBack()
 {
     OnReturn?.Invoke();
 }
Beispiel #10
0
 public void OnBackButtonTapped(object sender, EventArgs args)
 {
     OnReturn?.Invoke();
 }
 public void LoadMenu()
 {
     GameIsPause = false;
     OnReturn.Invoke();
 }
Beispiel #12
0
 public void LoadMenu()
 {
     OnReturn.Invoke();
 }
Beispiel #13
0
 private void GoBack(object sender, ElapsedEventArgs e)
 {
     OnReturn?.Invoke();
 }