【发布时间】:2021-11-16 16:03:18
【问题描述】:
我正在尝试在 MonoGame 中加载图片(没有 Content.Load),但是当我尝试启动项目时遇到 System.IO.FileNotFoundException,我希望有人能告诉我我做错了什么。
错误消息: System.IO.FileNotFoundException:'找不到文件'G:\Min enhet\C#\FirstMonoGameProject\bin\Debug\netcoreapp3.1\Content\Graphic\CoolBox'。 '
我得到错误的代码
public Texture2D LoadTexture()
{
string fullPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
+ "/Content/Graphic/" + "CoolBox";
using (FileStream fileStream = File.Open(fullPath, FileMode.Open))
{
Texture2D myTexture2D = Texture2D.FromStream(EntityManager.graphicsDevice, fileStream);
return myTexture2D;
}
}
我调用该方法的附加代码(均来自 game1)
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
coolBox.LoadTexture();
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(coolBox.LoadTexture(), new Rectangle(0, 0, 280, 210), Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
【问题讨论】:
-
显然路径和/或文件的名称是错误的。文件的扩展名不是 .gif、.png、.jpg 吗?
-
只需检查您的 Windows 机器上的文件路径是否存在。这里还有一个专业提示:不要在文件路径中使用特殊字符。系统允许这样做,但这可能会导致麻烦。将“C#”重命名为“CSharp”或任何你喜欢的名称。
-
请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。
-
在 Draw 中调用
CoolBox.LoadTexture()将在每次调用 draw 时从磁盘重新加载纹理。这将增加绘图方法的阻塞时间。从方法返回引用不是很好,但可以,每次调用都从磁盘加载不是。不可能在 gpu 上缓存纹理。