2016-05-31 174 views
-2

我製作了一個統一的遊戲,其中有不同級別的多個場景,用戶必須選擇一個彩色塊才能進入下一個級別,但是我希望我的代碼在單個場景中有一種方法可以將我的多重場景遊戲合併成一個場景。 代碼級別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); 
         } 
        } 
       } 

代碼級別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; 
      } 
     } 
    } 

回答

0

要開始我不知道你爲什麼要這樣做。從技術上講,這是有場景的全部要點。但是你仍然可以做的是把所有東西(第1級和第2級)放在一個場景中,然後定位並禁用相機外部屬於第2級的任何東西。然後,我不會調用Application.LoadLevel,而是調用一個函數,將屬於第1級的所有內容移出屏幕,同時將第2級移動到屏幕中。如果我在哪裏,我會將一個層次的所有對象作爲另一個GameObject(也許稱爲LevelX)的子對象,那麼一次移動所有內容都會更容易。這甚至會給你很好的幻燈片級別之間的轉換。如果你不想過渡只是立即改變立場。

要小心,因爲根據您可能有嚴重的性能問題的級別的數量。

+0

遊戲涉及一個簡單的不同顏色對象的網格。遊戲中的星星呈現一種顏色,然後屏幕上會出現不同顏色的物體,玩家必須爲該輪選擇正確的顏色塊。遊戲變得複雜不同級別的場景,但我想在一個遊戲屏幕上完整的遊戲。 –

+0

然後,我會做的是一旦玩家觸及正確顏色的所有塊,然後我將清除網格中的所有塊,然後再次產生初始顏色塊並繼續重複該過程。 – Agustin0987