UGUI绘制动态曲线

所属分类: 软件编程 / C#教程 阅读数: 130
收藏 0 赞 0 分享

本文实例为大家分享了UGUI绘制动态曲线的具体代码,供大家参考,具体内容如下

前言

等有空再补详细说明,先上代码。看官自行阅读

代码

UICurveData 类,用于存放点数据的基础结构。

public class UICurveData
{
 #region [Fields]
 public List<Vector2> Postion = new List<Vector2>();
 public Color Ccolor;
 public float Thickness = 1;
 #endregion

 #region [PublicTools]
 public void Addpos(float varX, float varY)
 {
  Addpos(new Vector2(varX, varY));
 }
 public void Addpos(Vector2 varV2)
 {
  Postion.Add(varV2);
 }
 #endregion

}

UICurve 负责构建顶点数据,mesh。

public class UICurve : MaskableGraphic
{
 #region [Fields]
 private Dictionary<int, UICurveData> mCurveData = new Dictionary<int, UICurveData>();
 #endregion

 #region [Inherit]
 protected override void OnPopulateMesh(VertexHelper varVerHeler)
 {
  varVerHeler.Clear();

  foreach (var tempKvp in mCurveData)
  {
   var tempUICurveData = tempKvp.Value;
   if (tempUICurveData.Postion.Count < 2)
   {
    continue;
   }
   for (int i = 1; i < tempUICurveData.Postion.Count; i++)
   {
    UIVertex[] verts = new UIVertex[4];

    float x1 = tempUICurveData.Postion[i - 1].x;
    float y1 = tempUICurveData.Postion[i - 1].y;
    float x2 = tempUICurveData.Postion[i].x;
    float y2 = tempUICurveData.Postion[i].y;

    float xd = (y2 - y1) / Mathf.Sqrt(Mathf.Pow(x2 - x1, 2) * Mathf.Pow(y2 - y1, 2)) * tempKvp.Value.Thickness / 2;
    float yd = (x2 - x1) / Mathf.Sqrt(Mathf.Pow(x2 - x1, 2) * Mathf.Pow(y2 - y1, 2)) * tempKvp.Value.Thickness / 2;

    int idx = 0;
    verts[idx].position = new Vector3(tempUICurveData.Postion[i - 1].x - xd, tempUICurveData.Postion[i - 1].y + yd);
    verts[idx].color = tempUICurveData.Ccolor;
    verts[idx].uv0 = Vector2.zero;

    idx++;
    verts[idx].position = new Vector3(tempUICurveData.Postion[i].x - xd, tempUICurveData.Postion[i].y + yd);
    verts[idx].color = tempUICurveData.Ccolor;
    verts[idx].uv0 = Vector2.zero;

    idx++;
    verts[idx].position = new Vector3(tempUICurveData.Postion[i].x + xd, tempUICurveData.Postion[i].y - yd);
    verts[idx].color = tempUICurveData.Ccolor;
    verts[idx].uv0 = Vector2.zero;

    idx++;
    verts[idx].position = new Vector3(tempUICurveData.Postion[i - 1].x + xd, tempUICurveData.Postion[i - 1].y - yd);
    verts[idx].color = tempUICurveData.Ccolor;
    verts[idx].uv0 = Vector2.zero;

    varVerHeler.AddUIVertexQuad(verts);
   }
  }

 }
 #endregion

 #region [PublicTools]
 public void AddCurveData(int varID, UICurveData varCurveData)
 {
  mCurveData.Add(varID, varCurveData);
  SetAllDirty();
 }
 public void Clear()
 {
  mCurveData.Clear();
  SetAllDirty();
 }
 public void RemovePointIDs(params int[] varRemovepoints)
 {
  List<int> tempL = new List<int>();
  tempL.AddRange(varRemovepoints);
  RemovePointIDs(tempL);
 }
 public void RemovePointIDs(List<int> varRemovePoints)
 {
  foreach (var i in varRemovePoints)
  {
   if (!mCurveData.ContainsKey(i)) continue;
   mCurveData.Remove(i);
  }
  SetAllDirty();
 }
 #endregion
}

测试使用

public class TestCurve : MonoBehaviour
{
 void Start()
 {
  var tempCurve = this.gameObject.AddComponent<UICurve>();
  UICurveData tempcd = new UICurveData();
  tempcd.Ccolor = Color.yellow;
  tempcd.Thickness = 2;
  for (int i = 0; i < 360; i++)
  {
   tempcd.Addpos(i * 2,(float)Mathf.Cos(i));
  }
  tempCurve.AddCurveData(1,tempcd);
 }
}

将该脚本挂在 Canvas 上,运行会看到

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

c#开发word批量转pdf源码分享

已经安装有Office环境,借助一些简单的代码即可实现批量Word转PDF,看下面的实例源码吧
收藏 0 赞 0 分享

c# xml API操作的小例子

这篇文章主要介绍了c# xml API操作的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

c#唯一值渲染实例代码

这篇文章主要介绍了c#唯一值渲染实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

淘宝IP地址库采集器c#代码

这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C#在后台运行操作(BackgroundWorker用法)示例分享

BackgroundWorker类允许在单独的专用线程上运行操作。如果需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用BackgroundWorker类方便地解决问题,下面看示例
收藏 0 赞 0 分享

c#文本加密程序代码示例

这是一个加密软件,但只限于文本加密,加了窗口控件的滑动效果,详细看下面的代码
收藏 0 赞 0 分享

c#生成站点地图(SiteMapPath)文件示例程序

这篇文章主要介绍了c#生成站点地图(SiteMapPath)文件的示例,大家参考使用
收藏 0 赞 0 分享

C# 键盘Enter键取代Tab键实现代码

这篇文章主要介绍了C# 键盘Enter键取代Tab键实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm导出Excel方法介绍

在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式
收藏 0 赞 0 分享

C#串口通信程序实例详解

在.NET平台下创建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空间是System.IO.Ports,创建C#串口通信程序的具体实现是如何的呢?让我们开始吧
收藏 0 赞 0 分享
查看更多