XNA:将Content.Load与Texture2D的子类一起使用

发布于 2024-12-14 08:04:14 字数 719 浏览 2 评论 0原文

所以我有一个名为ScrollingBackgroundTexture 的Texture2D 子类。我想用它来加载带有 Content.Load<> 的纹理,但我似乎无法让它工作。

这是我的子类中的代码(到目前为止,它只是一个构造函数):

class ScrollingBackgroundTexture : Texture2D {
    public ScrollingBackgroundTexture(GraphicsDevice graphicsDevice, int width, int height) : base(graphicsDevice, width, height) { }
}

这是我的主类中给我带来麻烦的代码:

test = Content.Load<ScrollingBackgroundTexture>("near stars");

错误告诉我该文件包含 Texture2D,但我我正在尝试将其加载为 ScrollingBackgroundTexture

我也尝试过

test = (ScrollingBackgroundTexture)Content.Load<Texture2D>("near stars");

但这只会给我另一个错误。

So I have a subclass of Texture2D called ScrollingBackgroundTexture. I'd like to use it to load a texture with Content.Load<>, but I can't seem to get it to work.

Here's the code in my subclass (as of now, it's just a constructor):

class ScrollingBackgroundTexture : Texture2D {
    public ScrollingBackgroundTexture(GraphicsDevice graphicsDevice, int width, int height) : base(graphicsDevice, width, height) { }
}

And here's the code from my main class that's giving me trouble:

test = Content.Load<ScrollingBackgroundTexture>("near stars");

The error tells me that the file contains a Texture2D, but I'm trying to load it as a ScrollingBackgroundTexture.

I've also tried

test = (ScrollingBackgroundTexture)Content.Load<Texture2D>("near stars");

But that just gives me another error.

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

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

发布评论

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

评论(1

我的鱼塘能养鲲 2024-12-21 08:04:14

我认为你做不到。原因如下。 Texture2D 的大小为 X 字节大。 ScrollingBackgroundTexture 的大小为 X + Y 大。 ScrollingBackgroundTexture 不知道如何加载 X 大的内容,因为它是 X + Y 大。

您需要做的是为 ScrollingBackgroundTexture 创建您自己的自定义内容类型。您可以在 MSDN 中找到有关如何执行此操作的信息。

如果我是你,我会改用设计更好的东西,有利于组合而不是继承 !这还有一个额外的好处,那就是不强迫你搞乱内容管道。

I don't think you can do that. Here's why. The size of a Texture2D is X bytes big. The size of a ScrollingBackgroundTexture is X + Y big. The ScrollingBackgroundTexture doesn't know how to load content that is X big becasue it is X + Y big.

What you'll need to do is create your own custom content type for ScrollingBackgroundTexture. You can find information on how to do that at MSDN.

If I were you though, I would switch to something with a bit better design that favors composition over inheritance! This will have the added bonus of not forcing you to mess around with the content pipeline.

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