更快显示图像 [英] Show images faster

查看:148
本文介绍了更快显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个成就列表,每个成就都有不同的图像.我已经从服务器下载了图像,但是它们在我的游戏中显得很慢.这是一种更快显示它们的方法吗?预先谢谢你.

I want to show a list of achievements each one with a different image. I've downloaded the images from the server but they appear very slow in my game. Is it a way to show them faster? Thank you in advance.

这是我的代码:

public GameObject filePrefab;
public GameObject contentRef;

private Texture2D downloadedImages;

public void AchievementsList_Bttn()
{
    new GameSparks.Api.Requests.LogEventRequest ()
        .SetEventKey ("LISTACHIEVEMENTS")
        .Send ((response) => {

            if(!response.HasErrors)
            {
                Debug.Log("List Achivements Loaded Sucessfully...");
                GSData scriptData = response.ScriptData;
                List<GSData> achievements = scriptData.GetGSDataList("achievements"); //retrieve the array of objects
                for (int i = 0; i < achievements.Count; i++)
                {
                    string name = achievements[i].GetString("name");
                    string description = achievements[i].GetString("description");
                    int? currency1Award = achievements[i].GetInt("currency1Award");
                    bool? earned = achievements[i].GetBoolean("earned");

                    GameObject tempFile = Instantiate (filePrefab, contentRef.transform);
                    Text tempName = tempFile.transform.GetChild(0).GetComponent<Text>();
                    Text tempDescription = tempFile.transform.GetChild(1).GetComponent<Text>();
                    Text tempCurrency1Award = tempFile.transform.GetChild(2).GetComponent<Text>();
                    RawImage tempImage = tempFile.transform.GetChild(3).GetComponent<RawImage>();

                    tempName.text = name;
                    tempDescription.text = description;
                    tempCurrency1Award.text = currency1Award.ToString();

                    DownloadtheFiles(name, tempImage);
                }
            }
        });
}

public void DownloadtheFiles(string name, RawImage tempImage)
{
    new GetDownloadableRequest()
        .SetShortCode(name+"_icon")
        .Send((response) => {
            if(!response.HasErrors)
            {
                StartCoroutine(DownloadImages((response.Url), tempImage));
            }
        });
}

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

这就是我要显示的内容:

And this is what I want to show:

推荐答案

  • 小点,但绝对慢并且经常出现:
  • 字符串名称=成就[i] .GetString(名称");

    string name = achievements[i].GetString("name");

    在循环中进行字典查找.获取循环之外的字段的序数,将它们放入一个变量中,然后使用GetString(variable)(变量为整数类型)-更快的查找.

    makes a dictionary lookup in the loop. Get the ordinals of the fields outside the loop, pu them into a variable and then use GetString(variable) (with the variable being integer type) - faster lookips.

    • 使用探查器.

    就像DownloadTheFiles一样,它是瓶颈(不要让我们做您的基础工作),并且除了隐藏它(即预加载之类的东西)之外,没有其他方法可以解决此问题.就是这样,没有您做基础工作,也没有进行一些调试和配置,这是唯一能脱颖而出的事情,因为它确实需要为每个图像真正进行网络回合丢弃.

    Likely DownloadTheFiles is the bottleneck (do NOT make us do your basic work) and there is no way around this outside of hiding it (i.e. preloading or something else). It is, without you doing the base work and actually doing some debugging and profiling, the only thing that stands out because it does need to really do a network round drop for every image.

    这篇关于更快显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆