UGUI繪制動態(tài)曲線
本文實例為大家分享了UGUI繪制動態(tài)曲線的具體代碼,供大家參考,具體內(nèi)容如下
前言
等有空再補詳細說明,先上代碼。看官自行閱讀
代碼
UICurveData 類,用于存放點數(shù)據(jù)的基礎結(jié)構(gòu)。
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 負責構(gòu)建頂點數(shù)據(jù),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 上,運行會看到
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持我們。
上一篇:C#中OpenCvSharp 通過特征點匹配圖片的方法
欄 目:C#教程
下一篇:C# http系列之以form-data方式上傳多個文件及鍵值對集合到遠程服務器
本文標題:UGUI繪制動態(tài)曲線
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/4673.html
您可能感興趣的文章
- 01-10WinForm繪制圓角的方法
- 01-10C#動態(tài)創(chuàng)建button的方法
- 01-10C#動態(tài)創(chuàng)建Access數(shù)據(jù)庫及密碼的方法
- 01-10C#獲取動態(tài)生成的CheckBox值
- 01-10C#中DataGridView動態(tài)添加行及添加列的方法
- 01-10C#繪制曲線圖的方法
- 01-10輕松學習C#的ArrayList類
- 01-10基于C#實現(xiàn)12306的動態(tài)驗證碼變成靜態(tài)驗證碼的方法
- 01-10C#利用GDI繪制常見圖形和文字
- 01-10C#如何動態(tài)設置屏幕分辨率


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-10C#中split用法實例總結(jié)
- 08-05織夢dedecms什么時候用欄目交叉功能?


