如何将实例化的 tilesprefabs 从停用的 tiles 交换为 activetiles?

How to exchange instantiated tilesprefabs from deactivated tiles to activetiles?

大家好,我一直在制作和试验 3d 无尽 运行ner 游戏,但遇到了一个问题。这就是我要实现的目标。我有两个名为 activeTiles 和 deactivatedTiles 的游戏对象列表。根据我的想法,我想首先将所有预制瓷砖添加到 deactivatedtTiles 列表中。接下来我有一个 float amtTileOnScreen 变量,它控制玩家面前的方块数量 运行。然后我通过考虑屏幕上的瓷砖数量从停用瓷砖列表中随机选择一个瓷砖到激活瓷砖并将其放在玩家面前。用过的瓷砖被放回停用瓷砖列表,整个循环开始。

问题是我该如何实现?帮助将不胜感激。

这是我试过的。

public class TileManager : MonoBehaviour 
{

    public GameObject[] tilePrefabs;

    private Transform playerTransform;
    private float spawnZ = -12f;
    private float tileLength = 24.0f;
    private int amtOfTilesOnScreen = 5;
    private float safeZone = 56.0f;
    private GameObject spawnedTile;



    public static List<GameObject> activeTiles;
    public static List<GameObject> deactivatedTiles;
    private int lastPrefabIndex = 0;
    private Vector3 transformTiles;

    // Use this for initialization
    void Start () 
    {

        activeTiles = new List<GameObject>();
        deactivatedTiles = new List<GameObject>();
        playerTransform = GameObject.FindGameObjectWithTag("Player").transform;

        for (int i = 0; i < amtOfTilesOnScreen; i++)
        {

            if (i < 1)
            {
                activeTiles.Add(SpawnTileAtFront(0));
            }
            else
            {
                activeTiles.Add(SpawnTileAtFront());
            }
        }


    }

    void Update () 
    {    
        if (playerTransform.position.z - safeZone > (spawnZ - amtOfTilesOnScreen * tileLength))
        {
            for (int i = 0; i < tilePrefabs.Length; i++)
            {
                spawnedTile = SpawnTileAtFront();
                deactivatedTiles.Add(spawnedTile);
                Debug.Log(deactivatedTiles.Count);
            }
            if (activeTiles.Count < (amtOfTilesOnScreen + 1))
            {
                activeTiles.Add(GetRandomDeactivatedTile());
                MoveTileToTheFront(GetRandomDeactivatedTile());
            }
            else
            {
                var disposeTile = activeTiles[0];
                deactivatedTiles.Add(disposeTile);
                DisposeActiveTiles(0);
            }

        }

    }

    private void MoveTileToTheFront(GameObject tile)
    {
        tile.transform.position = Vector3.forward * spawnZ;
        spawnZ += tileLength;
    }

    private GameObject SpawnTileAtFront(int prefabIndex = -1)
    {
        GameObject go;
        if (prefabIndex == -1)
        {
            go = Instantiate(tilePrefabs[RandomPrefabIndex()]) as GameObject;
        }
        else
        {
            go = Instantiate(tilePrefabs[prefabIndex]) as GameObject;
        }

        go.transform.SetParent(transform);
        MoveTileToTheFront(go);
        return go;
    }

    private void DisposeActiveTiles(int index)
    {
        GameObject unusedTile = activeTiles[index];
        activeTiles.RemoveAt(index);
        deactivatedTiles.Add(unusedTile);
    }

    private GameObject GetRandomDeactivatedTile()
    {
        if (deactivatedTiles.Count == 0)
            return null;
        int randomIndex = Random.Range(0, deactivatedTiles.Count);
        GameObject unusedTile = deactivatedTiles[randomIndex];
        deactivatedTiles.RemoveAt(randomIndex);
        return unusedTile;
    }

    private int RandomPrefabIndex()
    {
        if (tilePrefabs.Length <= 0)
        {
            return 0;
        }

        int randomIndex = lastPrefabIndex;
        while (randomIndex == lastPrefabIndex)
        {
            randomIndex = Random.Range(0, tilePrefabs.Length);
        }
        lastPrefabIndex = randomIndex;
        return lastPrefabIndex;
    }
}
void Update () 
{    
    while (playerTransform.position.z - safeZone > (spawnZ - amtOfTilesOnScreen * tileLength))
    {
        // we need to add a new tile in front of the player
        GameObject t;

        if (deactivatedTiles.Count == 0) {
            // no deactivated tiles so we need to instantiate a new tile
            t = SpawnTileAtFront ();
        } else {
            // otherwise take deactivated tile into use
            t = GetRandomDeactivatedTile ();
            MoveTileToTheFront (t);
        }
        // new tile is now active tile
        activeTiles.Add (t);

        // take oldest active tile and move it to deactivated list
        DisposeActiveTiles(0);
    }

}