在滚动视图中列出成就列表

时间:2018-06-13 07:28:48

标签: c# unity3d

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

我在下面附上了我想要显示的列表的图像以及我在层次结构中的滚动视图的屏幕截图。

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

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以将元素(名称,描述,货币)分配给相同的UITexts。 这就像你试图存储20个数字(0,1,2 ... 19),但你只有一个变量(int)来存储它们。

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

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();  
}

希望这有帮助。