【问题标题】:XNA ContentLoadException when loading textures in the main project using a load command in a DLL使用 DLL 中的加载命令在主项目中加载纹理时出现 XNA ContentLoadException
【发布时间】:2012-06-08 20:26:11
【问题描述】:

我刚刚开始尝试编写一个非常基本的游戏引擎,首先在 XNA 的 2d 世界中显示简单的图块。这是我第一次编写 DLL,一切都很顺利,直到我尝试为 ContentManager 编写 LoadAll 函数,这导致了我现在遇到的问题。我收到了 ContentLoadException,即使文件明显存在,否则程序将无法进入内容加载阶段。

XNB 文件肯定存在,并且 Content.RootDirectory 已设置。我已经解决了这样的每一个问题,我可以在这里和其他网站上找到,但找不到任何解决方案。如果有人能提供帮助,我将永远感激不尽。

    public static Dictionary<String, T> LoadAll<T>(this ContentManager Content, string directory)
    {
        DirectoryInfo dir = new DirectoryInfo(Content.RootDirectory + "/" + directory);
        if (!dir.Exists)
        {
            throw new DirectoryNotFoundException();
        }
        Dictionary<String, T> result = new Dictionary<string, T>();
        FileInfo[] files = dir.GetFiles();
        foreach (FileInfo file in files)
        {
            string key = Path.GetFileNameWithoutExtension(file.Name);
            result[key] = Content.Load<T>(dir.ToString() + "/" + key);
        }
        return result;
    }

显然我在 Content.Load 方法中遇到了错误,我得到了

"Error loading "Content\Tiles\bricktile". File not found."

我找到了这段代码here,但即使将其重写为列表形式,我也会遇到同样的错误。奇怪的部分是错误加载“文件路径”显示了绝对存在的 xnb 文件的正确路径。如果有人想要我的 DLL 的源代码,我可以轻松上传。

谢谢!

编辑:真的应该说我怎么称呼它:

textures = Content.LoadAll<Texture2D>(tilesFolder);

【问题讨论】:

    标签: c# xna xna-4.0


    【解决方案1】:

    您将错误的路径传递给 Content.Load() 方法。 dir.ToString() 将返回完整路径(包括内容的根目录)。但是你需要没有根目录的路径:

    result[key] = Content.Load<T>(directory + "/" + key); 
    

    【讨论】:

    • 谢谢,多年来一直在研究这个问题。菜鸟失误!
    猜你喜欢
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多