什么时候加载纹理?
我目前正在开发几个 Windows Phone 项目(尽管这个问题也可能适合 iphone/android),这让我思考,什么时候是从内容管理器加载纹理的最佳时间。
首先,我从我的 Game 基类加载它们,并根据需要传递它们。我很快就厌倦了这种情况,因此创建了一个小型资源管理器类,并将其传递给任何需要它的人。
所以我在想,也许最好的办法是,当一个类需要它时,我实际加载纹理,然后将其分配给一个变量,这样当我再次需要它时 - 它将准备就绪......是这是处理加载资源的最佳方式(高效?最快?)?如果没有,你会建议我如何去做?
I'm currently working on a couple of windows phone projects (Although the question may also fit iphone/android) and it got me thinking, when is the best time to load textures from the content manager.
At first, I was loading them all up, from my Game base class, and passing them along as required. Quickly getting sick of this, I have created a small resource manager class, which I pass out to anything that requires it.
So I'm thinking, that perhaps its best that I actually load the texture in, when a class requires it, and then assign it to a variable, so when I need it again - it will be all ready to go... is this the best way (efficient?, fastest?) to handle loading resources? If not, how would you recommend I go about it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要创建任何类型的“资源管理器”类。只需传递 XNA
ContentManager
类(从Game.Content
获取的实例)。默认内容管理器将自动为您处理重用加载的对象。因此,您可以从多个位置
Content.Load("something")
并且始终会返回相同纹理实例。因此,如果您的游戏对象有一堆类,标准设计是为每个对象提供一个
Update
和一个Draw
方法,您可以从中的相应方法调用它们>Game
- 只需向这些类添加另一个方法:LoadContent
,该方法接受ContentManager
参数。您可以从游戏的
LoadContent
方法中调用该方法。如果稍后您想要实现某种延迟加载系统(例如:更改关卡时加载内容),您还可以从游戏的
中调用游戏类的
方法(但请记住,加载内容很慢 - 因此您可能想要弹出“正在加载”屏幕)。LoadContent
方法>Update卸载内容稍微棘手一些。任何你自己创建的东西都必须卸载。但是从
ContentManager
加载的任何内容(因为实例是共享的)只能由该内容管理器卸载(Game
将处理卸载其Content
成员当需要时)。您可能会发现这篇博文值得阅读。Don't create any kind of "resource manager" class. Just pass the XNA
ContentManager
class around (the instance you get fromGame.Content
).The default content manager will automatically handle reusing loaded objects for you. So you can
Content.Load<Texture2D>("something")
from multiple places and you will always get back the same texture instance.So if you have a bunch of classes for your game objects, with the standard design of giving each an
Update
and aDraw
method that you call from the respective methods inGame
- just add another method:LoadContent
to those classes, that accepts an argument ofContentManager
.You can call that method from your game's
LoadContent
method.If, later, you want to implement some kind of system of delay-loading things (for example: loading content when changing levels), you can also call your game classes'
LoadContent
method from your game'sUpdate
method (but keep in mind that loading content is slow - so you might want to throw up a "loading" screen).Unloading content is slightly trickier. Anything you create yourself you must unload. But anything loaded from a
ContentManager
(because the instances are shared) should only be unloaded by that content manager (Game
will handle unloading itsContent
member when it needs to). You may find this blog post worth reading.