Post by echo17 on Sept 3, 2022 12:10:17 GMT
EnhancedScroller can use any data source (or none at all if you prefer). The scroller itself doesn't see the data or how it is structured, so your controller will handle that.
For example, if you had some json data that looked like:
And you set up your data class like:
Then your controller could look like:
and your cell view like:
For example, if you had some json data that looked like:
{
"records": [
{
"fieldA": "Hello",
"fieldB": 22.2
},
{
"fieldA": "World",
"fieldB": 100.1
},
{
"fieldA": "Something",
"fieldB": 9.2
}
]
}
And you set up your data class like:
using System;
[Serializable]
public class Data
{
public Record[] records;
}
[Serializable]
public class Record
{
public string fieldA;
public float fieldB;
}
Then your controller could look like:
using UnityEngine;
using System.Collections;
using EnhancedUI;
using EnhancedUI.EnhancedScroller;
public class JsonDemo : MonoBehaviour, IEnhancedScrollerDelegate
{
private Data _data;
public EnhancedScroller scroller;
public EnhancedScrollerCellView cellViewPrefab;
public TextAsset json_data;
void Start()
{
Application.targetFrameRate = 60;
scroller.Delegate = this;
_data = JsonUtility.FromJson<Data>(json_data.text);
scroller.ReloadData();
}
public int GetNumberOfCells(EnhancedScroller scroller)
{
return (_data != null && _data.records != null ? _data.records.Length : 0);
}
public float GetCellViewSize(EnhancedScroller scroller, int dataIndex)
{
return 200f;
}
public EnhancedScrollerCellView GetCellView(EnhancedScroller scroller, int dataIndex, int cellIndex)
{
CellView cellView = scroller.GetCellView(cellViewPrefab) as CellView;
cellView.name = "Cell " + dataIndex.ToString();
cellView.SetData(_data.records[dataIndex]);
return cellView;
}
}
and your cell view like:
using UnityEngine;
using UnityEngine.UI;
using EnhancedUI.EnhancedScroller;
public class CellView : EnhancedScrollerCellView
{
public Text someTextText;
public void SetData(Record data)
{
someTextText.text = data.fieldA + ": " + data.fieldB.ToString();
}
}