优化敌人生成器

本文关键字:人生 敌人 优化 | 更新日期: 2023-09-27 18:11:16

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

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

这个脚本可以正常工作,但在检查统一分析器后,我认为它没有得到足够的优化。如果有人有什么建议,我很乐意听听。

优化敌人生成器

好吧,我试着清理一下你的游戏循环-但实际上你的代码无论如何都会提前被剪掉,因为你在做任何工作之前都有敌人检查-所以我认为这真的更像是清理代码。

我认为要真正帮助你提高性能,我们需要看到一些其他的方法——如果你能说出为什么你认为它没有优化,那就太好了

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