2016-05-31 135 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