Get JSON data from Web in Unity

Viewed 40

there is a problem that I haven't been able to solve for a few days. im using this asset from assetstore (https://assetstore.unity.com/packages/tools/gui/enhancedscroller-36378)

For example, I have json data that looked like: link : jsonplaceholder.typicode.com/todos/1

{

  "userId": 1,

  "id": 1,

  "title": "delectus aut autem",

  "completed": false

}

And i set up the data class like:

public class ScrollerData

{

    public int userId;

    public int id;

    public string title;

    public bool completed;

}

and my controller is like that :

using UnityEngine;

using UnityEngine.UI;

using System.Collections;

using System.Collections.Generic;

using EnhancedUI.EnhancedScroller;

using UnityEngine.Networking;

public class ScrollerController : MonoBehaviour, IEnhancedScrollerDelegate

{

    private List<ScrollerData> _data;

    public EnhancedScroller myScroller;

    public AnimalCellView animalCellViewPrefab;

    public string _textURL;

    void Start()

    {

        StartCoroutine(GetText());

        _data = new List<ScrollerData>();

    

        myScroller.Delegate = this;

        myScroller.ReloadData();

    }

    public int GetNumberOfCells(EnhancedScroller scroller)

    {

        return _data.Count;

        

    }

    public float GetCellViewSize(EnhancedScroller scroller, int dataIndex)

    {

        return 300f;

    }

      private IEnumerator GetText()

    {

        using(UnityWebRequest request = UnityWebRequest.Get(_textURL))

        {

            yield return request.SendWebRequest();

            if(request.isHttpError||request.isNetworkError)

            {

                Debug.Log(request.error);

            }

            else

            {

                Debug.Log("Successfully downloaded text");



                var text = request.downloadHandler.text;

                ScrollerData catFact = JsonUtility.FromJson<ScrollerData>(text);

               // _text.text = catFact.fact;

                Debug.Log(text);

            }

        }

    }

    public EnhancedScrollerCellView GetCellView(EnhancedScroller scroller, int

    dataIndex, int cellIndex)

    {

        AnimalCellView cellView = scroller.GetCellView(animalCellViewPrefab) as

        AnimalCellView;

        cellView.SetData(_data[dataIndex]);

        return cellView;

    }

}

and the cell view is like :

using UnityEngine;

using UnityEngine.UI;

using System.Collections;

using EnhancedUI.EnhancedScroller;

public class AnimalCellView : EnhancedScrollerCellView

{

    public Text animalNameText;

    public void SetData(ScrollerData data)

    {

        animalNameText.text = data.title;

    }

}


my question is i want to use the json data in the controler in

public EnhancedScrollerCellView GetCellView(EnhancedScroller scroller, int

    dataIndex, int cellIndex)

    {

        AnimalCellView cellView = scroller.GetCellView(animalCellViewPrefab) as

        AnimalCellView;

        cellView.SetData(_data[dataIndex]);

        return cellView;

    }

but because is couroutine i cant get data from json anyone can help me

0 Answers
Related