C# 矩形是否需要绘制纹理?

C# 矩形是否需要绘制纹理?,c#,xna,monogame,C#,Xna,Monogame,我想画一些矩形来为我的玩家创建边界。当我去画它们时,我注意到它需要一种纹理。这让我很困惑,我想知道它是否需要一个纹理。实际上是的,你需要一个纹理来画一个矩形。 如果只是想在对象周围创建边界,这听起来很奇怪。解决方案是创建“1像素纹理”,并相应地绘制: public Texture2D pixel; protected override void Initialize() { ... pixel = new Texture2D(GraphicsDevice, 1, 1);

我想画一些矩形来为我的玩家创建边界。当我去画它们时,我注意到它需要一种纹理。这让我很困惑,我想知道它是否需要一个纹理。

实际上是的,你需要一个纹理来画一个矩形。 如果只是想在对象周围创建边界,这听起来很奇怪。解决方案是创建“1像素纹理”,并相应地绘制:

public Texture2D pixel;
protected override void Initialize()
{
    ...
    pixel = new Texture2D(GraphicsDevice, 1, 1);
    pixel.SetData(new[] { Color.White }); //
    ...
}

protected override void Draw(GameTime gameTime)
{
    ...
    int bw = 2; // Border width
    spriteBatch.Draw(pixel, new Rectangle(Player.PlayerPositionRectangle.Left, Player.PlayerPositionRectangle.Top, bw, Player.PlayerPositionRectangle.Height), Color.Black); // Left
    spriteBatch.Draw(pixel, new Rectangle(Player.PlayerPositionRectangle.Right, Player.PlayerPositionRectangle.Top, bw, Player.PlayerPositionRectangle.Height), Color.Black); // Right
    spriteBatch.Draw(pixel, new Rectangle(Player.PlayerPositionRectangle.Left, Player.PlayerPositionRectangle.Top, Player.PlayerPositionRectangle.Width, bw), Color.Black); // Top
    spriteBatch.Draw(pixel, new Rectangle(Player.PlayerPositionRectangle.Left, Player.PlayerPositionRectangle.Bottom, Player.PlayerPositionRectangle.Width, bw), Color.Black); // Bottom
    ...
}
注意: 在我的例子中,“PlayerPositionRectangle”就是玩家的位置,包括玩家纹理的大小。可以为交互范围、碰撞或任何需要的对象添加偏移:

Rectangle(X, Y, spriteWidth, spriteHeight);

请添加创建和绘制矩形的代码。如果有支持它的着色器,您可以只使用颜色绘制三角形。
public void draw(SpriteBatch SpriteBatch){SpriteBatch.draw(mapBounderyTexture,new Vector2(100100),Color.Black);}