【问题标题】:Unity C# Load textrue with wwwUnity C# 使用 www 加载 textrue
【发布时间】:2013-05-29 11:01:26
【问题描述】:

我正在制作一个通过读取 XML 来加载关卡的游戏。那部分工作正常,但我无法加载纹理。他没有给出错误或类似的东西。只是没有 textrue,它是一个空对象。

这是读取 xml 的代码:

void ReadXML(){

    XmlDocument doc = new XmlDocument();
    doc.Load("world/world.xml");

    //load all textrues
    XmlNodeList elemList = doc.GetElementsByTagName("textrue");
    _matrial = new Texture[elemList.Count];
    for (int i=0; i < elemList.Count; i++){
        LoadTexture(elemList[i].InnerXml);
        _matrial[i] = loaded;
        //_matrial[i] = Resources.Load(elemList[i].InnerXml) as Texture;
        Debug.Log(_matrial[i] + " <> " + elemList[i].InnerXml);
    }


    ///.... here he loads al object, that works fine

}

这是 LoadTexture 方法:

IEnumerator LoadTexture(string url) {
    WWW www = new WWW(url);
    yield return www;
    loaded = www.texture;
}

谢谢!

【问题讨论】:

  • 可能是因为您在 GetElementsByTagName 上打错了“纹理”?此外,“loaded = www.texture”永远不会运行。
  • 我认为问题在于您实际上并没有等待 WWW 完成,所以 www.texturenull 因为它还没有加载:answers.unity3d.com/questions/44999/…
  • 是的,这是一个错字,但我在 xml 上也做了同样的错字,所以这部分可以工作。但谢谢。问题确实是等待。非常感谢!
  • 统一标签适用于 Microsoft Unity。请不要滥用它。

标签: c# web unity3d ienumerator


【解决方案1】:

它必须等到 textare 加载后才能使用它。所以函数应该是这样的:

IEnumerator LoadTexture(string url) {
    WWW www = new WWW(url);
    while (!www.isDone){
        yield return null;
    }
    loaded = www.texture;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 2017-12-22
    • 2014-10-12
    • 1970-01-01
    相关资源
    最近更新 更多