相關鏈接
插件作者主頁:
插件下載鏈接:
- https://github.com/monitor1394/unity-ugui-XCharts:插件源項目
- https://download.csdn.net/download/f_957995490/12189881:我自己導出的unitypackage
創建
如下圖指示的創建例子圖表(線形圖)。
代碼
編寫代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XCharts;
public class XChartsTest : MonoBehaviour
{
LineChart chart;
string name1 = "test";
string name2 = "test2";
void Awake()
{
chart = GetComponent<LineChart>();
}
void Start()
{
chart.title.show = true;
chart.title.text = "Line Text";
chart.tooltip.show = true;
chart.legend.show = true;
chart.legend.data.Clear();
chart.legend.data.Add(name1);
chart.legend.data.Add(name2);
chart.xAxises[0].show = true;
chart.xAxises[1].show = false;
chart.yAxises[0].show = true;
chart.yAxises[1].show = false;
chart.xAxises[0].type = Axis.AxisType.Value;
chart.yAxises[0].type = Axis.AxisType.Category;
int[] data1 = { 10, 20, 30, 10, 50 };
int[] data2 = { 50, 60, 10, 50, 10 };
chart.xAxises[0].splitNumber = data1.Length;
chart.RemoveData();
for (int i = 0; i < data1.Length; i++)
{
chart.AddYAxisData(i.ToString());
}
chart.AddSerie(SerieType.Line, name1);
chart.AddSerie(SerieType.Line, name2);
foreach (int i in data1)
{
chart.AddData(name1, i);
}
foreach (int i in data2)
{
chart.AddData(name2, i);
}
time = 0;
}
private float time;
void Update()
{
if (2.0f <= time)
{
time = 0;
float temp = Random.Range(10, 70);
chart.UpdateData(name1, 2, temp);
}
time += Time.deltaTime;
}
}
然後,掛在創建的線形圖表上。
運行之後,效果如下:
當然圖中的紅線2號節點會每2秒鐘運動一次。