UGUI繪制動(dòng)態(tài)曲線
本文實(shí)例為大家分享了UGUI繪制動(dòng)態(tài)曲線的具體代碼,供大家參考,具體內(nèi)容如下
前言
等有空再補(bǔ)詳細(xì)說(shuō)明,先上代碼。看官自行閱讀
代碼
UICurveData 類(lèi),用于存放點(diǎn)數(shù)據(jù)的基礎(chǔ)結(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 負(fù)責(zé)構(gòu)建頂點(diǎn)數(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
}
測(cè)試使用
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 上,運(yùn)行會(huì)看到

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
BootStrap mvcpager分頁(yè)樣式(get請(qǐng)求,刷新頁(yè)面)
這篇文章主要介紹了BootStrap mvcpager分頁(yè)樣式(get請(qǐng)求,刷新頁(yè)面)的相關(guān)資料,通過(guò)引入相關(guān)文件,實(shí)現(xiàn)此功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
C#中使用Dapper進(jìn)行數(shù)據(jù)庫(kù)訪問(wèn)的流程步驟
在C#中,Dapper是一個(gè)非常流行的ORM(對(duì)象關(guān)系映射)工具,它提供了一個(gè)輕量級(jí)的方式來(lái)訪問(wèn)數(shù)據(jù)庫(kù),Dapper通過(guò)SQL語(yǔ)句與數(shù)據(jù)庫(kù)進(jìn)行交互,并將結(jié)果映射到.NET對(duì)象中,以下是如何在C#中使用Dapper進(jìn)行數(shù)據(jù)庫(kù)訪問(wèn)的基本步驟,需要的朋友可以參考下2024-12-12
C#對(duì)INI文件進(jìn)行讀寫(xiě)操作的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#對(duì)INI文件進(jìn)行讀寫(xiě)操作的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,有需要的小伙伴可以參考一下2024-02-02
淺析C#中的Main(String[] args)參數(shù)輸入問(wèn)題
本篇文章主要是對(duì)C#中的Main(String[] args)參數(shù)輸入問(wèn)題進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01
C# OCR實(shí)現(xiàn)文字識(shí)別功能
OCR,中文叫做光學(xué)字符識(shí)別。它是利用光學(xué)技術(shù)和計(jì)算機(jī)技術(shù)把印在或?qū)懺诩埳系奈淖肿x取出來(lái),并轉(zhuǎn)換成一種計(jì)算機(jī)能夠接受、人又可以理解的格式。本文將利用OCR實(shí)現(xiàn)文字識(shí)別功能,感興趣的可以了解一下2022-11-11
c# SQLHelper(for winForm)實(shí)現(xiàn)代碼
數(shù)據(jù)連接池c# SQLHelper 實(shí)現(xiàn)代碼2009-02-02

