Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

c# - Make a list of achievements in a scroll view

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

enter image description here

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

Hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...