Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 生成所需数量的预制件_C#_List_Unity3d_Spawn - Fatal编程技术网

C# 生成所需数量的预制件

C# 生成所需数量的预制件,c#,list,unity3d,spawn,C#,List,Unity3d,Spawn,如何使用以下代码实例化所需数量的预置?我需要生成一个(并且只有一个)玩家预置,X个敌人和一个(并且只有一个)结束游戏预置 private void GenerateEnemies(int xMax, int zMax) { GameObject landscape = new GameObject("ENEMIES"); for (int z = 0; z < zMax; z++) { for (i

如何使用以下代码实例化所需数量的预置?我需要生成一个(并且只有一个)玩家预置,X个敌人和一个(并且只有一个)结束游戏预置

    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);
                }

            }
        }
    }
private void GenerateEnemies(int-xMax,int-zMax)
{
游戏对象景观=新游戏对象(“敌人”);
对于(intz=0;z
好吧,只需在循环之外做你的一次性事情

然后只使用5种不同的情况,并且仅当值小于
50
(因此大约一半的情况下根本没有发生任何情况…)。如果这是有意的。。好的。。否则,我宁愿使用列表和随机索引:

// 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*/);
        }
    }
}
//提示:您应该为预置创建一个列表
//这会大大缩减您的代码
public List eneymprefables=新列表();
公共游戏对象playerPrefab;
公共游戏对象EndGamePrefact;
私有void GenerateEnemies(int-xMax,int-zMax)
{
var景观=新游戏对象(“敌人”);
//只做一次
//存储引用,以备以后需要
var player=实例化(playerPrefab);
var endGame=实例化(endGamePrefab);
对于(intz=0;z

或者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*/);
        }
    }
}
[可序列化]
公共类加权预制
{
公共游戏对象预制;
公共整数权重=1;
}
公共列表权重edenemyprefabs;
公共游戏对象playerPrefab;
公共游戏对象EndGamePrefact;
私有void GenerateEnemies(int-xMax,int-zMax)
{
//使用此列表上的权重和随机索引创建临时列表
var enemyprefables=新列表();
foreach(WeightedEndEmypRefabs中的var项)
{
对于(变量i=0;i
如果不是每种情况下都有敌人被实例化,那么您仍然可以使用这两种方法,只需将预制引用留空&rightarrow;对于该索引,不会实例化任何内容



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

在本例中,我将首先将整个网格组合写入一个列表。从这个列表中随机选择两个条目,并将玩家和终局放在那里。然后封锁这两个网格位置,不要在那里繁殖敌人:

[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*/);
        }
    }
}
[可序列化]
公共类加权预制
{
公共游戏对象预制;
公共整数权重=1;
}
公共列表权重edenemyprefabs;
公共游戏对象playerPrefab;
公共游戏对象EndGamePrefact;
私有void GenerateEnemies(int-xMax,int-zMax)
{
//创建所有可用栅格位置的列表
var gridPositions=新列表();
对于(intz=0;z[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*/);
        }
    }
}
 for (int z = 0; z < zMax; z++)
 {
   for (int x = 0; x < xMax; x++)
   {
     randNum = Random.Range(1, 11);

     if (randNum == 1) Instantiate(enemy1, new Vector3(x * 10, 0, z * 10), Quaternion.Euler(0, 0, 0));//, landscape.transform);
     if (randNum == 2) Instantiate(enemy2, new Vector3(x * 10, 0, z * 10), Quaternion.Euler(0, 0, 0));//, landscape.transform);
     if (randNum == 3) Instantiate(enemy3, new Vector3(x * 10, 0, z * 10), Quaternion.Euler(0, 0, 0));//, landscape.transform);
     if (randNum == 4) Instantiate(enemy4, new Vector3(x * 10, 0, z * 10), Quaternion.Euler(0, 0, 0));//, landscape.transform);
     if (randNum == 5) Instantiate(enemy5, new Vector3(x * 10, 0, z * 10), Quaternion.Euler(0, 0, 0));//, landscape.transform);
   }
 }