简体   繁体   中英

How can i convert a game with multiple scene into a single scene game?

I have made a game in unity which have multiple scenes of different level in which user have to select a colored block to go the next level but i want my code to be in a single scene is there a way i can convert my multi scene game into a single scene. Code for level 1 grid base is

public IEnumerator Start() {
        grid = new GameObject[ySize, xSize];
        float x = xStart;
        float y = yStart;
        ScreenRandom = Random.Range(0, 3);
        if (ScreenRandom == 0)
        {
            img.color = UnityEngine.Color.red;
            text.text = "Select all the Red Objects";
            yield return new WaitForSeconds(time);
            Destroy(img);
            Destroy(text);
            int check = 1;
                for (int i = 0; i < ySize; i++)
                {
                    for (int j = 0; j < xSize; j++)
                    {

                        if (check <= 1)
                        {
                            GameObject rblock = Instantiate(RedPrefabs[Random.Range(0, 1)]) as GameObject;
                            rblock.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
                        rblock.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
                        rblock.transform.SetParent(canvas.transform);
                            grid[i, j] = rblock;
                            CountRed++;
                        }
                        else {
                            GameObject block = Instantiate(NonRedPrefab[Random.Range(0, 2)]) as GameObject;
                            block.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
                        block.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
                        block.transform.SetParent(canvas.transform);
                            grid[i, j] = block;
                        }
                        check++;
                        x += xWidth * space;
                    }
                    y -= yHeight * space;
                    x = xStart;
                }

        }
        if (ScreenRandom == 1)
        {
            img.color = UnityEngine.Color.blue;
            text.text = "Select all the Blue Objects";
            yield return new WaitForSeconds(time);
            Destroy(img);
            Destroy(text);
            int check = 1;
            for (int i = 0; i < ySize; i++)
            {
                for (int j = 0; j < xSize; j++)
                {

                    if (check <= 1)
                    {
                        GameObject rblock = Instantiate(BluePrefabs[Random.Range(0, 1)]) as GameObject;
                        rblock.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
                        rblock.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
                        rblock.transform.SetParent(canvas.transform);
                        grid[i, j] = rblock;
                        CountBlue++;
                    }
                    else {
                        GameObject block = Instantiate(NonBluePrefab[Random.Range(0, 2)]) as GameObject;
                        block.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
                        block.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
                        block.transform.SetParent(canvas.transform);
                        grid[i, j] = block;
                    }
                    check++;
                    x += xWidth * space;
                }
                y -= yHeight * space;
                x = xStart;
            }
        }
        if (ScreenRandom == 2)
        {
            img.color = UnityEngine.Color.yellow;
            text.text = "Select all the Yellow Objects";
            yield return new WaitForSeconds(time);
            Destroy(img);
            Destroy(text);
            int check = 1;
            for (int i = 0; i < ySize; i++)
            {
                for (int j = 0; j < xSize; j++)
                {

                    if (check <= 1)
                    {
                        GameObject rblock = Instantiate(YellowPrefabs[Random.Range(0, 1)]) as GameObject;
                        rblock.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
                        rblock.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
                        rblock.transform.SetParent(canvas.transform);
                        grid[i, j] = rblock;
                        CountYellow++;
                    }
                    else {
                        GameObject block = Instantiate(NonYellowPrefab[Random.Range(0, 2)]) as GameObject;
                        block.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
                        block.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
                        block.transform.SetParent(canvas.transform);
                        grid[i, j] = block;
                    }
                    check++;
                    x += xWidth * space;
                }
                y -= yHeight * space;
                x = xStart;
            }
        }
    } 

Code to check and Load Level 2 is:

void Update () {
        screen = GridControl.ScreenRandom;
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (screen == 0)
            {
                if (Physics.Raycast(ray, out hit))
                {
                    if (hit.collider.tag == "BlueBlock")
                    {
                        Destroy(hit.transform.gameObject);
                        print("GameOver");
                        Application.LoadLevel(0);
                    }
                    if (hit.collider.tag == "RedBlock")
                    {
                        RedCount++;
                        Destroy(hit.transform.gameObject);
                        Correct++;
                        Score.text = " " + Correct;
                        if (RedCount == GridControl.CountRed)
                        {
                            print("Next Level");
                            Application.LoadLevel(3);
                        }
                    }
                    if (hit.collider.tag == "YellowBlock")
                    {
                        Destroy(hit.transform.gameObject);
                        print("GameOver");
                        Application.LoadLevel(0);
                    }
                }
            }
            if (screen == 1)
            {
                if (Physics.Raycast(ray, out hit))
                {
                    if (hit.collider.tag == "BlueBlock")
                    {
                        BlueCount++;
                        Destroy(hit.transform.gameObject);
                        Correct++;
                        Score.text = " " + Correct;
                        if (BlueCount == GridControl.CountBlue)
                        {
                            print("Next Level");
                            Application.LoadLevel(3);
                        }
                    }
                    if (hit.collider.tag == "RedBlock")
                    {
                        Destroy(hit.transform.gameObject);
                        print("GameOver");
                        Application.LoadLevel(0);
                    }
                    if (hit.collider.tag == "YellowBlock")
                    {
                        Destroy(hit.transform.gameObject);
                        print("GameOver");
                        Application.LoadLevel(0);
                    }
                }
            }
            if (screen == 2)
            {
                if (Physics.Raycast(ray, out hit))
                {
                    if (hit.collider.tag == "BlueBlock")
                    {
                        Destroy(hit.transform.gameObject);
                        print("GameOver");
                        Application.LoadLevel(0);
                    }
                    if (hit.collider.tag == "RedBlock")
                    {
                        Destroy(hit.transform.gameObject);
                        print("Game Over");
                        Application.LoadLevel(0);
                    }
                    if (hit.collider.tag == "YellowBlock")
                    {
                        YellowCount++;
                        Destroy(hit.transform.gameObject);
                        Correct++;
                        Score.text = " " + Correct;
                        if (YellowCount == GridControl.CountYellow)
                        {
                            print("Next Level");
                            Application.LoadLevel(3);
                        }
                    }
                }

Code for Level 2 GridBase is;

public void Start()
    {
        grid = new GameObject[ySize, xSize];
        ScreenRandom = GridControl.ScreenRandom;
        float x = xStart;
        float y = yStart;
        if (ScreenRandom == 0)
        {
            int check = 1;
            for (int i = 0; i < ySize; i++)
            {
                for (int j = 0; j < xSize; j++)
                {

                    if (check <= 2)
                    {
                        GameObject rblock = Instantiate(RedPrefabs[Random.Range(0, 1)]) as GameObject;
                        rblock.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
                        rblock.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
                        rblock.transform.SetParent(canvas.transform);
                        grid[i, j] = rblock;
                        CountRed++;
                    }
                    else {
                        GameObject block = Instantiate(NonRedPrefab[Random.Range(0, 2)]) as GameObject;
                        block.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
                        block.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
                        block.transform.SetParent(canvas.transform);
                        grid[i, j] = block;
                    }
                    check++;
                    x += xWidth * space;
                }
                y -= yHeight * space;
                x = xStart;
            }

        }
        if (ScreenRandom == 1)
        {
            int check = 1;
            for (int i = 0; i < ySize; i++)
            {
                for (int j = 0; j < xSize; j++)
                {

                    if (check <= 2)
                    {
                        GameObject rblock = Instantiate(BluePrefabs[Random.Range(0, 1)]) as GameObject;
                        rblock.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
                        rblock.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
                        rblock.transform.SetParent(canvas.transform);
                        grid[i, j] = rblock;
                        CountBlue++;
                    }
                    else {
                        GameObject block = Instantiate(NonBluePrefab[Random.Range(0, 2)]) as GameObject;
                        block.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
                        block.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
                        block.transform.SetParent(canvas.transform);
                        grid[i, j] = block;
                    }
                    check++;
                    x += xWidth * space;
                }
                y -= yHeight * space;
                x = xStart;
            }
        }
        if (ScreenRandom == 2)
        {

            int check = 1;
            for (int i = 0; i < ySize; i++)
            {
                for (int j = 0; j < xSize; j++)
                {

                    if (check <= 2)
                    {
                        GameObject rblock = Instantiate(YellowPrefabs[Random.Range(0, 1)]) as GameObject;
                        rblock.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
                        rblock.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
                        rblock.transform.SetParent(canvas.transform);
                        grid[i, j] = rblock;
                        CountYellow++;
                    }
                    else {
                        GameObject block = Instantiate(NonYellowPrefab[Random.Range(0, 2)]) as GameObject;
                        block.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
                        block.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
                        block.transform.SetParent(canvas.transform);
                        grid[i, j] = block;
                    }
                    check++;
                    x += xWidth * space;
                }
                y -= yHeight * space;
                x = xStart;
            }
        }
    }

To start I don't know why would you do that. Technically that is the whole point of having scenes. But still what you could do is put everything (Level 1 and 2) in one scene and then position and disable whatever belongs to Level 2 outside the camera. Then instead of calling Application.LoadLevel I would call a function that will move everything that belongs to Level 1 out of the screen and at the same time moves Level 2 into the screen. If I where you I would group all the objects of one level as child of another GameObject (maybe called LevelX), that way it will be easier to move everything at once. This will even give you nice slide in transition between levels. If you don't want the transition just change the positions instantly.

Be carefull because depending on the amount of levels you could have serious performance issues.

You could use a prefab for the colored blocks and drag it into your different level scenes. If you now attach your behaviour script (managing what happens if a raycast hits it) to your prefab, all instances in all levels will have the same logic. That way you can reuse the same code base for different levels.

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