簡體   English   中英

如何將多場景游戲轉換為單場景游戲?

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

我制作了一個統一游戲,其中有多個不同級別的場景,其中用戶必須 select 一個彩色塊到下一個級別 go 但我希望我的代碼在一個場景中有沒有辦法可以轉換我的多場景游戲成一個單一的場景。 一級網格基礎的代碼是

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

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 級和 2 級)放在一個場景中,然后在攝像機外定位和禁用屬於 2 級的任何內容。 然后,我將調用一個函數,而不是調用Application.LoadLevel該函數會將屬於 Level 1 的所有內容移出屏幕,同時將 Level 2 移入屏幕。 如果我將一個級別的所有對象分組為另一個 GameObject(可能稱為 LevelX)的子級,這樣可以更輕松地一次移動所有對象。 這甚至可以讓您在級別之間的過渡中獲得漂亮的幻燈片。 如果您不想要過渡,只需立即更改位置。

請小心,因為根據級別的數量,您可能會遇到嚴重的性能問題。

您可以為彩色塊使用預制件並將其拖到不同級別的場景中。 如果您現在將行為腳本(管理光線投射到它時發生的情況)附加到您的預制件,則所有級別中的所有實例都將具有相同的邏輯。 這樣您就可以為不同級別重用相同的代碼庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM