【问题标题】:Spawn multiple cubes with Array data使用数组数据生成多个立方体
【发布时间】:2021-09-10 16:13:49
【问题描述】:

我需要帮助解决我的这个问题。假设我想生成一个魔方 (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 的立方体,为什么我需要这种数组。我需要这样的数据,所以我只能生成立方体的外层,而不是里面的那些。保存性能问题。

【问题讨论】:

    标签: c# arrays unity3d instantiation


    【解决方案1】:

    这听起来很简单:

    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];
    }
    

    【讨论】:

    • 这就是答案。我是团结和学习的新手,这是一个很大的帮助!谢谢大佬
    猜你喜欢
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 2022-07-18
    相关资源
    最近更新 更多