为什么使用Buttons会降低Unity中的场景加载速度?

时间:2017-07-11 16:05:02

标签: c# unity3d

所以,我试图通过按屏幕上的按钮来加载下一个场景。早些时候,当用户按下Enter键时,我就这样了,场景会加载,但是在意识到在iPad上工作太顺利之后,我决定切换到按钮。但按钮方法使下一个场景加载近两分钟,而按下Enter方法几乎是瞬间。任何人都知道为什么会这样?

//Press Enter method

if (Input.GetKeyDown(KeyCode.Tab))
{
    //If there's a space in the word, display a message and don't progress
    if (input.text.Contains(" "))
        warningWord.text = "Words cannot contain spaces!";
    //If the word is valid...
    else if (!string.IsNullOrEmpty(input.text))
    {
        //Display no error message
        warningWord.text = ("");
        //Set the string to the inputfield's contents
        wordToSpell = input.text;
        //Show the list of already typed words
        typedWords.text = typedWords.text + "\n" + input.text;
        //clear the input field
        input.text = "";
        //Set the word to all caps in order to keep it simple
        wordToSpell = wordToSpell.ToUpper();
        //Add the word to the list of words
        wordList.Add(wordToSpell);
    } 
}
//If the Return (Enter) key is pressed...
if (Input.GetKeyDown(KeyCode.Return))
{
    //If the user hasn't entered any words, display a message and don't progress
    if (wordList.Count == 0)
        warningWord.text = ("You haven't entered any words!");
    //If the user has not selected a difficulty, display a message and don't progress
    if (!easy.isOn && !medium.isOn && !hard.isOn)
        warningDifficulty.text = ("You haven't selected a difficulty!");
    //If a difficulty is selected and there are words that have been entered...
    if ((easy.isOn || medium.isOn || hard.isOn) && wordList.Count > 0)
    {
        //Set the backup list to the real list
        backupList = new List<string>(wordList);
        //Set the difficulties depending on the selected bubbles
        if (easy.isOn)
            PlayerPrefs.SetString("difficulty", "easy");
        else if (medium.isOn)
            PlayerPrefs.SetString("difficulty", "medium");
        else if (hard.isOn)
            PlayerPrefs.SetString("difficulty", "hard");
        //Make the canvas invisible as to hide the text boxes in the later scenes
        canvas.gameObject.SetActive(false);
        //Load the game
        SceneManager.LoadScene(1);
    }

}


//Button method
void Update()
{
    //As long as a difficulty is selected, don't display an error message
    if (easy.isOn || medium.isOn || hard.isOn)
        warningDifficulty.text = "";
    //If the enter word button is pressed...
    enterWord.onClick.AddListener(EnterButton);
    //If the start game button is pressed...
    startGame.onClick.AddListener(StartButton);
}
void EnterButton()
{
    //If there's a space in the word, display a message and don't progress
    if (input.text.Contains(" "))
        warningWord.text = "Words cannot contain spaces!";
    //If the word is valid...
    else if (!string.IsNullOrEmpty(input.text))
    {
        //Display no error message
        warningWord.text = ("");
        //Set the string to the inputfield's contents
        wordToSpell = input.text;
        //Show the list of already typed words
        typedWords.text = typedWords.text + "\n" + input.text;
        //clear the input field
        input.text = "";
        //Set the word to all caps in order to keep it simple
        wordToSpell = wordToSpell.ToUpper();
        //Add the word to the list of words
        wordList.Add(wordToSpell);
    }
}
void StartButton()
{
    //If the user hasn't entered any words, display a message and don't progress
    if (wordList.Count == 0)
        warningWord.text = ("You haven't entered any words!");
    //If the user has not selected a difficulty, display a message and don't progress
    if (!easy.isOn && !medium.isOn && !hard.isOn)
        warningDifficulty.text = ("You haven't selected a difficulty!");
    //If a difficulty is selected and there are words that have been entered...
    if ((easy.isOn || medium.isOn || hard.isOn) && wordList.Count > 0)
    {
        //Set the backup list to the real list
        backupList = new List<string>(wordList);
        //Set the difficulties depending on the selected bubbles
        if (easy.isOn)
            PlayerPrefs.SetString("difficulty", "easy");
        else if (medium.isOn)
            PlayerPrefs.SetString("difficulty", "medium");
        else if (hard.isOn)
            PlayerPrefs.SetString("difficulty", "hard");
        //Make the canvas invisible as to hide the text boxes in the later scenes
        canvas.gameObject.SetActive(false);
        //Load the game
        SceneManager.LoadScene(1);
    }
}

1 个答案:

答案 0 :(得分:2)

我准备出门,说你的Update循环导致了问题。您在每个帧上调用AddListener方法。这应该在Start方法上完成,因此它只执行一次,并在从场景中删除GameObject时删除。

所以它很可能只是多次添加相同的方法然后当你点击按钮时它试图加载调用方法x

您可以在 documentation

中查看MonoBehaviour课程中可用的各种活动