UGUI绘制多点连续的平滑曲线
本文实例为大家分享了UGUI绘制平滑曲线的具体代码,供大家参考,具体内容如下
绘制
实现自定义的MaskableGraphic挂载在UGUI的UI节点上
publicclassUGUIObjectRender:MaskableGraphic { /** *points为需要穿过的点 *segments为曲线细分度 *linewidth为曲线粗细 */ protectedoverridevoidOnPopulateMesh(VertexHelpervh) { vh.DrawBeziers(points,segments,linewidth); } }
需要时用的工具类在后面
二次贝塞尔工具类
usingUnityEngine; namespaceViVi.UIExtensions { /** *Createdbyvivion16/5/11. */ publicclassBezierUtils{ publicfloatP0x; publicfloatP1x; publicfloatP2x; publicfloatP3x; publicfloatP0y; publicfloatP1y; publicfloatP2y; publicfloatP3y; publicBezierUtils(floatp0x,floatp0y,floatp1x,floatp1y,floatp2x,floatp2y,floatp3x,floatp3y){ P0x=p0x; P0y=p0y; P1x=p1x; P1y=p1y; P2x=p2x; P2y=p2y; P3x=p3x; P3y=p3y; } publicBezierUtils(Vector2p0,Vector2p1,Vector2p2,Vector2p3){ P0x=p0.x; P0y=p0.y; P1x=p1.x; P1y=p1.y; P2x=p2.x; P2y=p2.y; P3x=p3.x; P3y=p3.y; } publicfloatbeze_x(floatt){ floatit=1-t; returnit*it*it*P0x+3*it*it*t*P1x+3*it*t*t*P2x+t*t*t*P3x; } publicfloatbeze_y(floatt){ floatit=1-t; returnit*it*it*P0y+3*it*it*t*P1y+3*it*t*t*P2y+t*t*t*P3y; } //------------------------------------------------------------------------------------- publicfloatbeze_speed_x(floatt) { floatit=1-t; return-3*P0x*it*it+3*P1x*it*it-6*P1x*it*t+6*P2x*it*t-3*P2x*t*t+3*P3x*t*t; } publicfloatbeze_speed_y(floatt) { floatit=1-t; return-3*P0y*it*it+3*P1y*it*it-6*P1y*it*t+6*P2y*it*t-3*P2y*t*t+3*P3y*t*t; } privatefloatbeze_speed(floatt) { floatsx=beze_speed_x(t); floatsy=beze_speed_y(t); return(float)Mathf.Sqrt(Mathf.Pow(sx,2)+Mathf.Pow(sy,2)); } //------------------------------------------------------------------------------------- privatefloatbeze_length(floatt) { floatLATESTCOUNT=10000; intsteps=(int)Mathf.Ceil(LATESTCOUNT*t); if(steps==0) return0; if(steps%2!=0) steps++; floathalfSteps=steps*.5f; floatnSum=0; floatn1Sum=0; floatdisStep=t/steps; for(inti=0;iUGUI绘图工具类
usingSystem.Collections.Generic; usingUnityEngine; usingUnityEngine.UI; namespaceViVi.UIExtensions { publicstaticclassPolygonHelper { publicstaticvoidAddUIVertexTriangles(thisVertexHelpervh,UIVertex[]verts) { for(inti=0;ivertices,floatx,floaty,Colorcolor) { UIVertexv=UIVertex.simpleVert; v.color=color; v.position=newVector2(x,y); v.uv0=Vector2.zero; } //画圆形 publicstaticvoidcircle(thisVertexHelpervh,Vector2pos,floatradius,Colorcolor,boolfilled=false){ circle(vh,pos.x,pos.y,radius,Mathf.Max(1,(int)(6*Mathf.Pow(radius,1/3f))),color,filled); } publicstaticvoidcircle(thisVertexHelpervh,floatx,floaty,floatradius,Colorcolor,boolfilled=false){ circle(vh,x,y,radius,Mathf.Max(1,(int)(6*Mathf.Pow(radius,1/3f))),color,filled); } publicstaticvoidcircle(thisVertexHelpervh,floatx,floaty,floatradius,intsegments,Colorcolor,boolfilled=false){ if(segments>0){ floatangle=2*Mathf.PI/segments; floatcos=Mathf.Cos(angle); floatsin=Mathf.Sin(angle); floatcx=radius,cy=0; List vs=newList (); segments--; for(inti=0;i points,floatsegment,floatwidth) { List beziers=CreateBeziers(points); if(beziers!=null){ for(inti=0;i =0){ vec.x=-vec.y; vec.y=x; }else{ vec.x=vec.y; vec.y=-x; } returnvec; } publicstaticVector2cpy(thisVector2vec){ returnnewVector2(vec.x,vec.y); } privatestaticvoidDrawBezier(VertexHelpervh,BezierUtilsbezier,floatsegment,floatwidth) { List lpos=newList (); List rpos=newList (); for(inti=0;i<=segment;i++) { Vector2bezierPos=newVector2(bezier.beze_x((float)i/(float)segment),bezier.beze_y((float)i/(float)segment)); Vector2bezierSpeed=newVector2(bezier.beze_speed_x((float)i/(float)segment),bezier.beze_speed_y((float)i/(float)segment)); Vector2offseta=bezierSpeed.normalized.rotate90(1)*(0.5f*width); Vector2offsetb=bezierSpeed.normalized.rotate90(-1)*(0.5f*width); lpos.Add(bezierPos.cpy()+offseta); rpos.Add(bezierPos.cpy()+offsetb); } for(intj=0;j CreateBeziers(List points) { floatscale=0.6f; List beziers=newList (); intoriginCount=points.Count-1; List midpoints=newList (); for(inti=0;i ctrlPoints=newList (); floatoffsetx; floatoffsety; ctrlPoints.Add(newVector2( points[0].x, points[0].y )); for(inti=0;i 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。