UGUI繪制動(dòng)態(tài)曲線
本文實(shí)例為大家分享了UGUI繪制動(dòng)態(tài)曲線的具體代碼,供大家參考,具體內(nèi)容如下
前言
等有空再補(bǔ)詳細(xì)說(shuō)明,先上代碼??垂僮孕虚喿x
代碼
UICurveData 類,用于存放點(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)文章
C#中實(shí)現(xiàn)插入、刪除Excel分頁(yè)符的方法
這篇文章主要給大家介紹了關(guān)于在C#中實(shí)現(xiàn)插入、刪除Excel分頁(yè)符的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05C# Winform程序?qū)崿F(xiàn)防止多開(kāi)的方法總結(jié)【親測(cè)】
這篇文章主要介紹了C# Winform程序?qū)崿F(xiàn)防止多開(kāi)的方法,結(jié)合實(shí)例形式總結(jié)分析了C# Winform防止多開(kāi)相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2020-03-03C#實(shí)現(xiàn)毫秒轉(zhuǎn)換成時(shí)分秒的方法
這篇文章主要介紹了C#實(shí)現(xiàn)毫秒轉(zhuǎn)換成時(shí)分秒的方法,涉及C#時(shí)間的操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03C#實(shí)現(xiàn)將json轉(zhuǎn)換為DataTable的方法
這篇文章主要介紹了C#實(shí)現(xiàn)將json轉(zhuǎn)換為DataTable的方法,涉及C#操作json及DataTable的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03使用C#給PDF文檔添加注釋的實(shí)現(xiàn)代碼
本文將實(shí)例講述C#中如何使用免費(fèi)組件給PDF文檔添加文本注釋,包括自由文本注釋。自由文本注釋能允許我們自定義它的風(fēng)格和外觀,非常具有實(shí)用價(jià)值2017-01-01C# BackgroundWorker組件學(xué)習(xí)入門(mén)介紹
一個(gè)程序中需要進(jìn)行大量的運(yùn)算,并且需要在運(yùn)算過(guò)程中支持用戶一定的交互,為了獲得更好的用戶體驗(yàn),使用BackgroundWorker來(lái)完成這一功能2013-10-10WCF實(shí)現(xiàn)進(jìn)程間管道通信Demo分享
下面小編就為大家分享一篇WCF實(shí)現(xiàn)進(jìn)程間管道通信Demo,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12詳解C#如何實(shí)現(xiàn)一個(gè)安全的事件訂閱器
事件訂閱器是一個(gè)對(duì)象,它訂閱(或監(jiān)聽(tīng))某個(gè)事件,并在事件發(fā)生時(shí)執(zhí)行相應(yīng)的操作,本文主要介紹了C#實(shí)現(xiàn)一個(gè)安全的事件訂閱器的相關(guān)知識(shí),感興趣的可以了解下2024-01-01