看到一位大佬做的,花了一点时间才复现出来。


public class LineChart : MaskableGraphic,ICanvasRaycastFilter
{ [SerializeField] private int lineCount = 1;[SerializeField] private List<UnityEngine.Color> lineColors = new List<UnityEngine.Color>() { };[SerializeField] private int xwidth = 10;//数据间隔[SerializeField] private float pointWidth = 1;//点的宽度[SerializeField] private float linewidth = 3;//线的宽度private List<float> pointList = new List<float>();private Vector3 pos;//点的坐标private RectTransform rectTransform;private Text numText;//两个数据之间的间隔private float xLength=200f;//最多显示的数据数量private const int RemainCount = 10;public Vector3 Pos{get{return pos;}}protected override void Awake(){base.Awake();rectTransform = GetComponent<RectTransform>();//xLength = transform.parent.parent.GetComponent<RectTransform>().sizeDelta.x;//numText = transform.Find("NumText").GetComponent<Text>();}private bool IsReachLength = false;public void AddPoint(float point){pointList.Add(point);int count = pointList.Count;if (count > lineCount)//如果只有一条曲线,则至少有两个点才可以开始绘制曲线{Vector2 size = rectTransform.sizeDelta;rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, xwidth * (count)); //当数据量达到我们设定的显示上限  数据个数保持不变  这个时候设置他的大小是不发生变化的 rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, xwidth * (count + 1));//所以我们就先设置小一单位 在设置加一单位  保证大小变化 SetVerticesDirty();                                                                                             //此函数改变RectTransform的大小  可以触发OnPopulateMesh调用//SetAllDirty();if (size.x > xLength)//显示区域的大小{if (count > RemainCount)//当数据个数大于我们规定的显示个数  就需要移除前面的数据 {pointList.RemoveAt(0);Vector3 pos = transform.localPosition;transform.localPosition = pos + new Vector3(xwidth, 0, 0);//把显示往前移动一个单位 然后做移动动画}transform.DOLocalMoveX(transform.localPosition.x - xwidth, 0.3f);}}}protected override void OnPopulateMesh(VertexHelper vh){int _count = pointList.Count;//画线if (_count > lineCount){vh.Clear();for (int i = 0; i < _count - lineCount; i++){//让曲线宽度在各种斜率下宽度一致float k = (pointList[i + lineCount] - pointList[i]) / (xwidth);//float _y = Mathf.Sqrt(Mathf.Pow(k, 2) + 4);float _y = Mathf.Sqrt(Mathf.Pow(Mathf.Abs(k) + 1, 2)) * linewidth / 2;_y = Mathf.Abs(_y);UIVertex[] verts = new UIVertex[4];verts[0].position = new Vector3(xwidth * (i / lineCount), pointList[i] - _y / 2);verts[1].position = new Vector3(xwidth * (i / lineCount), _y / 2 + pointList[i]);verts[2].position = new Vector3(xwidth * ((i + lineCount) / lineCount), pointList[i + lineCount] + _y / 2);verts[3].position = new Vector3(xwidth * ((i + lineCount) / lineCount), pointList[i + lineCount] - _y / 2);for (int j = 0; j < 4; j++){verts[j].color = UnityEngine.Color.yellow;//verts[j].color = lineColors[(i % lineCount)];verts[j].uv0 = Vector2.zero;}vh.AddUIVertexQuad(verts);}}//draw quad  显示数据大小的方块点for (int i = 0; i < _count; i++){UIVertex[] quadverts = new UIVertex[4];quadverts[0].position = new Vector3((i / lineCount) * xwidth - pointWidth, pointList[i] - pointWidth);quadverts[0].color = UnityEngine.Color.white;quadverts[0].uv0 = Vector2.zero;quadverts[1].position = new Vector3((i / lineCount) * xwidth - pointWidth, pointList[i] + pointWidth);quadverts[1].color = UnityEngine.Color.white;quadverts[1].uv0 = Vector2.zero;quadverts[2].position = new Vector3((i / lineCount) * xwidth + pointWidth, pointList[i] + pointWidth);quadverts[2].color = UnityEngine.Color.white;quadverts[2].uv0 = Vector2.zero;quadverts[3].position = new Vector3((i / lineCount) * xwidth + pointWidth, pointList[i] - pointWidth);quadverts[3].color = UnityEngine.Color.white;quadverts[3].uv0 = Vector2.zero;vh.AddUIVertexQuad(quadverts);}}//鼠标在数据点public bool IsRaycastLocationValid(UnityEngine.Vector2 sp, UnityEngine.Camera eventCamera){Vector2 local;RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, sp, eventCamera, out local);Rect rect = GetPixelAdjustedRect();local.x += rectTransform.pivot.x * rect.width;local.y += rectTransform.pivot.y * rect.height;int _count = pointList.Count;for (int i = 0; i < _count; i++){if (local.x > (i / lineCount) * xwidth - 3f && local.x < ((i / lineCount) * xwidth + 3f) && local.y > (pointList[i] - 3f)&& local.y < (pointList[i] + 3f)){pos = new Vector3((i / lineCount) * xwidth, pointList[i], 0);return true;}}return false;}// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){}

原文在这:https://blog.csdn.net/qq_33994566/article/details/72730325

unity3d折线图相关推荐

  1. 【GraphMaker】Unity3D图表——柱状图、折线图、饼图

    http://blog.csdn.net/yongh701/article/details/71637090?utm_source=itdadao&utm_medium=referral 版权 ...

  2. Unity3D开发之折线图的制作(二)

    一年前写过一篇折线图的制作,当时显示效果还可以,只不过因为之前刚接触写博客,所以写的内容不是很完善,加上比较忙,都是草率记录的,代码结构也不是很好.昨天我又把这个项目的代码熟悉了一遍,然后把代码更改精 ...

  3. Unity3D开发之折线图制作

    因为之前有人问我有没有源码,看这篇博客没看懂.所以最近博主又把这个实现的代码精简了下放到这里.有兴趣的可以来看下.点击打开链接 . 柱状图.折线图这个在数据可视化中会经常用到,可是unity自身并不带 ...

  4. Unity3D开发之折线图的制作(三)折线图终结篇之抗锯齿

    至于怎么挂载脚本怎么能够运行请参考折线图制作(二)的文章. 之前写的折线图一直被锯齿烦恼着,一直无法使用.一开始一直以为要自己写边际模糊算法来模糊编辑的锯齿部分,这可太难了.后来我的美术搭档告诉我可以 ...

  5. qchart折现图_Qt Charts 动态实时折线图绘制

    在Qt Charts发布之前, Qt比较著名两个画图插件是 qwt和Qcustom, 其中Qcustom较轻量,只需要在project 中包含qcustomplot.h 和 qcustomplot.c ...

  6. echarts切换折线图变大_这个月,我就和折线图杠上了...

    作者 hustcc 蚂蚁金服·数据体验技术团队 TL;DR G2Plot 是一个注重于细节体验的通用统计图表库. 背景 折线图用于表示连续时间跨度内的数据,它通常用于显示某变量随时间的变化模式:是上升 ...

  7. graphpad做折线图坐标轴数字_pandas做数据可视化具体操作,快来看看吧

    常见的数据可视化库有: matplotlib 是最常见的2维库,可以算作可视化的必备技能库,由于matplotlib是比较底层的库,api很多,代码学起来不太容易. seaborn 是建构于matpl ...

  8. 设置echarts的grid、tooltip、柱状图渐变色、折线图渐变色

    grid: {show: false,left: '0px',top: '50px',right: '1px',bottom: '0px',containLabel: true,backgroundC ...

  9. 【统计图】Echarts实现多条折线图渐变堆叠效果

    initSgLineChart() {// 基于DOM,初始化echarts实例(注意!Vue的DOM日怪的很,一般要腾个1秒才加载完)this.lineChart = this.$echarts.i ...

最新文章

  1. TensorRT优化方案图例
  2. from beautifulsoup4 import BeautifulSoup 报错
  3. myeclipse 2015 stable 2.0连接mysql URL错误
  4. 苹果证实收购Drive.ai自动驾驶汽车初创公司
  5. 【算法】一个简单的决策树(DT)原理
  6. mysql load会锁表吗_Mysql必读MySQL中由load data语句引起死锁的解决案例
  7. ubuntu16.04安装CecureCRT 并破解
  8. python的代码在哪写_python代码在哪里编写
  9. ECCV 2020 论文大盘点 - OCR 篇
  10. 用双边模式,让生意立刻火爆
  11. 用python写一个简单的web服务器
  12. MacOS 开发 — Dock 显示网速/消息
  13. php post 漏洞_帝国CMS(EmpireCMS)v7.5漏洞分析 - LEOGG
  14. 优化 | Pick and delivery problem的简介与建模实现(一)
  15. Win10系统下如何有效的关闭易升(系统自动更新)
  16. (python)改进的k-shell算法来识别关键节点 The improved k-shell algorithm(IKS)
  17. Python3网络爬虫使用User Agent和代理IP隐藏身份
  18. 分布式服务器框架之搭建C#+MongoDB+Redis初步
  19. 如何快速开发一个App手机应用
  20. 2.机器学习复习笔记-各种评估指标

热门文章

  1. CIE LAB色差公式与 CIE DE 2000色差公式计算类
  2. 《数据结构》--内部排序算法比较
  3. arduino串口接收数据包_Arduino 串口接收转发十六进制数据,软串口收不到数据
  4. 矩阵笔记4:矩阵分析(第三版)-史荣昌-第四章:矩阵分解
  5. 少儿编程教育:scratch教学
  6. 聚焦大数据生态产业链
  7. 油电混合动力汽车行星齿轮箱设计(论文+CAD图纸)
  8. 起重机和起重机的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  9. Bounding Volume Hierarchy (BVH)
  10. python太阳花画法_python太阳花绘制