【问题标题】:Show multiple images downloaded from a server显示从服务器下载的多个图像
【发布时间】:2018-05-12 10:58:27
【问题描述】:

我想下载我已上传到服务器的多张图片,并在我的场景中的画廊或幻灯片中显示。我已经完成了下面的代码来下载图像,但我只能显示一张图像。如何显示从服务器下载的多张图片?

public void DownloadtheFiles()
    {

    List <string> photolist = ES2.LoadList<string>("myPhotos.txt");

    for (int i = 0; i < photolist.Count; i++) {

        new GetUploadedRequest()

            .SetUploadId(photolist[i])
            .Send((response) =>
                {
                    StartCoroutine(DownloadImages(response.Url));
                } );
    }
    }

    public IEnumerator DownloadImages(string downloadUrl)
    {
        var www = new WWW(downloadUrl);
        yield return www;
        downloadedImages = new Texture2D(200, 200);
        www.LoadImageIntoTexture(downloadedImages);
        imageLoaded.texture = downloadedImages as Texture;
    }

更新 1:使用下面的代码,我展示了我希望如何展示它们,但它从文件夹路径获取图像,我需要展示我从服务器下载的图像。如何集成此代码以使用下载的图像制作幻灯片?

public class ImageLoader : MonoBehaviour
{
[SerializeField]
[Tooltip("The folder where images will be loaded from")]
private string imagePath;

[SerializeField]
[Tooltip("The panel where new images will be added as children")]
private RectTransform content;

private List<Texture2D> textures;

private void Start()
{
    Application.runInBackground = true;
    StartCoroutine(LoadImages());
}

public IEnumerator LoadImages()
{
    textures = new List<Texture2D>();

    DirectoryInfo di = new DirectoryInfo(imagePath);
    var files = di.GetFiles("*.png");

    foreach (var file in files)
    {
        Debug.Log(file.FullName);
        yield return LoadTextureAsync(file.FullName, AddLoadedTextureToCollection);
    }

    CreateImages();
}

private void AddLoadedTextureToCollection(Texture2D texture)
{
    textures.Add(texture);
}

private void CreateImages()
{
    foreach(var texture in textures)
    {
        GameObject imageObject = new GameObject("Image");
        imageObject.transform.SetParent(content);
        imageObject.AddComponent<Image>().sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
    }
}

public IEnumerator LoadTextureAsync(string originalFileName, Action<Texture2D> result)
{
    string fileToLoad = GetCleanFileName(originalFileName);

    Debug.Log("Loading Image from path: " + fileToLoad);

    WWW www = new WWW(fileToLoad);
    yield return www;

    Texture2D loadedTexture = new Texture2D(1, 1);

    www.LoadImageIntoTexture(loadedTexture);

    result(loadedTexture);
}

private static string GetCleanFileName(string originalFileName)
{
    string fileToLoad = originalFileName.Replace('\\', '/');

    if (fileToLoad.StartsWith("http") == false)
    {
        fileToLoad = string.Format("file://{0}", fileToLoad);
    }

    return fileToLoad;
}
}

更新 2:我创建了一个 ScrollView 和 Horizo​​natalLayoutGroup,并应用了更新 1 的 ImageLoader.cs。我在文件夹中添加了 4 个图像,这些是层次结构和结果的屏幕截图:

它作为测试工作正常,但图像的来源是我电脑中的一个文件夹,我想下载服务器的图像。我该怎么做?

【问题讨论】:

  • 你想在哪里显示图像?用户界面? 2D 还是 3D 对象?
  • 我想在 UI 中显示所有图片
  • 嗨@Programmer 有什么办法解决这个问题吗?
  • 什么 UI 组件?也许它在 Inspector 选项卡中的屏幕截图?如果您尝试下载多个图像,那么您将需要多个该 UI 组件。你有多少?抱歉,这还不足以帮助您。
  • @Programmer 现在可以正常工作了!!!非常感谢您对我的耐心等待!!!!

标签: c# unity3d


【解决方案1】:

您已经使用ScrollViewHorizonatalLayoutGroup 完成了所有必需的部分。请注意,我要求您使用 RawImage 组件,但您似乎正在使用 Image 组件。我推荐RawImage,因为可以避免使用昂贵的Sprite.Create。如果你愿意,你仍然可以使用Image

您现在唯一要做的就是调整RawImage 的大小,直到您对它的大小感到满意为止。从 RawImage 创建 一个 预制件。删除 Content GameObject 下的 Image/RawImage。你不再需要它们了。

现在,从服务器下载您的图像,从该 RawImage 预制件中实例化一个预制件。 最后,让 RawImage 成为 Content GameObject 的子对象。 就是这样。 HorizonatalLayoutGroup 应该会自动定位它。

使用您的原始代码,以下是如何做到这一点。 contentRef 变量是对 ScrollView 下的 Content GameObject 的引用。 imgPrefab 变量是对RawImage 预制件的引用。确保从编辑器分配两者。另外,请注意我是如何添加和使用新的int index = i; 变量来防止capturingi 变量并导致它只下载最后一张图片。

public GameObject contentRef;
public RawImage imgPrefab;

void Start()
{
    DownloadtheFiles();
}

public void DownloadtheFiles()
{

    List<string> photolist = ES2.LoadList<string>("myPhotos.txt");

    for (int i = 0; i < photolist.Count; i++)
    {
        //Don't capture i variable
        int index = i;

        new GetUploadedRequest()

            .SetUploadId(photolist[index])
            .Send((response) =>
            {
                StartCoroutine(DownloadImages(response.Url, index));
            });
    }
}


public IEnumerator DownloadImages(string downloadUrl, int index)
{
    var www = new WWW(downloadUrl);
    yield return www;

    //Instantiate the image prefab GameObject and make it a child of the contentRef
    RawImage newImg = Instantiate(imgPrefab, contentRef.transform);
    //Change the name
    newImg.name = "Image-" + index;

    //Get the downloaded image
    Texture2D tex = new Texture2D(4, 4);
    www.LoadImageIntoTexture(tex);

    //Apply the downloaded image
    newImg.texture = tex;
}

【讨论】:

  • @Programer 每次我进入场景时,图像都会以不同的顺序显示。是否有可能总是以相同的顺序出现?
  • 它们应该根据它们在内容对象下的层次结构中的顺序出现。这不就是现在的情况吗?
  • 是的,但是每次打开场景这个顺序都不一样
  • 你能创建关于这个的新帖子吗?那会更好。确保解释帮助您所需的一切。在加载图像时发布它的屏幕截图并在层次结构中发布,然后在重新打开图像后再次发布另一个。还要解释为什么您认为重新启动编辑器时会出现未保存的下载图像。
  • 好的。我要创建一个新帖子。谢谢你!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-04
  • 2013-03-04
  • 2018-10-24
相关资源
最近更新 更多