Esempio n. 1
0
    public void SpawnTheObject()
    {
        Quaternion rot        = transform.rotation;
        Vector3    pos        = transform.position;
        float      halfLength = length / 2.0f; //x-axis
        float      halfWidth  = width / 2.0f;  //z-axis
        float      halfHeight = height / 2.0f; //y-axis

        switch (whereToSpawn)
        {
        case WHERE_TO_SPAWN.TARGET_VECTOR3:
            rot = spawnRotation;
            pos = spawnPosition;
            break;

        case WHERE_TO_SPAWN.TARGET_TRANSFORM:
            rot = targetRotationTransform.rotation;
            pos = targetPositionTransform.position;
            break;

        case WHERE_TO_SPAWN.RANDOM_AREA_IN_BOUNDS:
            if (randomBounds == RANDOM_BOUNDS.SPHERE)
            {
                pos = Random.insideUnitSphere * radius;
            }
            else
            {
                float rndX = Random.Range(-halfLength, halfLength);
                float rndY = Random.Range(-halfHeight, halfHeight);
                float rndZ = Random.Range(-halfWidth, halfWidth);
                pos = new Vector3(rndX, rndY, rndZ);
            }
            break;
        }

        //now we know pos and rot to spawn

        SingletonManager m = SingletonManager.GetInstance();

        if (m != null)
        {
            IPoolManager p = m.GetPoolManager();
            if (p != null)
            {
                ObjectPool pool = p.GetObjectPool(poolName);
                if (pool != null)
                {
                    PooledObject pooled = pool.GetPooledObject(objectName);
                    if (pooled != null)
                    {
                        GameObject objSpawned = pooled.Spawn(pos, rot);
                        if (objSpawned == null)
                        {
                            Debug.LogWarning("The pool \"" + poolName + "\" had no more allocated \"" + objectName + "\" objects able to be spawned.");
                        }
                    }
                    else
                    {
                        Debug.LogError("The SpawnObject tried to spawn the object \"" + objectName + "\" from the pool \"" + poolName + "\" but it didn't exist.");
                    }
                }
                else
                {
                    Debug.LogError("The SpawnObject tried to access the pool \"" + poolName + "\" and it doesn't exist.");
                }
            }
        }
    }