欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

UGUI绘制动态曲线

程序员文章站 2022-06-21 20:31:31
本文实例为大家分享了ugui绘制动态曲线的具体代码,供大家参考,具体内容如下 前言 等有空再补详细说明,先上代码。看官自行阅读 代码 uicurvedata 类,用...

本文实例为大家分享了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 上,运行会看到

UGUI绘制动态曲线

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