首页 > 解决方案 > 在 Unity 中从 Http 请求结果设置 GameObject

问题描述

我的项目中有 4 个按钮,所有这些按钮都从服务器调用不同的分数结果。在唤醒函数中,我生成了一个带有随机值的表,它工作得很好。但是,当我调用一个函数并获得结果时,我无法使用接收到的值设置表,因为 entryContainer 和 entryTemplate 将为空,我认为不应该是因为它们被实例化了!这是我的代码,在打开页面时效果很好,但在收到请求后无法设置项目

using Assets.Scripts.HttpPost;
using Assets.Scripts.Models;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

public class HighScoreTable : MonoBehaviour
{
    public static Transform entryContainer;
    public static Transform entryTemplate;
    public List<HighscoreEntry> highscoreEntryList;
    public static List<Transform> highscoreEntryTransformList;
    public static bool isChanged = false;

    public void Awake()
    {

        // Works pretty fine at opening
        entryContainer = transform.Find("highscoreEmptyContainer");
        entryTemplate = entryContainer.Find("highscoreEntryTemplate");

        entryTemplate.gameObject.SetActive(false);

        highscoreEntryList = new List<HighscoreEntry>() {
            new HighscoreEntry{score = 33.6, username = "Osman"},
            new HighscoreEntry{score = 46033.5, username = "Ahmet"},
            new HighscoreEntry{score = 1833.5, username = "Veli"},
            new HighscoreEntry{score = 563.5, username = "Can"},
            new HighscoreEntry{score = 433.5, username = "Esma"},
            new HighscoreEntry{score = 6533.5, username = "Gözde"},
            new HighscoreEntry{score = 733.5, username = "Ayşe"},
            new HighscoreEntry{score = 733.5, username = "Fatma"},
            new HighscoreEntry{score = 383.5, username = "Hayriye"}
        };
        //generates table its ok 
        highscoreEntryTransformList = new List<Transform>();
        foreach (var highscoreEntry in highscoreEntryList)
        {
            CreateHighscoreEntryTransform(highscoreEntry, entryContainer, highscoreEntryTransformList);
        }
    }

    public void Update()
    {

    }
    private void CreateHighscoreEntryTransform(HighscoreEntry highscoreEntry, Transform container, List<Transform> transformList)
    {
        float templateHeight = 30f;
        while (container == null)
            container = transform.Find("highscoreEmptyContainer");
        Transform entryTransform = Instantiate(entryTemplate, entryContainer);
        RectTransform entryRectTransform = entryTransform.GetComponent<RectTransform>();
        entryRectTransform.anchoredPosition = new Vector2(0, -30 - templateHeight * transformList.Count);
        entryTransform.gameObject.SetActive(true);

        int rank = transformList.Count + 1;

        string rankString;

        switch (rank)
        {
            default:
                rankString = rank + "TH"; break;
            case 1: rankString = "1ST"; break;
            case 2: rankString = "2ND"; break;
            case 3: rankString = "3RD"; break;
        }
        entryTransform.Find("posText").GetComponent<Text>().text = rankString;
        double score = highscoreEntry.score;
        entryTransform.Find("scoreText").GetComponent<Text>().text = score.ToString();
        entryTransform.Find("nameText").GetComponent<Text>().text = highscoreEntry.username;

        transformList.Add(entryTransform);
    }
    #region AllVerbal
    public WWW AllVerbalGet(string postAddress, string jsonData)
    {
        entryContainer = transform.Find("highscoreEmptyContainer");
        entryTemplate = entryContainer.Find("highscoreEntryTemplate");

        entryTemplate.gameObject.SetActive(false);
        WWW www;
        byte[] formData = null;
        try
        {
            Hashtable postHeader = new Hashtable();
            postHeader.Add("Content-Type", "application/json");

            // convert json string to byte
            if (!string.IsNullOrEmpty(jsonData))
            {
                formData = System.Text.Encoding.UTF8.GetBytes(jsonData);
            }
            www = new WWW(postAddress, formData, postHeader);
            StartCoroutine(WaitForAllVerbalGetRequest(www));
        }
        catch (System.Exception ex)
        {
            www = null;
        }
        return www;
    }
    //Wait for the www Request
    IEnumerator WaitForAllVerbalGetRequest(WWW www)
    {
        yield return www;
        if (www.error == null)
        {
            //Print server response
            try
            {
                LeaderboardListModel result = JsonUtility.FromJson<LeaderboardListModel>("{\"leaders\":" + www.text + "}");
                highscoreEntryList = new List<HighscoreEntry>();
                // Items received and converted to model successfully!
                HighscoreEntry temp;
                foreach (var item in result.leaders)
                {
                    temp = new HighscoreEntry();
                    temp.username = item.username;
                    temp.score = item.totalScore;
                    temp.weekscore = item.weeklyTotalScore;
                    highscoreEntryList.Add(temp);
                }
                highscoreEntryTransformList = new List<Transform>();

                foreach (var highscoreEntry in highscoreEntryList)
                {
                    // HERE IS THE PROBLEM ENTRY CONTAINER IS NULL BUT ITS INSTANTIATED AT AWAKE FUNCTION!!!!!!!!!!!
                    // I ALSO TRY TO SET THESE ITEMS AGAIN BUT STILL  RETURNS NULL
                    //entryContainer = transform.Find("highscoreEmptyContainer");
                    //entryTemplate = entryContainer.Find("highscoreEntryTemplate");
                    //entryTemplate.gameObject.SetActive(false);
                    //WHAT SHOULD I DO entry container is null!! :(
                    CreateHighscoreEntryTransform(highscoreEntry, entryContainer, highscoreEntryTransformList);
                }
                isChanged = true;

            }
            catch (System.Exception ex)
            {
            }
        }
        else
        {
            try
            {
                highscoreEntryList = new List<HighscoreEntry>();
                highscoreEntryTransformList = new List<Transform>();
                foreach (var highscoreEntry in highscoreEntryList)
                {
                    CreateHighscoreEntryTransform(highscoreEntry, entryContainer, highscoreEntryTransformList);
                }
            }
            catch (System.Exception)
            {

            }
        }
    }

    #endregion

    public void allVerbalButton_Click()
    {
        isChanged = true;
        AllVerbalGet(HttpOperations.allVerbalURL, null);
    }

    public void allCompButton_Click()
    {
        isChanged = true;
        AllVerbalGet(HttpOperations.allCompURL, null);
    }

    public void Top10VerbalButton_Click()
    {
        isChanged = true;
        AllVerbalGet(HttpOperations.Top10VerbalURL, null);
    }

    public void Top10ComputButton_Click()
    {
        isChanged = true;
        AllVerbalGet(HttpOperations.Top10CompURL, null);
    }
    [System.Serializable]
    public class HighscoreEntry
    {
        public double score;
        public string username;
        public double weekscore;
    }
}

我在团结方面很新,但无法解决它:(任何帮助都会很棒

标签: c#unity3dhttp-post

解决方案


推荐阅读