UGUI繪制動態(tài)曲線
更新時間:2019年08月21日 15:52:57 作者:Admin_Jhon
這篇文章主要為大家詳細介紹了UGUI繪制動態(tài)曲線的具體方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了UGUI繪制動態(tài)曲線的具體代碼,供大家參考,具體內(nèi)容如下
前言
等有空再補詳細說明,先上代碼??垂僮孕虚喿x
代碼
UICurveData 類,用于存放點數(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 負責構(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
C# Winform程序?qū)崿F(xiàn)防止多開的方法總結(jié)【親測】
這篇文章主要介紹了C# Winform程序?qū)崿F(xiàn)防止多開的方法,結(jié)合實例形式總結(jié)分析了C# Winform防止多開相關(guān)操作技巧與使用注意事項,需要的朋友可以參考下2020-03-03C#實現(xiàn)將json轉(zhuǎn)換為DataTable的方法
這篇文章主要介紹了C#實現(xiàn)將json轉(zhuǎn)換為DataTable的方法,涉及C#操作json及DataTable的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03