コード例 #1
0
    // Returns the specified object from the pool
    public InfiniteObject objectFromPool(int localIndex, ObjectType objectType)
    {
        InfiniteObject        obj         = null;
        int                   objectIndex = localIndexToObjectIndex(localIndex, objectType);
        List <InfiniteObject> objectPool  = objectsPool[objectIndex];
        int                   poolIndex   = objectPoolIndex[objectIndex];

        // keep a start index to prevent the constant pushing and popping from the list
        if (objectPool.Count > 0 && objectPool[poolIndex].isActive() == false)
        {
            obj = objectPool[poolIndex];
            objectPoolIndex[objectIndex] = (poolIndex + 1) % objectPool.Count;
            return(obj);
        }

        // No inactive objects, need to instantiate a new one
        InfiniteObject[] objects = null;
        switch (objectType)
        {
        case ObjectType.Platform:
            objects = platforms;
            break;

        case ObjectType.Scene:
            objects = scenes;
            break;

        case ObjectType.Obstacle:
            objects = obstacles;
            break;

        case ObjectType.Coin:
            objects = coins;
            break;

        case ObjectType.PowerUp:
            objects = powerUps;
            break;
        }

        obj = (GameObject.Instantiate(objects[localIndex].gameObject) as GameObject).GetComponent <InfiniteObject>();

        assignParent(obj, objectType);
        obj.setLocalIndex(localIndex);

        objectPool.Insert(poolIndex, obj);
        objectPoolIndex[objectIndex] = (poolIndex + 1) % objectPool.Count;
        return(obj);
    }