コード例 #1
0
    IEnumerator SwapLevelTimer()
    {
        if (!titleScreen)
        {
            try //chose to do this because an error that wasn't affecting the flow of the game was coming up
            {
                playerController.ResetMoveSpeedAndAttackTime();
            }
            catch
            {
                //pass
            }
            characterSelector.GetCharacterObject().SetActive(false);
        }

        yield return(new WaitForSeconds(swapLevelTime));

        if (selectRandomScene)                                                        //if the selectRandomScene boolean is active, then the gameobject the script is attached to wants to randomly load levels from a list
        {
            if (scenesListLength > 0 && (scenesListLength != chosenScenesListLength)) //if the array doesn't have any levels to select from, the code will not be run to avoid any errors
            {
                int  randomSceneNum;
                bool available = false;
                while (!available)                                          //if the specific random number that was chosen isn't in the list of levels (that holds the build indexes), then keep randomly choosing
                {
                    randomSceneNum = Random.Range(1, scenesListLength + 1); //set to start at one since the Title Screen's index is 0 //had to add a +1 since the starting point was changed

                    if (chosenScenesListLength > 0)
                    {
                        if (CheckChosenNum(randomSceneNum))
                        {
                            levelTransitionManager.AddChosenScene(randomSceneNum);
                            levelTransitionManager.SetLevelIndex(randomSceneNum);
                            available = true;
                        }
                    }
                    else
                    {
                        levelTransitionManager.AddChosenScene(randomSceneNum);
                        levelTransitionManager.SetLevelIndex(randomSceneNum);
                        available = true;
                    }
                }
                if (!titleScreen && returnPlayerHealth)
                {
                    playerHealthManager.SetCurrentHealth(playerHealthManager.GetCurrentHealth() + 50); //returns the player's health to the specified value if the fillPlayerHealth boolean is selected
                    //playerHealthManager.SetCurrentHealth(playerHealthManager.GetMaxHealth()); //returns the player's health to full if the fillPlayerHealth boolean is selected
                }
                levelTransitionManager.FadeToLevel(); //loads the randomly selected level
            }
            else //will load the VictoryScreen if all levels are complete
            {
                gameOver = true;
                characterSelector.TurnOffCanvas();
                levelTransitionManager.SetLevelIndex(SceneManager.sceneCountInBuildSettings - 2); //the penultimate level index is the Victory Screen but needs to be -2 because the index starts at 1
                levelTransitionManager.FadeToLevel();
            }
        }
        else
        {
            if (levelToLoad == "GameOverScreen") //string reference is slightly dangerous, but works fine
            {
                gameOver = true;
                characterSelector.TurnOffCanvas();
            }
            SceneManager.LoadScene(levelToLoad); //loads a selected scene since random selection is not the desired functionality
        }
    }