Unity Engine的Vector3结构中可以存储哪些类型的数据?

What types of data can be stored in Unity Engine's Vector3 structure?

我对 Unity 2D 有点陌生,所以我不熟悉 Vector3 数据结构的限制,但我想知道我是否可以存储非坐标non-pixel/graphic相关数据像普通数组一样放入其中。

一些背景信息: 我正在制作一个 2D 地牢生成器并希望在主 Vector3 的每个索引中存储编码整数数组(使用两个嵌套的 for 循环),以便在生成地牢时可以通过每次迭代更改整数。然后最终布局将采用编码整数并擦除索引数据,而是在该位置实例化适当的地板或墙砖。

代码为:

等等

所以,32是区域2中类型房间的格子,当一条走廊连接到它时,它变成12。当两个区域从一条走廊连接时,它采用两个位置中较低的一个,依此类推上,直到整个网格填充 1(连接到主要区域),此时它们都转换为 0,表示地牢已准备好转换为纹理。 100 是一个在 x > 99 时触发的特殊标志,表示一个方块应该保持填充状态,否则会意外加入房间或扩展门口。

Vector3 只是 3 个标记为 x、y 和 z 的浮点数。尽管您可以 随心所欲地使用它,但我建议反对 为此目的使用它。您想要的是一个自定义结构,它包含的不是三个整数,而是一个枚举值、一个整数和一个布尔值。像这样:

/// <summary>
/// A simple struct to represent a tile on the map
/// </summary>
public struct MapTile {
    /// <summary>
    /// The type of the tile
    /// </summary>
    public TileType Type;

    /// <summary>
    /// The region this tile is in
    /// </summary>
    public int Region;

    /// <summary>
    /// True if this tile is protected
    /// </summary>
    public bool Protected;

    /// <summary>
    /// Constructs a new MapTile
    /// </summary>
    /// <param name="type">The type of the tile</param>
    /// <param name="region">The region the tile is in</param>
    /// <param name="isProtected">true if the tile is protected</param>
    public MapTile(TileType type, int region, bool isProtected) {
        this.Type = type;
        this.Region = region;
        this.Protected = isProtected;
    }
}

/// <summary>
/// An enum that contains the types of possible tiles
/// </summary>
public enum TileType {
    FILLED, CORRIDOR, DOOR, ROOM
}

在需要时制作自定义结构或 类 是一种很好的做法,而不是试图将您的数据硬塞进现有的结构中。
此外,尝试将整数值(-1、2、7,...)存储在浮点数(十进制)中通常不是一个好主意,因为浮点数容易出现舍入误差。您可能希望您的值为 25,但计算机可能会将其设为 25.00000000001 或 24.9999999999,因此难以处理。

避免 "magic numbers" 也是一种很好的做法,例如您计划如何将 "corridor" 存储为“010”。如果有人看到您的代码,他们怎么知道 010 表示走廊?如果您以后想更改它怎么办?幸运的是,C#(和大多数其他语言)有一个内置类型 "number that represents a thing",它被称为枚举。枚举类似于整数,但它们只有您赋予它们的特定值,并且有很好的标记。

希望对您有所帮助!