Files
XCharts/Assets/Demo.cs

53 lines
1.4 KiB
C#
Raw Normal View History

2018-09-15 06:52:42 +08:00
using UnityEngine;
2018-09-25 07:33:04 +08:00
using UnityEngine.UI;
2018-09-15 06:52:42 +08:00
using xcharts;
2018-09-12 07:27:22 +08:00
public class Demo : MonoBehaviour
{
2018-10-02 08:57:47 +08:00
public Theme theme = Theme.Dark;
2019-03-16 07:49:36 +08:00
private BaseChart chart;
2018-09-12 07:27:22 +08:00
private float time;
2018-09-21 22:30:48 +08:00
private int count;
2018-10-02 08:57:47 +08:00
private Theme checkTheme = Theme.Dark;
2018-09-12 07:27:22 +08:00
2018-09-21 22:30:48 +08:00
void Awake()
{
2018-09-25 07:33:04 +08:00
var xchart = transform.Find("xchart");
GridLayoutGroup grid = xchart.GetComponent<GridLayoutGroup>();
RectTransform rect = transform.GetComponent<RectTransform>();
2018-09-25 07:33:04 +08:00
var wid = rect.sizeDelta.x;
int childNum = xchart.childCount;
2019-03-16 07:49:36 +08:00
float hig = grid.padding.top + childNum * (grid.cellSize.y + grid.spacing.y);
rect.sizeDelta = new Vector2(wid, hig);
xchart.GetComponent<RectTransform>().sizeDelta = new Vector2(wid, hig);
2019-03-16 07:49:36 +08:00
chart = xchart.gameObject.GetComponentInChildren<BaseChart>();
2018-09-12 07:27:22 +08:00
}
2018-09-21 22:30:48 +08:00
void Update()
{
2018-09-12 07:27:22 +08:00
time += Time.deltaTime;
if (time >= 1)
{
time = 0;
2018-09-21 22:30:48 +08:00
count++;
2019-03-16 07:49:36 +08:00
chart.UpdateData(0, Random.RandomRange(60, 150));
2018-09-12 07:27:22 +08:00
}
2019-03-16 07:49:36 +08:00
if (checkTheme != theme)
2018-10-02 08:57:47 +08:00
{
checkTheme = theme;
UpdateTheme(theme);
}
}
void UpdateTheme(Theme theme)
{
var charts = transform.Find("xchart").GetComponentsInChildren<BaseChart>();
2019-03-16 07:49:36 +08:00
foreach (var chart in charts)
2018-10-02 08:57:47 +08:00
{
chart.UpdateTheme(theme);
}
2018-09-12 07:27:22 +08:00
}
}