如何将具有多个场景的游戏转换为单个场景游戏?

时间:2016-05-31 05:57:50

标签: c# oop unity3d unity5

我已经制作了一个统一的游戏,其中有多个不同级别的场景,用户必须选择一个彩色块才能进入下一个级别,但我希望我的代码在一个场景中是否有一种方法可以转换我的将多场景游戏融入单一场景。 1级网格基础的代码是

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;
            }
        }
    } 

检查和加载级别2的代码是:

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);
                        }
                    }
                }

Level 2 GridBase的代码是;

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;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

首先,我不知道你为什么这样做。从技术上讲,这是场景的重点。但你仍然能做的就是将所有东西(1级和2级)放在一个场景中,然后定位和禁用属于摄像机外2级的任何东西。然后我不会调用Application.LoadLevel而是调用一个函数来将属于Level 1的所有内容移出屏幕,同时将Level 2移动到屏幕中。如果我在哪里,我将一个级别的所有对象组合为另一个GameObject的子对象(可能称为LevelX),这样就可以更容易地一次移动所有对象。这甚至可以让你在不同关卡之间进行过渡。如果您不想转换,只需立即更改位置。

请注意,因为根据级别的数量,您可能会遇到严重的性能问题。