在滚动视图中列出成就列表 [英] Make a list of achievements in a scroll view

查看:106
本文介绍了在滚动视图中列出成就列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想列出玩家可以在我的游戏中获得的所有成就.使用下面的代码,我从服务器中检索成就,并在Unity中创建了一个滚动列表以显示这些成就,但仅显示第一行.我想显示所有这些的名称,描述和货币.如果bool获得的变量为true,我还想更改玩家获得的成就的颜色.

I want to make a list of all the achievements the player can earn in my game. With the code bellow I retrieve the achievements from the server and I've made a scroll list in Unity to show them but only the first row appears. I'd like to show the name, description and currency of all of them. I'd also like to change the color of the achievements the player has earned if bool earned variable is true.

我已经在想要显示的列表的图像和我在层次结构中拥有的滚动视图的屏幕快照的下方附加了该图像.

I've attached below an image of the list I'd like to appear and a screenshot of the scrollview I have in the hierarchy.

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");

                    Debug.Log(name);
                    Debug.Log(description);
                    Debug.Log(currency1Award);
                    Debug.Log(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>();

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

            }
            else
            {
                Debug.Log("Error Loading Achivements...");
            }
        });

推荐答案

您将元素(名称,描述,货币)分配给相同的UIText. 就像您尝试存储20个数字(0,1,2 ... 19),但是只有一个变量(int)来存储它们一样.

You assign the elements (name, description, currency) to the same UITexts. It is like you try to store 20 numbers (0,1,2...19), but you only have one variable (int) to store them.

尝试根据需要在for循环中创建文本. 这是一个示例:

Try to create the texts in the for loop as much as you need. Here is an example:

public GameObject filePrefab; // to be able to instantiate new "files"

...

for (...)
{
    // Create the file and assign the valuse
    GameObject tempFile = Instantiate( filePrefab);
    Text tempName = tempFile.GetChild(0).GetComponenet<Text>();
    Text tempDescription = tempFile.GetChild(1).GetComponenet<Text>();
    Text tempCurrency1Award = tempFile.GetChild(2).GetComponenet<Text>();

    // Here you can set there position, etc.
    ...

    // Assign the values
    tempName.text = name;
    tempDescription.text = description;
    tempCurrency1Award.text = currency1Award.ToString();  
}

希望这会有所帮助.

这篇关于在滚动视图中列出成就列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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