2016-06-20 1 views
0

그래서 여러 개의 사각형으로 게임 보드를 만들고, 켜고 끌 수있는 그리드가있는 간단한 프로젝트가 있습니다.Unity가 인스턴스화 된 객체 토글을 표시하거나 사라지게 만듭니다.

불행히도, 그런 식으로 작동하지 않습니다. 여기 보드 관리자 코드가 있습니다 :

using UnityEngine; 
using UnityEngine.UI; 
using System.Collections; 
using System.Collections.Generic; 

namespace BoardHandle { 
    [RequireComponent(typeof(SpriteRenderer))] 
    public class BoardManager : MonoBehaviour { 

     //2d list that represents the board 
     List<List<GameObject>> board = new List<List<GameObject>>(); 

     //Gamebjects import 
     public GameObject GrassTile; 
     public GameObject Grid; 

     //UI buttons import 
     public Toggle GridToggle; 

     //Miscellanious variables that could be edited in inspector 
     public int rows; 
     public int cols; 
     public float tileWidth; 
     public float tileHeight; 


     void Start() { 

      GrassTile.transform.localScale = new Vector3 (tileWidth, tileHeight, 0f); 
      Grid.transform.localScale = new Vector3 (tileWidth, tileHeight, 0f); 

      //Adds all of the stuff to the game board 
      for (int x = 0; x < cols; x++) { 
       board.Add (new List<GameObject>()); 
       for (int y = 0; y < rows; y++) { 
        board [x].Add (GrassTile); 
       } 
      } 


      //Makes board bits all go to the screen 
      for (int x = 0; x < board.Count; x++) { 
       for (int y = 0; y < board[x].Count; y++) { 

        //Makes the grid. And it stays there currently until the game ends. 
        Instantiate (Grid, 
           new Vector3 (x * tileWidth, y * tileHeight, 0), 
           Quaternion.identity); 

        //This is the actual board, filled with grass tiles. I wnat to keep this. 
        Instantiate (board [x] [y], 
           new Vector3 (x * tileWidth, y * tileHeight, 0), 
           Quaternion.identity); 
       } 
      } 
     } 


     void Update() { 

     } 


    } 
} 

내 게임 프로그래밍 경험은 파이 게임입니다. 파이 게임에서는 배경이 끊임없이 재생성되어야하므로 그리드를 사라지게하려면 프레임 당 한 번 블리치를 멈추고 나중에 프레임이 사라지면됩니다. 화일에서 배경을 재생성하지 않고도 객체가 움직일 수 있기 때문에 그리드는 단지 하나의 인스턴스 생성 후에 그대로 유지됩니다. GridToggle.isOn이 참일 때 그리드가 인스턴스화되고 GridToggle.isOn이 false가 될 때까지 머물러있는 방식을 알고 싶습니다. GridToggle.isOn이 false 일 때 그리드가 더 이상 켜지지 않을 때까지 머물러있게됩니다. 고맙습니다!

답변

1

Unity 게임 오브젝트를 비활성화하고 구성 요소를 비활성화 할 수 있습니다.

GameObject가 비활성화 된 경우 "해당 없음"으로 간주됩니다. 렌더링을 비활성화하려는 경우 타일의 렌더러 구성 요소를 비활성화 할 수도 있습니다. 첫 번째 옵션과 같은 소리가 원하는 것입니다.

GameObject를 비활성화하면 하위 GameObject도 비활성화됩니다. 보드 및 해당 내용을 사용하지 않으려면 인스턴스화 된 개체를 보드의 자식으로 사용하는 것이 좋습니다. 당신은 게임 오브젝트의 변환을 통해이 작업을 수행 할 수 있습니다

instantiatedGameObject.transform.parent = this.transform; 

는 토글을 사용하고 있기 때문에, 당신은에 이벤트 리스너를 추가 할 수 있습니다

GridToggle.onValueChanged.AddListener((value) => this.SetActive(value)); 
+0

나는 이것을 시도 할 것이다. 고맙습니다! – Demandooda

+0

차가움. 하지만 다른 스크립트에서 렌더러를 원격으로 비활성화하려면 어떻게해야합니까? 그리드는 별도의 스크립트로 관리되며 프리 패브입니다. – Demandooda

+0

Toggle 대신 다른 스크립트로 제어하려고합니까? BoardHandle이있는 GameObject에 대한 참조가 있으면 다른 스크립트에서 간단하게 비활성화 할 수 있습니다. – Lof