示例#1
0
    private void Start()
    {
        //SceneManager.LoadScene(1);
        xml = GetComponent <XmlMethods>();

        //Set player name to text.
        string playerName = PlayerPrefs.GetString("playerName");

        GameObject.Find("PlayerName").GetComponent <Text>().text = playerName;

        //Get won game title object.
        GameObject wonGame = GameObject.Find("WonGameTitle");

        if (!wonGame)
        {
            Debug.LogError("Failed to find the won game title object");
        }

        //Check if player won the game.
        if (PlayerPrefs.GetInt("wonGame") == 1)
        {
            //Won game.

            //Get the score from playerprefs.
            int score = PlayerPrefs.GetInt("score");

            //Check if current score is higher than best score.
            int bestScore = xml.GetCurrentValue(playerName);
            if (bestScore < score && bestScore != 0)
            {
                xml.UpdateScore(playerName, score);
            }
            else if (bestScore == 0)
            {
                xml.CreateChildInXml(playerName, score);
            }

            //Get the title text object.
            GameObject scoreObject = GameObject.Find("ScoreTitle");
            if (!scoreObject)
            {
                Debug.LogError("Failed to find the score title object");
            }

            //Set the text to the text object.
            scoreObject.transform.Find("ScoreTextBlack").GetComponent <Text>().text = score.ToString();
            scoreObject.transform.Find("ScoreTextWhite").GetComponent <Text>().text = score.ToString();

            //Set won game title.
            wonGame.transform.Find("TitleBlack").GetComponent <Text>().text = "CONGRATULATIONS";
            wonGame.transform.Find("TitleWhite").GetComponent <Text>().text = "CONGRATULATIONS";
        }
        else if (PlayerPrefs.GetInt("wonGame") == 2)
        {
            //Lose game.

            //Set won game title.
            wonGame.transform.Find("TitleBlack").GetComponent <Text>().text = "LOSE";
            wonGame.transform.Find("TitleWhite").GetComponent <Text>().text = "LOSE";
        }
    }