生成所需数量的预制件 [英] Spawning the desired number of prefabs

查看:64
本文介绍了生成所需数量的预制件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用以下代码实例化所需数量的预制件?我需要生成一个(只有一个)玩家预制件,X个敌人和一个(只有一个)最终游戏预制件.

How can I instantiate the desired number of prefabs using the following code? I need to spawn ONE (and only one) player prefab, X enemies and ONE (and only one) end game prefab.

    private void GenerateEnemies(int xMax, int zMax)
    {
        GameObject landscape = new GameObject("ENEMIES");


        for (int z = 0; z < zMax; z++)
        {
            for (int x = 0; x < xMax; x++)
            {
                randNum = Random.Range(0, 100);

                if (randNum < 10 )
                {
                    Instantiate(enemy1, new Vector3(x * 10, 0, z * 10), Quaternion.Euler(0, 0, 0));//, landscape.transform);
                }
                else if (randNum < 20)
                {
                    Instantiate(enemy2, new Vector3(x * 10, 0, z * 10), Quaternion.Euler(0, 0, 0));//, landscape.transform);
                }

                else if (randNum < 30)
                {
                    Instantiate(enemy3, new Vector3(x * 10, 0, z * 10), Quaternion.Euler(0, 0, 0));//, landscape.transform);
                }

                else if (randNum < 40)
                {
                    Instantiate(enemy4, new Vector3(x * 10, 0, z * 10), Quaternion.Euler(0, 0, 0));//, landscape.transform);
                }

                else if (randNum < 50)
                {
                    Instantiate(enemy5, new Vector3(x * 10, 0, z * 10), Quaternion.Euler(0, 0, 0));//, landscape.transform);
                }

            }
        }
    }

推荐答案

只需简单地在循环之外做一次事情!

Well simply do your one-time things outside the loop!

randNum = Random.Range(0, 100);

,然后仅使用5种不同的情况,并且仅当值小于 50 时才使用(因此,在一半的情况下,什么也没有发生...).如果打算这样做..还可以..否则,我宁愿使用一个列表和随机索引:

and then you use only 5 different cases and only if the value is smaller 50 (so about in half of the cases nothing happens at all ...). If this was intended .. ok-ish .. otherwise I would rather use a list and random indices:

// HINT: Rather have a list for your prefabs
// this shrinks your code a lot
public List<GameObject/*or whatever type*/> eneymPrefabs = new List<GameObject>();
public Gamebject playerPrefab;
public GameObject endGamePrefab;

private void GenerateEnemies(int xMax, int zMax)
{
    var landscape = new GameObject("ENEMIES");

    // Do these only once
    // store the references in case you need them later
    var player = Instantiate(playerPrefab);
    var endGame = Instantiate(endGamePrefab);

    for (int z = 0; z < zMax; z++)
    {
        for (int x = 0; x < xMax; x++)
        {
            // simply pick a random index from the prefab list
            int randIndex = Random.Range(0, eneymPrefabs.Count);

            // and get the according random prefab
            var enemyPrefab = enemyPrefabs[randIndex];

            if(enemyPrefab) Instantiate(enemyPrefab, new Vector3(x * 10, 0, z * 10), Quaternion.identity /*, landscape.transform*/);
        }
    }
}


或者是Draco18s提到的加权列表的示例


Or an example for the weighted list mentioned by Draco18s

[Serializable]
public class WeightedPrefab
{
    public GameObject Prefab;
    public int Weight = 1;
}

public List<WeightedPrefab> weightedEnemyPrefabs;
public Gamebject playerPrefab;
public GameObject endGamePrefab;

private void GenerateEnemies(int xMax, int zMax)
{
    // create a temp list using the weights and random index on this one
    var enemyPrefabs = new List<GameObject>();
    foreach(var item in weightedEnemyPrefabs)
    {
        for(var i = 0; i < item.Weight; i++)
        {
            enemyPrefabs.Add(item.Prefab);
        }
    }

    // Rest stays the same

    var landscape = new GameObject("ENEMIES");

    // Do these only once
    // store the references in case you need them later
    var player = Instantiate(playerPrefab);
    var endGame = Instantiate(endGamePrefab);

    for (int z = 0; z < zMax; z++)
    {
        for (int x = 0; x < xMax; x++)
        {
            // simply pick a random index from the prefab list
            int randIndex = Random.Range(0, eneymPrefabs.Count);

            // and get the according random prefab
            var enemyPrefab = enemyPrefabs[randIndex];

            if(enemyPrefab) Instantiate(enemyPrefab, new Vector3(x * 10, 0, z * 10), Quaternion.identity /*, landscape.transform*/);
        }
    }
}

如果有意并非在所有情况下都实例化一个敌人,您仍然可以使用两种方法,只需将预制参考留空即可.对于该索引,将不会实例化任何内容.

If it was intentional that not in every case an enemy is instantiated you can still use both approaches and simply leave a prefab reference empty → for that index nothing will be instantiated.

敌人和最终游戏应该是网格的一部分

the enemy and end game should be part of the grid

在这种情况下,我首先将整个网格组合写入列表.从该列表中选择两个随机条目,并将玩家和endGame放置在此处.然后封锁这两个网格位置,不要在那里产生敌人:

In this case I would first write the entire grid combinations into a list. Pick two random entries from this list and place the player and the endGame there. Then block these two grid positions and do not spawn enemies there:

[Serializable]
public class WeightedPrefab
{
    public GameObject Prefab;
    public int Weight = 1;
}

public List<WeightedPrefab> weightedEnemyPrefabs;
public Gamebject playerPrefab;
public GameObject endGamePrefab;

private void GenerateEnemies(int xMax, int zMax)
{
    // Create a list of all awailable grid positions
    var gridPositions = new List<Vector2Int>();
    for (int z = 0; z < zMax; z++)
    {
        for (int x = 0; x < xMax; x++)
        {
            gridPositions.Add(new Vector2Int(x,z));
        }
    }

    // pick the two random positions for player and endgame
    var playerPosIndex = Random.Range(0, gridPositions.Count);
    var playerPos = gridPositions[playerPosIndex];
    gridPositions.RemoveAt(playerPosIndex);

    var endGamePosIndex = Random.Range(0, gridPositions.Count);
    var endGamePos = gridPositions[endGamePosIndex];
    gridPositions.RemoveAt(endGamePosIndex);

    // create a temp list using the weights and random index on this one
    var enemyPrefabs = new List<GameObject>();
    foreach(var item in weightedEnemyPrefabs)
    {
        for(var i = 0; i < item.Weight; i++)
        {
            enemyPrefabs.Add(item.Prefab);
        }
    }

    var landscape = new GameObject("ENEMIES");

    // Do these only once
    // store the references in case you need them later
    var player = Instantiate(playerPrefab, new Vector3(payerPos.x * 10, 0, playerPos.y * 10), Quaternion.identity /*, landscape.transform*/);
    var endGame = Instantiate(endGamePrefab, new Vector3(endGamePos.x * 10, 0, endGamePos.y * 10), Quaternion.identity /*, landscape.transform*/);

    for (int z = 0; z < zMax; z++)
    {
        for (int x = 0; x < xMax; x++)
        {
            // Now simply ignore the playerPos and endGamePos
            if(x == playerPos.x && z == playerPos.y) continue;
            if(x == endGamePos.x && z == endGamePos.y) continue;

            // pick a random index from the prefab list
            int randIndex = Random.Range(0, eneymPrefabs.Count);

            // and get the according random prefab
            var enemyPrefab = enemyPrefabs[randIndex];

            // do nothing if enemyPrefab is null otherwise instantiate
            if(enemyPrefab) Instantiate(enemyPrefab, new Vector3(x * 10, 0, z * 10), Quaternion.identity /*, landscape.transform*/);
        }
    }
}

这篇关于生成所需数量的预制件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆