简体   繁体   中英

Unity 2D: How can I flip through a specific sequences of scenes, not lose my score data and then retrieve my score data?

I'm new to unity and C# and I have a few questions. Here's the context: I've created 7 different scenes which are basically different mini-games. My goal is the following: At the start, the participant selects mode 1 or mode 2. If they chose mode 1, scenes 1,2 and 3 should be played next. If they choose mode 2, scenes 2,4,5,6,7 should be played next. At the end of the mini-games, I want to show the participant's score for all mini-games with visual graphics.

So here are my questions:

  1. How can I set my game in such a way that when the participant selects 'mode 1', the specific scenes 1, 2 and 3 are played? Or if they select 'mode 2', scenes 2, 4, 5, 6, and 7 are played? And is their a way to make them play in a random order?

  2. How can I save the scores of every mini-game so I can access them at the end and use the scores to make graphics?

For the scene order, I did somethig like this:

void Start()
    {
        RandNum = Random.Range(0, 8);

        if (RandNum == 0)
        {
            SceneManager.LoadScene(MiniGame1);
        }

        if (RandNum == 1)
        {
            SceneManager.LoadScene(MiniGame2);
        }

        if (RandNum == 2)
        {
            SceneManager.LoadScene(MiniGame3);
        }

...etc. Would this work?

And for the score saves, when I searched online, I found that I could use PlayPrefs.SetIn("Score mini-game 1", score1) for example. Is this a good method? and if so, how would I retrieve this information at the end?

Thank you so much for your help:)

To get your score from PlayerPrefs use PlayerPrefs.GetInt

For Scene loading sequence you can create some ScriptableObject with level queue presets (like int arrays for each mode).

For overall architecture I would create some gameobject that will be preserved through all game lifecycle (ie not being destroyed after new scene is loaded) and use that gameobject to hold your game manager. For that use DontDestroyOnLoad . But this method may be not ideal for your case, so give it some thoughts before choosing some approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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