Beispiel #1
0
        /// <summary>
        ///     Use a pooled GameObject.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="ObjectDisposedException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        public PooledObjectOperation <GameObject> Use()
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            var operation = new PooledObjectOperation <GameObject>(_pool,
                                                                   x =>
            {
                // If the returned instance has been destroyed outside the pool, do nothing.
                // InvalidOperationException will be thrown the next time this instance is retrieved from the pool.
                if (x.Equals(null))
                {
                    return;
                }

                x.transform.SetParent(PoolGameObject.transform);
                x.SetActive(false);
            });

            var instance = operation.Object;

            // It seems that this instance has been destroyed outside the pool.
            if (instance.Equals(null))
            {
                throw new InvalidOperationException(
                          "It seems that a GameObject you are trying to use has been destroyed outside the pool.");
            }

            instance.SetActive(true);

            return(operation);
        }
Beispiel #2
0
        /// <summary>
        ///     Binds the lifetime of the instance to the <see cref="gameObject" />.
        /// </summary>
        /// <param name="self"></param>
        /// <param name="gameObject"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static PooledObjectOperation <GameObject> BindTo(this PooledObjectOperation <GameObject> self,
                                                                GameObject gameObject)
        {
            if (gameObject == null)
            {
                self.Dispose();
                throw new ArgumentNullException(nameof(gameObject));
            }

            if (!gameObject.TryGetComponent(out MonoBehaviourDestroyedEventDispatcher eventDispatcher))
            {
                eventDispatcher = gameObject.AddComponent <MonoBehaviourDestroyedEventDispatcher>();
            }

            return(self.BindTo(eventDispatcher));
        }
Beispiel #3
0
        /// <summary>
        ///     Binds the lifetime of the instance to the <see cref="eventDispatcher" />.
        /// </summary>
        /// <param name="self"></param>
        /// <param name="eventDispatcher"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static PooledObjectOperation <GameObject> BindTo(this PooledObjectOperation <GameObject> self,
                                                                IEventDispatcher eventDispatcher)
        {
            if (eventDispatcher == null)
            {
                self.Dispose();
                throw new ArgumentNullException(nameof(eventDispatcher));
            }

            void OnDispatch()
            {
                if (!self.IsDisposed)
                {
                    self.Dispose();
                }

                eventDispatcher.OnDispatch -= OnDispatch;
            }

            eventDispatcher.OnDispatch += OnDispatch;
            return(self);
        }