1.效果展示
2.基础类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class DrawLineChart : MonoBehaviour
{// 网格渲染器MeshRenderer meshRenderer;// 网格过滤器MeshFilter meshFilter;// 用来存放顶点数据List<Vector3> verts; // 顶点列表List<int> indices; // 序号列表public Material LineMat;public static List<List<int>> points = new List<List<int>>() {new List<int>() { 0, 1, 1, 2, 3, 4, 4, 5, 6, 6, 6, 7, 7, 8,8,8,8,9,9,9,10,11,12,13,14,14,15 },new List<int>() { 0, 0, 0, 1, 2, 3, 3, 4, 4, 4, 5, 5, 5, 5 },new List<int>() { 0, 0, 1, 2, 2, 2, 2, 3, 4, 5, 6, 6, 6, 6 },new List<int>() { 0, 1, 1, 1, 2, 3, 3, 4, 4, 4, 5, 5, 6, 7 },new List<int>() { 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 5 }};public static List<Color> colors = new List<Color>() {Color.red,Color.blue,Color.green,Color.cyan,Color.yellow};public Material mat;public int Index;private void Start(){verts = new List<Vector3>();indices = new List<int>();meshRenderer = GetComponent<MeshRenderer>();meshFilter = GetComponent<MeshFilter>();meshRenderer.material = mat;Draw(points[Index], colors[Index]);}private void Draw(List<int> points ,Color color) {List<Vector3> lineList = new List<Vector3>();int index = 0;for (int i = 0; i < points.Count; i++){int one = points[i];if (i + 1 < points.Count) {int two = points[i + 1];verts.Add(new Vector3(i, 0, 0));verts.Add(new Vector3(i, one, 0));verts.Add(new Vector3(i + 1, 0, 0));verts.Add(new Vector3(i + 1, 0, 0));verts.Add(new Vector3(i, one, 0));verts.Add(new Vector3(i + 1, two, 0));index += 6;}lineList.Add(new Vector3(i, one, 0));}lineList.Add(new Vector3(points.Count - 1, 0, 0));for (int i = 0; i < index; i++){indices.Add(i);}Mesh mesh = new Mesh();mesh.SetVertices(verts);mesh.SetIndices(indices, MeshTopology.Triangles, 0);// 自动计算法线mesh.RecalculateNormals();// 自动计算物体的整体边界mesh.RecalculateBounds();// 将mesh对象赋值给网格过滤器,就完成了meshFilter.mesh = mesh;meshRenderer.material.SetColor("_Color", color);GameObject lintObj = new GameObject();lintObj.transform.name = "Line";lintObj.transform.SetParent(transform);lintObj.transform.localPosition = Vector3.zero;LineRenderer lineRender = lintObj.AddComponent<LineRenderer>();lineRender.useWorldSpace = false;lineRender.startWidth = 0.1f;lineRender.endWidth = 0.1f;lineRender.positionCount = lineList.Count;lineRender.material = LineMat;lineRender.material.color = color;lineRender.SetPositions(lineList.ToArray());}
}
3.UI布局
4.具体用例
每个GameObject上都挂载DrawLineChart类,然后LineMat和Mat分别赋上材质,第一个LineMat是LinderRender画边缘线的材质,第二个Mat是折线图颜色渐变材质,Index为此GameObject所在的节点索引,从0开始。
5.LineMat材质Shader
Shader "Unlit/LineColorShader"
{Properties{_MainTex ("Texture", 2D) = "white" {}_Color("Color",Color) = (1,1,1,1)_Alpha("Alpha",Range(0,1)) = 1}SubShader{Tags { "RenderType"="Transparent" }LOD 100Pass{Blend SrcAlpha OneMinusSrcAlphaCGPROGRAM#pragma vertex vert#pragma fragment frag// make fog work#pragma multi_compile_fog#include "UnityCG.cginc"struct appdata{float4 vertex : POSITION;float2 uv : TEXCOORD0;};struct v2f{float2 uv : TEXCOORD0;UNITY_FOG_COORDS(1)float4 vertex : SV_POSITION;};sampler2D _MainTex;float4 _MainTex_ST;fixed4 _Color;float _Alpha;v2f vert (appdata v){v2f o;o.vertex = UnityObjectToClipPos(v.vertex);o.uv = TRANSFORM_TEX(v.uv, _MainTex);UNITY_TRANSFER_FOG(o,o.vertex);return o;}fixed4 frag (v2f i) : SV_Target{// sample the texturefixed4 col = tex2D(_MainTex, i.uv);// apply fogUNITY_APPLY_FOG(i.fogCoord, col);return fixed4(_Color.rgb,_Alpha);}ENDCG}}
}
6.折线图颜色渐变材质shader
Shader "Unlit/LineChartShader"
{Properties{_MainTex ("Texture", 2D) = "white" {}_Color("Color",Color) = (1,1,1,1)_Pow("Pow",Range(0,2)) = 1_Light("Light",Range(0,0.02)) = 0}SubShader{//Tags { "RenderType"="Transparent"}Pass{ZWrite OffBlend SrcAlpha OneCGPROGRAM#pragma vertex vert#pragma fragment frag#include "UnityCG.cginc"struct appdata{float4 vertex : POSITION;float2 uv : TEXCOORD0;};struct v2f{float2 uv : TEXCOORD0;float4 vertex : SV_POSITION;float2 xy : TEXCOORD2;};sampler2D _MainTex;float4 _MainTex_ST;fixed4 _Color;float _Pow;float _Light;v2f vert (appdata v){v2f o;o.xy = v.vertex.xy;o.vertex = UnityObjectToClipPos(v.vertex);o.uv = TRANSFORM_TEX(v.uv, _MainTex);;return o;}fixed4 frag (v2f i) : SV_Target{return fixed4(_Color.rgb, pow(i.xy.x * i.xy.y * _Light,_Pow) );}ENDCG}}
}