【问题标题】:Download Texture in Unity3d giving InvalidOperationException在 Unity3d 中下载纹理给出 InvalidOperationException
【发布时间】:2017-09-30 16:46:29
【问题描述】:

我正在尝试在我的统一项目中使用谷歌地图纹理。但我也面临同样的问题。

我检查了通话记录的 url,当我在浏览器中使用它时,它工作正常。我正在尝试将该纹理加载到立方体中。

这是正在使用的代码。有人可以指导我做错什么以及如何纠正吗?如果您需要更多详细信息,请告诉我。

方法 1 - 使用 WWW 方法:

string googleStaticMapsURL = "https://maps.googleapis.com/maps/api/staticmap?center=42.49414,-83.40547&size=1024x1024&scale=2&maptype=roadmap&markers=size:mid|color:orange|label:abc|44.49414,-83.40547";

WWW req = new WWW(googleStaticMapsURL);

cubeObject.SetActive (true);

// Create a texture in DXT1 format
cubeObject.GetComponent<Renderer>().material.mainTexture = 
                new Texture2D(size, size, TextureFormat.DXT1, false);

while (!req.isDone) {
    Debug.Log ("req.isdone is false");
    yield return 0;
}

if (req.error == null) {
    Debug.Log ("Response form Google maps texture service:: ");
    req.LoadImageIntoTexture ((Texture2D)cubeObject.GetComponent<Renderer> ().material.mainTexture);
}  else {
    Debug.Log ("Google maps.. Req.error is:: " + req.error);
} 

错误:不支持的网址。

我在一些帖子中读到 WWW 在 iOS 中不能很好地工作,并尝试使用 UnityWebRequest。所以我修改了如下代码。

方法 2 - 使用 UnityWebRequest 方法:

string googleStaticMapsURL = "https://maps.googleapis.com/maps/api/staticmap?center=42.49414,-83.40547&size=1024x1024&scale=2&maptype=roadmap&markers=size:mid|color:orange|label:abc|44.49414,-83.40547";

UnityWebRequest www = UnityWebRequest.Get(googleStaticMapsURL);
DownloadHandlerTexture textD = new DownloadHandlerTexture ();
www.downloadHandler = textD;

yield return www.Send();

if (www.isError) {
    Debug.Log ("Error in Google maps web service::" + www.error);
}  else {
    Debug.Log ("Time1:: " + Time.time + ".. isDone::" + www.isDone);
    yield return new WaitForSeconds(3); //wait for 3 secs
    Debug.Log ("Time2:: " + Time.time + ".. isDone::" + www.isDone);
    while (!www.isDone) {
        Debug.Log ("www.isdone is false");
    }
    // Show results as text
    //sphereObject.GetComponent<Renderer>().material.mainTexture = 
    //                          new Texture2D(size, size, TextureFormat.DXT1, false);
    Debug.Log("Setting Texture!!");
    Debug.Log("Response form Google maps texture service:: "+ textD.texture.height);
    cubeObject.SetActive (true);
    //cubeObject.transform.parent.gameObject.SetActive (true);
    cubeObject.GetComponent<Renderer>().material.mainTexture = textD.texture;
}

错误:InvalidOperationException:纹理尚未完成下载

但我可以看到 www.isDone 打印为 True。你能帮我解决这个问题吗?

另外,我正在使用 https 版本的谷歌地图网址,但我仍然在控制台中看到以下警告。打印出来的 url 只显示 https 协议。

您正在使用通过 http 下载。目前,unity 将 NSAllowsArbitraryLoads 添加到 Info.plist 以简化转换,但很快就会被删除。请考虑更新到 https。

【问题讨论】:

  • 嗨朋友们,你能告诉我为什么它被否决了吗?如果您觉得需要更多信息,请告诉我。
  • @Hristo,我已经编辑了我的问题。你介意再看一遍,帮帮我吗?谢谢。

标签: c# google-maps unity3d unityscript


【解决方案1】:

我不知道为什么它不适合你,只是将此脚本附加到立方体对象并且它工作正常:

public class TextureLoader : MonoBehaviour 
{

    IEnumerator Start()
    {
        string path = "https://maps.googleapis.com/maps/api/staticmap?center=42.49414,-83.40547&size=1024x1024&scale=2&maptype=roadmap&markers=size:mid|color:orange|label:abc|44.49414,-83.40547";
        UnityWebRequest www = UnityWebRequest.Get(path);
        DownloadHandlerTexture textD = new DownloadHandlerTexture ();
        www.downloadHandler = textD;

        yield return www.Send();

        while (!www.isDone)
        {
            yield return www;
        }

        if (www.isError) 
        {
            Debug.Log ("Error in Google maps web service::" + www.error);
        }  
        else 
        {
            GetComponent<Renderer>().material.mainTexture = textD.texture;
        }
    }
}

【讨论】:

  • 感谢您的更新。我也尝试运行您的代码,但在尝试在设备(iPhone 6、iOS 10.3)中运行时遇到相同的问题“InvalidOperationException:纹理尚未完成下载”,但在 Unity 播放模式下运行良好。这可能是iOS中的一些问题吗?知道如何解决吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-27
  • 1970-01-01
  • 2013-09-06
  • 2015-06-13
相关资源
最近更新 更多