그래서 여러 개의 사각형으로 게임 보드를 만들고, 켜고 끌 수있는 그리드가있는 간단한 프로젝트가 있습니다.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 일 때 그리드가 더 이상 켜지지 않을 때까지 머물러있게됩니다. 고맙습니다!
나는 이것을 시도 할 것이다. 고맙습니다! – Demandooda
차가움. 하지만 다른 스크립트에서 렌더러를 원격으로 비활성화하려면 어떻게해야합니까? 그리드는 별도의 스크립트로 관리되며 프리 패브입니다. – Demandooda
Toggle 대신 다른 스크립트로 제어하려고합니까? BoardHandle이있는 GameObject에 대한 참조가 있으면 다른 스크립트에서 간단하게 비활성화 할 수 있습니다. – Lof