Esempio n. 1
0
        private void SpawnObject()
        {
            // Spawn object without any callback.
            // You can get an error if you forgot to add this prefab to ObjectPool component.
            // In this way you cant catch and process this error.

            pool.GetObject(prefab);

            // Spawn object and check if it was spawned.
            // Returns true if object was spawned.

            if (pool.TryGetObject(prefab) == true)
            {
                Debug.Log("Prefab spawned");
            }
            else
            {
                Debug.Log("Prefab wasn't spawned");
            }

            // Spawn object and get a callback with this object.
            // Due to adressables asynchronous loading you can't get object in moment you get it from pool,
            // because unity need a bit of time to load it.

            if (pool.TryGetObject(prefab, PoolCallback) == true)
            {
                Debug.Log("Prefab spawned");
            }
            else
            {
                Debug.Log("Prefab wasn't spawned");
            }
        }
Esempio n. 2
0
    private void Start()
    {
        _ballsPool = new ObjectsPool(1, _ball);

        Hoop.Goal += delegate(){
            Vector2 NewBallPosition = new Vector2(Random.Range(-15, 15), Random.Range(-10, 18));

            while (NewBallPosition == OldBallPosition)
            {
                NewBallPosition = new Vector2(Random.Range(-15, 15), Random.Range(-10, 18));
            }

            _ball.ReturnToPool();
            _ball = _ballsPool.GetObject();
            _ball.GetComponent <BallBehaviour>().SetBall(NewBallPosition);
            OldBallPosition = NewBallPosition;
        };
    }