首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
其他好文
> 详细
Mesh绘制雷达图(UGUI)
时间:
2016-05-12 16:54:02
阅读:
614
评论:
0
收藏:
0
[点我收藏+]
标签:
/********************************************************************************
参考资料:http://www.cnblogs.com/jeason1997/p/5130413.html
** 类名称: RadarEditor
** 描述:雷达图
** 作者: 田树东
*********************************************************************************/
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
[AddComponentMenu("UI/Extensions/RadarEditor")]
public class RadarEditor : MaskableGraphic
{
public bool isFill = true;
[Range(0, 0.99f)]
public float fillPercent = 0.8f;
[Range(0f, 1f)]
public float[] values;
[Range(0f, 360f)]
public float angleOffset = 0;
public bool useStateLine = true;
public Color lineColor = Color.white;
public float lineWidth = 0.5f;
[Range(0f, 1f)]
public float lineLength = 0.8f;
/// <summary>
/// 重写OnPopulateMesh
/// </summary>
/// <param name="vh"></param>
protected override void OnPopulateMesh(VertexHelper vh)
{
Vector2 size = GetComponent<RectTransform>().rect.size / 2f;
vh.Clear();
int partCount = values.Length;
for (int i = 0; i < partCount; i++)
{
Vector2 pos1 = GetPoint(size, i) * values[i];
Vector2 pos2 = isFill ? Vector2.zero : (pos1 * fillPercent);
Vector2 pos4 = (i + 1 >= partCount) ? (GetPoint(size, 0) * values[0]) : (GetPoint(size, i + 1) * values[i + 1]);
Vector2 pos3 = isFill ? Vector2.zero : (pos4 * fillPercent);
vh.AddUIVertexQuad(GetQuad(pos1, pos2, pos3, pos4));
if (useStateLine)
{
if (i != 0)
{
Vector2 lineEndPos = GetPoint(size, i) * lineLength;
Vector2 lineStartPos = Vector2.zero;
vh.AddUIVertexQuad(GetLine(lineStartPos, lineEndPos));
}
if (i + 1 == partCount)
{
Vector2 lineEndPos = GetPoint(size, 0) * lineLength;
Vector2 lineStartPos = Vector2.zero;
vh.AddUIVertexQuad(GetLine(lineStartPos, lineEndPos));
}
}
}
}
/// <summary>
/// 画线
/// </summary>
/// <param name="start"></param>
/// <param name="end"></param>
/// <returns></returns>
private UIVertex[] GetLine(Vector2 start, Vector2 end)
{
UIVertex[] vs = new UIVertex[4];
Vector2[] uv = new Vector2[4];
uv[0] = new Vector2(0, 0);
uv[1] = new Vector2(0, 1);
uv[2] = new Vector2(1, 0);
uv[3] = new Vector2(1, 1);
Vector2 v1 = end - start;
Vector2 v2 = (v1.y == 0f) ? new Vector2(0f, 1f) : new Vector2(1f, -v1.x / v1.y);
v2.Normalize();
v2 *= lineWidth / 2f;
Vector2[] pos = new Vector2[4];
pos[0] = start + v2;
pos[1] = end + v2;
pos[2] = end - v2;
pos[3] = start - v2;
for (int i = 0; i < 4; i++)
{
UIVertex v = UIVertex.simpleVert;
v.color = lineColor;
v.position = pos[i];
v.uv0 = uv[i];
vs[i] = v;
}
return vs;
}
/// <summary>
/// 获取点Vector2
/// </summary>
/// <param name="size"></param>
/// <param name="i"></param>
/// <returns></returns>
private Vector2 GetPoint(Vector2 size, int i)
{
int partCount = values.Length;
float angle = 360f / partCount * i + angleOffset;
float sin = Mathf.Sin(angle * Mathf.Deg2Rad);
float cos = Mathf.Cos(angle * Mathf.Deg2Rad);
return new Vector2(size.x * cos, size.y * sin);
}
/// <summary>
/// 根据点获取UIVertex
/// </summary>
/// <param name="vertPos"></param>
/// <returns></returns>
private UIVertex[] GetQuad(params Vector2[] vertPos)
{
UIVertex[] vs = new UIVertex[4];
Vector2[] uv = new Vector2[4];
uv[0] = new Vector2(0, 0);
uv[1] = new Vector2(0, 1);
uv[2] = new Vector2(1, 0);
uv[3] = new Vector2(1, 1);
for (int i = 0; i < 4; i++)
{
UIVertex v = UIVertex.simpleVert;
v.color = color;
v.position = vertPos[i];
v.uv0 = uv[i];
vs[i] = v;
}
return vs;
}
}
Mesh绘制雷达图(UGUI)
标签:
原文地址:http://blog.csdn.net/cxihu/article/details/51362468
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
分布式事务
2021-07-29
OpenStack云平台命令行登录账户
2021-07-29
getLastRowNum()与getLastCellNum()/getPhysicalNumberOfRows()与getPhysicalNumberOfCells()
2021-07-29
【K8s概念】CSI 卷克隆
2021-07-29
vue3.0使用ant-design-vue进行按需加载原来这么简单
2021-07-29
stack栈
2021-07-29
抽奖动画 - 大转盘抽奖
2021-07-29
PPT写作技巧
2021-07-29
003-核心技术-IO模型-NIO-基于NIO群聊示例
2021-07-29
Bootstrap组件2
2021-07-29
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!