使用数组数据生成多个立方体
Spawn multiple cubes with Array data
我需要帮助解决我的这个问题。假设我想生成一个 Rubik 立方体 (3 3 3)。我希望生成以这种方式发生,我们将 4 个数据传递给游戏/每个立方体都有 4 个数据,如 x、y、z、t,其中 x、y、z 是位置坐标,t 是立方体的类型。例如,假设 (0,0,0,r)。我需要一个红色的立方体来生成,其中 r 是红色的,以便统一生成在 0,0,0 处。另一个例子是 (0,2,0,b),我需要一个蓝色立方体在 y 轴 2 处生成。希望你们明白了。所以,有了这样的数据,我想生成一个 333 的立方体。
如果我想生成一个 10 * 10 * 10 的立方体,我为什么需要这种数组。我需要这样的数据,所以我只能生成立方体的外层,而不是里面的那些。保存性能问题。
这听起来很简单:
public enum CubeType
{
white,
yellow,
blue,
red,
green,
orange
}
private readonly IReadOnlyDictionary<CubeType, Color> colors = new Dictionary<CubeType, Color>
{
{CubeType.white, Color.white},
{CubeType.yellow, Color.yellow},
{CubeType.blue, Color.blue},
{CubeType.red, Color.red},
{CubeType.green, Color.green},
{CubeType.orange, Color.orange},
}
// Size/Expands of one cube tile
public float cubeSize = 1f;
public GameObject prefab;
// Amount of layers/dimensions e.g. 3x3
public int dimensions = 3;
public void SpawnCube(int x, int y, int z, CubeType type)
{
var cube = Instantiate (prefab, new (Vector3(x,y,z) - Vector3.one * dimensions / 2f) * cubeSize, Quaternion.identity);
// Or whatever you want to do with the type
cube.GetComponent<Renderer>().material.color = colors[type];
}
我需要帮助解决我的这个问题。假设我想生成一个 Rubik 立方体 (3 3 3)。我希望生成以这种方式发生,我们将 4 个数据传递给游戏/每个立方体都有 4 个数据,如 x、y、z、t,其中 x、y、z 是位置坐标,t 是立方体的类型。例如,假设 (0,0,0,r)。我需要一个红色的立方体来生成,其中 r 是红色的,以便统一生成在 0,0,0 处。另一个例子是 (0,2,0,b),我需要一个蓝色立方体在 y 轴 2 处生成。希望你们明白了。所以,有了这样的数据,我想生成一个 333 的立方体。
如果我想生成一个 10 * 10 * 10 的立方体,我为什么需要这种数组。我需要这样的数据,所以我只能生成立方体的外层,而不是里面的那些。保存性能问题。
这听起来很简单:
public enum CubeType
{
white,
yellow,
blue,
red,
green,
orange
}
private readonly IReadOnlyDictionary<CubeType, Color> colors = new Dictionary<CubeType, Color>
{
{CubeType.white, Color.white},
{CubeType.yellow, Color.yellow},
{CubeType.blue, Color.blue},
{CubeType.red, Color.red},
{CubeType.green, Color.green},
{CubeType.orange, Color.orange},
}
// Size/Expands of one cube tile
public float cubeSize = 1f;
public GameObject prefab;
// Amount of layers/dimensions e.g. 3x3
public int dimensions = 3;
public void SpawnCube(int x, int y, int z, CubeType type)
{
var cube = Instantiate (prefab, new (Vector3(x,y,z) - Vector3.one * dimensions / 2f) * cubeSize, Quaternion.identity);
// Or whatever you want to do with the type
cube.GetComponent<Renderer>().material.color = colors[type];
}