【问题标题】:How Do I Load A XNA Model From A File Path Instead Of From The Content (eg. Model.FromStream but it doesnt exist)如何从文件路径而不是从内容加载 XNA 模型(例如 Model.FromStream 但它不存在)
【发布时间】:2012-01-01 18:27:34
【问题描述】:

如何从文件路径而不是从内容加载 XNA 模型(例如,Model.FromStream,但它不存在)? Texture2D.FromStream,我怎样才能为Model 做同样的事情?

【问题讨论】:

  • 您说的是已经处理成 .xnb 格式的模型,还是说 .x 或 .fbx(或其他)模型?因为除非它是 xnb 格式,否则 content.Load 将不起作用并且它不能成为“模型”。另一方面,如果它是 xnb 格式,则将内容管理器定向到其位置并正常加载是最简单的。
  • 是否有您不想使用 content.Load 的具体原因?这就是它的工作原理。
  • @James 你让我失望了:P

标签: c# model xna load


【解决方案1】:

你可以使用Content.Load<Model>("Path here, make sure you use drive letter");

如果你真的需要在 Model 上使用 FromStream 方法,你可以使用扩展方法(不完全是 FromStream 的完美副本,但它应该可以工作):

public static Model FromPath(this Model model, ContentManager content, string path)
{
    return content.Load<Model>(path);
}

编辑:

我刚刚测试了上面的代码,显然,而不是使用驱动器号,您需要匹配私有(或受保护,我是没有把握)。没有使用反射,我认为无法访问该变量。如果我们确实知道完整的根目录路径,那么这应该可以工作:

string reversePath = "";
foreach (string level in Content.FullRootDirectory.Split('\\'))// I know there isn't actually a property 'FullRootDirectory' but for the sake of argument,
{
    reversePath += "..\\";
}
reversePath = reversePath.Substring(4);

我已经用谷歌搜索了几次,但无法找到获取 ContentManager 根目录的方法。我什至可能会在herehere 上提出这个问题。

好的,这是最后一件事(使用上面链接的问题的答案):

  1. 向您的项目添加对 System.Windows.Forms 的引用

  2. 将以下代码添加到 Game1.cs 文件的顶部:

    using System.IO;
    using TApplication = System.Windows.Forms.Application;
    
  3. 将此代码添加到您需要的任何位置,例如在 LoadContent 或扩展方法中:

    string ContentFullPath = Path.Combine(Path.GetDirectoryName(TApplication.ExecutablePath),
    Content.RootDirectory);
    
    string reversePath = "";
    foreach (string level in ContentFullPath.Split('\\'))
    {
        reversePath += "..\\";
    }
    
    reversePath = reversePath.Substring(3);
    
    
    Model test = Content.Load<Model>(Path.Combine(ContentFullPath, reversePath) + "*Your file name here*");
    

【讨论】:

  • 为什么我不能这样做。 Content.RootDirectory = "C:\\Content" 还有,为什么没有Model.FromStream却有Texture2D.FromStream???????
  • 好吧,我认为您实际上可以做到这一点,如果这是您需要的,那么我误解了您的问题。至于为什么,你可以用微软来解决这个问题,但我认为这可能是因为模型文件比纹理复杂得多。此外,它默认不支持动画,因此您需要制作自定义内容处理器或使用蒙皮模型示例进行动画。
  • 加载模型也会加载其材质,其中包含效果和纹理。这两个都是 IDisposable 但 Model 不是,那么如何清理它们?好吧,因为当您加载模型时,它们也会在内部通过内容管理器加载。
猜你喜欢
  • 1970-01-01
  • 2016-08-31
  • 2020-06-05
  • 2021-07-21
  • 2018-10-31
  • 1970-01-01
  • 2021-02-19
  • 1970-01-01
  • 2020-12-08
相关资源
最近更新 更多