简体   繁体   English

Unity 场景更改

[英]Unity Scene Change

Im trying to make a scene change when all enemies have been defeated, here is what i have so far我试图在所有敌人都被击败后改变场景,这就是我目前所拥有的

public class areOpponentsDead : MonoBehaviour
{
    List<GameObject> listOfOpponents = new List<GameObject>();

    void Start()
    {
        listOfOpponents.AddRange(GameObject.FindGameObjectsWithTag("Enemy"));
        print(listOfOpponents.Count);
    }

    public void KilledOpponent(GameObject enemy)
    {
        if(listOfOpponents.Contains(opponent))
        {
            listOfOpponents.Remove(opponent);
        }

        print(listOfOpponents.Count);
    }

    public bool AreOpponentsDead()
    {
        if(listOfOpponents.Count <= 0)
        {
            Application.LoadScene("Level2");
        }
    }
}

i dont know if i should link this to an existing script or make a new one and how to connect this to the game.我不知道是否应该将其链接到现有脚本或制作新脚本以及如何将其连接到游戏。

I put this feature in 1 of my games and my enemy script performed a check every time an enemy died.我在我的 1 款游戏中加入了这个功能,每次有敌人死亡时,我的敌人脚本都会执行一次检查。 The function looked like: function 看起来像:

void Die()
    {
        Destroy(gameObject);
        if (gameManager != null)
        {
            if (gameManager.GetEnemies - 1 == 0)
            {
                gameManager.Invoke("WinLevel", 1f);
            }
        }
    }

It is common practice to use a Game Manager for managing your game so here, my gameManager script tracks the number of enemies in my game in the function GetEnemies .使用游戏管理器来管理游戏是一种常见的做法,所以在这里,我的gameManager脚本在 function GetEnemies中跟踪我的游戏中的敌人数量。 It also only makes sense that it is responsible for changing scenes (In the function WinLevel in my case).它也只有负责改变场景才有意义(在我的例子中是 function WinLevel )。 And this script is attached to a Game Manager object.此脚本附加到游戏管理器 object。

You can then either:然后,您可以:

  1. Make a reference to the Game Manager in the enemy script or...在敌人脚本中引用游戏管理器或...
  2. Make a static instance of the script制作脚本的 static 实例

Edit: The GameManager script has the following code:编辑: GameManager 脚本具有以下代码:

public class GameManager : MonoBehaviour
{
    // The integer is the index for the current scene and the string is the name of the scene
    public string nextLevel = "2";
    public int levelToUnlock = 2;

    public GameObject winMenu;
    public GameObject gameplayUI;
    public GameObject backgroundMusic;
    GameObject player;
    Rigidbody2D playerRigidbody2D;
    public AudioClip winMusic;
    public int GetEnemies { get { return GameObject.FindGameObjectsWithTag("Enemy").Length; } }
    private void Start()
    {
        GoogleMobileAdsDemoScript._Adins.DestroyBanner();
    }
    public void WinLevel()
    {
        GoogleMobileAdsDemoScript._Adins.ShowBannerAd();
        player = GameObject.FindGameObjectWithTag("Player");
        playerRigidbody2D = player.GetComponent<Rigidbody2D>();
        playerRigidbody2D.simulated = false;
        PlayerPrefs.SetInt("levelReached", levelToUnlock);
        winMenu.SetActive(true);
        gameplayUI.SetActive(false);
        backgroundMusic.GetComponent<AudioSource>().Stop();
        backgroundMusic.GetComponent<AudioSource>().PlayOneShot(winMusic);
    }
    public void NextLevel()
    {
        GoogleMobileAdsDemoScript._Adins.DestroyBanner();
        SceneManager.LoadScene(nextLevel);
    }
    public void RestartLevel()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM