【发布时间】:2016-06-21 01:56:15
【问题描述】:
所以我有一个简单的项目,我用多个方块制作了一个游戏板,它有一个可以打开或关闭的网格。
不幸的是,它不能那样工作。这是我的董事会经理代码:
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() {
}
}
}
我的游戏编程经验来自 Pygame。在 Pygame 中,背景必须不断地在自身上重新生成,所以如果我想让网格消失,我需要做的就是每帧停止一次 blitting,然后一帧之后它就消失了。在统一中,由于对象可以移动而无需在代码中重新生成背景,因此网格仅在一次实例化后就保持不变。我只是想知道一种方法,当 GridToggle.isOn 为真时,网格被实例化并将一直保留在那里,直到 GridToggle.isOn 为假,当网格将不再存在时,直到它再次打开。谢谢!
【问题讨论】:
标签: c# unity3d toggle instantiation