为什么在`LoadContent`方法中更改图形设置失败? [英] Why does changing the graphics setting after doing so in the `LoadContent` method fail?
问题描述
如果我在 Initialize
方法中设置图形设置,然后在 Update
方法中设置图形设置,如下所示:
protected override void Initialize()
{
graphics.ApplyChanges();
base.Initialize();
protected override void Update(GameTime gameTime)
{
graphics.ApplyChanges();
base.Update(gameTime);
}
一切都好。
然而,当我将代码移动到我的
LoadContent
方法中时,如下所示: protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
graphics.ApplyChanges();
protected override void Update(GameTime gameTime)
{
graphics.ApplyChanges();
base.Update(gameTime);
}
我得到一个 InvalidOperationException
$ b
在调用EndScreenDeviceChange之前必须调用BeginScreenDeviceChange
这对我来说没什么意义,因为我在两个方面都做了同样的事情。我的理解是,在 Initialize
方法之后简单调用 LoadContent
方法。那些调用 GraphicsDeviceManager
?
你搞乱了XNA的一些内部管道。通常,当图形设备被创建/销毁并正确配对时,XNA应该在 GameWindow
上挂接事件以调用这些方法。但是,因为你正在改变图形设备,所以你不应该这样做,否则你会以某种方式导致它失败。
要回答关于您的特定情况的问题:发生了什么事是 LoadContent
在设置之后那些事件。您对 ApplyChanges
的呼叫已从事件挂钩之前移至之后。
不重要,因为 版本的代码都不正确。它似乎在第一个版本中工作,只是祝你好运。 请参阅此答案 ,其中介绍了如何设置和更改图形设备正确。
If I set the graphics settings in the Initialize
method and then in the Update
method, like so:
protected override void Initialize()
{
graphics.ApplyChanges();
base.Initialize();
}
protected override void Update(GameTime gameTime)
{
graphics.ApplyChanges();
base.Update(gameTime);
}
Everything is fine.
However, when I move the code to my LoadContent
method like so:
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
graphics.ApplyChanges();
}
protected override void Update(GameTime gameTime)
{
graphics.ApplyChanges();
base.Update(gameTime);
}
I get an InvalidOperationException
:
Must call BeginScreenDeviceChange before calling EndScreenDeviceChange
This doesn't make much sense to me, since I'm doing the same thing in both. It was my understanding that the LoadContent
method was simply called after the Initialize
method. What is happening between those calls that messes up the GraphicsDeviceManager
?
You're messing up some of XNA's internal plumbing. Normally XNA should hook up events to call those methods on GameWindow
when the graphics device is created/destroyed, with correct pairing. But, because you're changing the graphics device somewhere you shouldn't be, you've somehow caused it to fail.
To answer the question about your specific case: What is happening is that base.Initialize()
calls LoadContent
after it has set up those events. Your call to ApplyChanges
has moved from before the events get hooked up, to after.
Not that it matters, because neither version of your code is correct. That it seems to work in the first version is just good luck. Please see this answer, which explains how to set up and change the graphics device correctly.
这篇关于为什么在`LoadContent`方法中更改图形设置失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!