【发布时间】:2015-10-22 22:57:28
【问题描述】:
我一直在使用这个脚本来画线,它可以工作,但是当我运行我的游戏时,它开始像没有人的事一样消耗我的资源。
public static void DrawLine(Vector2 start, Vector2 end, Color color, int width, float alpha, float depth)
{
// GraphicsDevice stored in other class
var texture = new Texture2D(Graphics2D.GraphicsDevice, 1, 1);
texture.SetData(new[] { color });
Vector2 edge = end - start;
float angle = (float)Math.Atan2(edge.Y, edge.X);
// SpriteBatch stored in other class
Graphics2D.SpriteBatch.Draw(
texture,
new Rectangle(
(int)start.X,
(int)start.Y,
(int)edge.Length(),
width),
null,
Color.White,
angle,
new Vector2(0, 0),
SpriteEffects.None,
1.0f);
}
一旦开始绘制线条,内存和 CPU 使用率就会开始飙升。
对于它为什么这样做有任何猜测吗?我是否应该过度担心?或者这对于这些类型的操作是否正常。
【问题讨论】:
-
我将只存储一个白色像素的
Texture2D,然后在Draw调用中指定颜色。 -
太棒了,它有效。非常感谢。
标签: c# resources draw xna-4.0 monogame