C# XNA DrawString()仅绘制部分字符串

C# XNA DrawString()仅绘制部分字符串,c#,xna,C#,Xna,我对XNA中的DrawString()有问题。我对多个逻辑层使用多个spritebatch。例如:背景、对象、菜单等 在我的菜单批处理中,我绘制了一个菜单(背景中的大灰色框)、按钮(菜单上较小的灰色框)和按钮的字符串 问题是: 但由于某些原因,这些线并没有完全画出来。有人知道为什么吗 编辑: _menuLayer.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend); if (_menu != null)

我对
XNA
中的
DrawString()
有问题。我对多个逻辑层使用多个
spritebatch
。例如:背景、对象、菜单等

在我的菜单批处理中,我绘制了一个菜单(背景中的大灰色框)、按钮(菜单上较小的灰色框)和按钮的字符串

问题是:

但由于某些原因,这些线并没有完全画出来。有人知道为什么吗

编辑:

_menuLayer.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
        if (_menu != null)
        {
            _menuLayer.Draw(_menuBoard, new Vector2(graphics.PreferredBackBufferWidth / 2 - 160, graphics.PreferredBackBufferHeight / 2 - 240), Color.White);
        }
        _menuLayer.End();
        _buttonLayer.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
        if (_menu != null)
        {
            foreach (Button button in _menu.Buttons)
            {
                if (button.Pressed)
                {
                    _buttonLayer.Draw(_menuButtonPressed, button.Location, Color.White);
                    _buttonLayer.DrawString(_text, button.Text, button.GiveStringLocation(_text), Color.Black);
                }
                else
                {
                    _buttonLayer.Draw(_menuButton, button.Location, Color.White);
                    _buttonLayer.DrawString(_text, button.Text, button.GiveStringLocation(_text), Color.Black);
                }
            }
        }
        _buttonLayer.End();

<?xml version="1.0" encoding="utf-8"?>
<!--
This file contains an xml description of a font, and will be read by the XNA
Framework Content Pipeline. Follow the comments to customize the appearance
of the font in your game, and to change the characters which are available to draw
with.
-->
<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
  <Asset Type="Graphics:FontDescription">

    <!--
    Modify this string to change the font that will be imported.
    -->
    <FontName>Arial</FontName>

    <!--
    Size is a float value, measured in points. Modify this value to change
    the size of the font.
    -->
    <Size>20</Size>

    <!--
    Spacing is a float value, measured in pixels. Modify this value to change
    the amount of spacing in between characters.
    -->
    <Spacing>0</Spacing>

    <!--
    UseKerning controls the layout of the font. If this value is true, kerning information
    will be used when placing characters.
    -->
    <UseKerning>true</UseKerning>

    <!--
    Style controls the style of the font. Valid entries are "Regular", "Bold", "Italic",
    and "Bold, Italic", and are case sensitive.
    -->
    <Style>Bold</Style>

    <!--
    If you uncomment this line, the default character will be substituted if you draw
    or measure text that contains characters which were not included in the font.
    -->
    <!-- <DefaultCharacter>*</DefaultCharacter> -->

    <!--
    CharacterRegions control what letters are available in the font. Every
    character from Start to End will be built and made available for drawing. The
    default range is from 32, (ASCII space), to 126, ('~'), covering the basic Latin
    character set. The characters are ordered according to the Unicode standard.
    See the documentation for more information.
    -->
    <CharacterRegions>
      <CharacterRegion>
        <Start>&#32;</Start>
        <End>&#126;</End>
      </CharacterRegion>
    </CharacterRegions>
  </Asset>
</XnaContent>
\u menuLayer.Begin(SpriteSortMode.BackToFront,BlendState.AlphaBlend);
如果(_menu!=null)
{
_menuLayer.Draw(_菜单板,新矢量2(graphics.PreferredBackBufferWidth/2-160,graphics.PreferredBackBufferHeight/2-240),颜色为白色);
}
_menuLayer.End();
_buttonLayer.Begin(SpriteSortMode.BackToFront、BlendState.AlphaBlend);
如果(_menu!=null)
{
foreach(菜单中的按钮。按钮)
{
如果(按下按钮)
{
_buttonLayer.Draw(按下菜单按钮,按钮位置,颜色,白色);
_buttonLayer.DrawString(_text,button.text,button.GiveStringLocation(_text),Color.Black);
}
其他的
{
_buttonLayer.Draw(菜单按钮、按钮位置、颜色、白色);
_buttonLayer.DrawString(_text,button.text,button.GiveStringLocation(_text),Color.Black);
}
}
}
_buttonLayer.End();
Arial
20
0
真的
大胆的
 
~

您使用的字体可能有问题。确保您使用的字体是“.ttf”,然后尝试几种不同的字体。我强烈建议您使用Microsoft推荐的字体之一。链接:

看起来您的字体只允许您使用某些字符

<CharacterRegions>
  <CharacterRegion>
    <Start>&#32;</Start>
    <End>&#126;</End>
  </CharacterRegion>
</CharacterRegions>

 
~
确保您的字符在开始和结束标记的ASCII值内,或者更改开始和结束标记以包含所需的字符


尝试使用SpriteSortMode.Immediate。否则,我认为问题可能在于你做事的方式。将渲染留给UI之外的类似乎很混乱。你应该能够告诉一个按钮画,它应该画自己。或者实际上只是告诉你的用户界面去绘制,它会绘制所有的控件,从而绘制它们的子控件,等等

这样,您就可以在层次结构中构建UI:)

稍微构造一下你的UI;有一个像UIControl之类的基类

List<UIControl> Children { get; protected set; }


Update(MyInputSystem input, float dt)
{
    UpdateChildren(input, dt);
}

UpdateChildren(MyInputSystem input, float dt)
{
    foreach(var child in Children)
        child.Update(input, dt);
}

Draw(SpriteBatch spriteBatch)
{
    DrawChildren(spriteBatch);
}

DrawChildren(SpriteBatch spriteBatch)
{
    foreach(var child in Children)
        child.Draw(spriteBatch);
}
然后您就有了一个标签类:

class Label: Panel{

SpriteFont Font { get; set; }
String Text { get; set; }
Vector2 TextOffset { get; set; }
Color TextColor { get; set; }

override Draw(SpriteBatch spriteBatch)
{
    spriteBatch.Draw(Texture, Position, Color.White);
    spriteBatch.DrawString(Font, Text, Position + TextOffset,Text Color);
    DrawChildren(spriteBatch);
}

}
和按钮类:

class Button: Label { //Inherit label, so we dont need to create som much new code

public event EventHandler<EventArgs> Click;

Rectangle _Bounds
public Rectangle Bounds
{
    get
    {
        if (_Rectangle.Width == 0)
            _Rectangle = new Rectangle((int)Position.X,(int)Position.Y, Texture.Width, Texture.Height);

        return _Texture;
    }
}

override Update(MyInputSystem input, flaot dt)
{
    if (input.MouseClicked && Bounds.Contains(input.MousePosition))
        Click(this, new EventArgs());
}

}
class按钮:Label{//Inherit Label,所以我们不需要创建太多新代码
公共事件事件处理程序单击;
矩形_界
公共矩形边界
{
得到
{
如果(_Rectangle.Width==0)
_矩形=新矩形((int)Position.X,(int)Position.Y,Texture.Width,Texture.Height);
返回纹理;
}
}
覆盖更新(MyInputSystem输入,flaot dt)
{
if(input.MouseClicked&&Bounds.Contains(input.MousePosition))
单击(此,新事件参数());
}
}
你可能已经注意到我没有任何SpriteBatch。在这里开始/结束?这是因为:

class UI
{
List<UIControl> Controls;

Update(MyInputSystem input, flioat dt)
{
    foreach(var control in Controls)
       control.Update(input, dt);
}

Draw(SpriteBatch spriteBatch)
{
    spriteBatch.Begin(/*Parameters*/);
    foreach(var control in Controls)
       control.Draw(spriteBatch);
    spriteBatch.End();
}
}
类用户界面
{
列表控件;
更新(MyInputSystem输入,flioat dt)
{
foreach(控件中的var控件)
控制。更新(输入,dt);
}
绘制(SpriteBatch SpriteBatch)
{
spriteBatch.Begin(/*参数*/);
foreach(控件中的var控件)
控件。绘制(spriteBatch);
spriteBatch.End();
}
}

明白我的意思了吗?

而用于呈现菜单的代码?剪裁或模糊是最可能的原因,可能两者都有。除非你正在做一些奇怪的事情,否则隐形墨水(钢笔颜色=背景色)是不可能的。不过从这里没办法说。嘿,看起来你在使用Unicode字符(想想
é
Á
)。默认情况下,Spritefonts不包括这些。你能发布你的spritefont吗?我已经添加了代码。我希望,这能让你更好地理解我的问题……:)我指的是spritefont本身的实际内容。对不起,我使用的字母来自普通拉丁字母表。它们介于两个最大值之间。这似乎很有趣但很奇怪。。。我来测试一下。看起来很棒!我回家后会试试的。:)哇!SpriteSortMode。立即运行非常好!但如果你同意的话,我想我也会尝试使用你的UI结构没问题,如果你需要一些灵感;
class UI
{
List<UIControl> Controls;

Update(MyInputSystem input, flioat dt)
{
    foreach(var control in Controls)
       control.Update(input, dt);
}

Draw(SpriteBatch spriteBatch)
{
    spriteBatch.Begin(/*Parameters*/);
    foreach(var control in Controls)
       control.Draw(spriteBatch);
    spriteBatch.End();
}
}