栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

UGUI绘制动态曲线

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

UGUI绘制动态曲线

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

前言

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

代码

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

public class UICurveData
{
 #region [Fields]
 public List Postion = new List();
 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 mCurveData = new Dictionary();
 #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 tempL = new List();
  tempL.AddRange(varRemovepoints);
  RemovePointIDs(tempL);
 }
 public void RemovePointIDs(List 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();
  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 上,运行会看到

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/122420.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号