XNA 在屏幕上绘制文本

发布于 2024-10-12 01:01:42 字数 66 浏览 2 评论 0原文

有没有办法使用 SpriteBatch.DrawString 在屏幕上绘制长文本?我的意思是当到达屏幕末尾时插入新行。

Is there a way to draw a long text on the screen using SpriteBatch.DrawString? I mean inserts new lines when comes to the end of the screen.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿! 2024-10-19 01:01:43

我建议查看XNAwiki - TextRendering

I'd suggest looking at XNAwiki - TextRendering.

安静被遗忘 2024-10-19 01:01:43

插入新行是您必须自己计算的事情,这不是您使用 XNA 框架自动执行的操作,但是可以编写代码来执行此操作。

一种方法是取出想要写入的字符串,一次移动一个单词并测量它,直到获得足够的单词来填充您想要填充的任何区域的宽度。找到该宽度后,您可以更改要绘制的字符串的 Y 位置并移动到下一行,或者在该点插入一个新行字符到字符串中,然后开始计算应该绘制的单词数。在下一行。

需要注意的一点是,字符串操作的成本很高,并且会产生大量垃圾,因此您应该尝试尽量减少执行此类操作的次数。如果文本是静态的并且永远不会改变,那么最好在游戏运行时执行此操作并且不再执行此操作。

Inserting new lines is something you'd have to calculate yourself it's not something you an do automatically with the XNA framework, but yes it's possible to write code to do that.

One way to do it would be to take the string want to write and to move through it a word at a time an measure it until you get enough words to fill the width of whatever area it is you want to fill. Once you've found that width, you'd either change the Y Position of the string you want to draw and move onto the next line or insert a new line character into the string at that point and start calculating the amount of words that should be on the next line.

A thing to pay attention to is that string manipulation is expensive and generates a lot of garbage so you should try and minimize the amount of times you do something like that. If the text is static and never changes it would be ideal to do this one and never again while the game is running.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文