【问题标题】:Unity LoadImageIntoTexture isn't workingUnity LoadImageIntoTexture 不起作用
【发布时间】:2016-10-02 19:12:42
【问题描述】:

我有一些代码可以让我在我的 Android 设备中获取所有图像路径。然后,我想使用 www 类将这些图像加载到纹理中,例如:

public void SetImage()
{
    List<string> galleryImages = GetAllGalleryImagePaths();

    DebugText.text = galleryImages.Count.ToString() + " images found";

    DisplayPanel.SetActive(true);
    ControlPanel.SetActive(false);

    for (int i = 0; i < galleryImages.count; i++)
    {
        WWW www = new WWW(galleryImages[i]);

        Texture2D t = new Texture2D(2, 2);
        www.LoadImageIntoTexture(t);

        GameObject imgObj = Instantiate(Resources.Load("GalleryImage")) as GameObject;
        imgObj.GetComponent<RawImage>().texture = t;
        imgObj.transform.SetParent(contentHolder.transform);
    }
}

但是,如果我调用 www.LoadImageIntoTexture(t) 并循环太多次,应用程序只会跳到主屏幕。 (几次,比如20次就可以了)

有谁知道这个问题以及如何解决它?

【问题讨论】:

    标签: android image web unity3d textures


    【解决方案1】:

    您无需等待下载完成即可继续。您要么必须让 WWW 对象返回,要么手动检查它们是否完成。

    要将其作为协程工作,您可以将代码修改为

    public IEnumerator SetImage()
    {
        List<string> galleryImages = GetAllGalleryImagePaths();
    
        DebugText.text = galleryImages.Count.ToString() + " images found";
    
        DisplayPanel.SetActive(true);
        ControlPanel.SetActive(false);
    
        for (int i = 0; i < galleryImages.count; i++)
        {
            WWW www = new WWW(galleryImages[i]);
            yield return www; //Wait for the download to complete
    
            Texture2D t = new Texture2D(2, 2);
            www.LoadImageIntoTexture(t);
    
            GameObject imgObj = Instantiate(Resources.Load("GalleryImage")) as GameObject;
            imgObj.GetComponent<RawImage>().texture = t;
            imgObj.transform.SetParent(contentHolder.transform);
        }
    }
    

    要么这样,要么检查每个WWWinstance 中的isDone

    【讨论】:

    • 是的,他没有在等。
    猜你喜欢
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多