优化敌人生成器

时间:2015-09-05 17:06:11

标签: c# unity3d monodevelop

我创建了一个用于在现场生成敌人的脚本。它是一个基于波浪的敌人发生器,一群敌人被生成然后它等待直到它们全部死亡然后进入下一轮。这是代码:

while (GameObject.FindGameObjectWithTag("Player") != null)
    {
        Spawner();
        if (waveCount == 1)
        {
            sound.clip = startRoundAlert;
            sound.Play();
            Instantiate(enemyz[0], SpawnPoint, Quaternion.identity);
            waveCount++;

        }
        else if (waveCount == 2)
        {
            if(GameObject.FindGameObjectsWithTag("Enemy").Length == 0)
            {
                sound.clip = endRoundAlert;
                sound.Play();
                roundText.text = "Get ready for round" + waveCount;
                yield return new WaitForSeconds(waveWait);
                roundText.text = "";

                sound.clip = startRoundAlert;
                sound.Play();

                Instantiate(enemyz[0], SpawnPoint, Quaternion.identity);
                yield return new WaitForSeconds(4);
                Spawner();
                Instantiate(enemyz[0], SpawnPoint, Quaternion.identity);
                waveCount++;

            }
        }
        else if (waveCount == 3)
        {
            if (GameObject.FindGameObjectsWithTag("Enemy").Length == 0)
            {
                sound.clip = endRoundAlert;
                sound.Play();
                roundText.text = "Get ready for round" + waveCount;
                yield return new WaitForSeconds(waveWait);
                roundText.text = "";

                sound.clip = startRoundAlert;
                sound.Play();

                Instantiate(enemyz[1], SpawnPoint, Quaternion.identity);
                waveCount++;

            } 
        }

这个脚本工作正常,但在使用unity profiler检查后,我认为它不够优化。如果有人建议让它变得更好,我会很高兴听到它。

1 个答案:

答案 0 :(得分:1)

好吧,我试图稍微清理你的游戏循环 - 但是你的代码实际上会提前删除,因为你在做任何工作之前都有敌人检查 - 所以我认为这更像是一个清理代码

我认为真的能帮助你提高表现,我们需要看一些其他的方法 - 如果你能说出为什么你认为它没有得到优化就会很棒

while(GameObject.FindGameObjectWithTag("Player") != null)
{
    Spawner(); //What does this do?
    if(GameObject.FindGameObjectsWithTag("Enemy").Length == 0)
    {
        StartNextRound();
        waveCount++;
    }
}

private void StartNextRound()
{
    switch(waveCount) 
    {
        case 1:
            Instantiate(enemyz[0], SpawnPoint, Quaternion.identity);
            break;
        case 2:
            NewRoundSetup()
            Instantiate(enemyz[0], SpawnPoint, Quaternion.identity);
            yield return new WaitForSeconds(4);
            Spawner();
            Instantiate(enemyz[0], SpawnPoint, Quaternion.identity);
            break;
        case 3:
            NewRoundSetup()
            Instantiate(enemyz[1], SpawnPoint, Quaternion.identity);
            break;
    }
}

private void NewRoundSetup() 
{
    sound.clip = endRoundAlert;
    sound.Play();
    roundText.text = "Get ready for round" + waveCount;
    yield return new WaitForSeconds(waveWait);
    roundText.text = "";

    sound.clip = startRoundAlert;
    sound.Play();
}