【Unity3d】如何绘制椭圆
发表于2017-08-28
如果项目需要画圆,肯定很多人知道怎么实现,因为用到公式比较简单,但是如果在Unity3D的开发中要去绘制椭圆呢,可能有些人就懵了,其实同画圆一样,运用合理函数方程式即可实现绘制椭圆的操作。
代码如下:
/// <summary> /// 首先绘制椭圆的公式 /// 椭圆的参数方程x=acosθ,y=bsinθ; /// </summary> public Transform trans; public float w;//椭圆长 public float h; //椭圆高 public int angle = 360; [Range(0,360)] public int speed = 0; private Vector3[] vec; private int index = 0; private LineRenderer line; float x, y; void Start() { vec = new Vector3[angle]; for (int i = 0; i < angle; i++) { // Mathf.Deg2Rad 单位角度的弧 相当于 1° 的弧度 x = w * Mathf.Cos(i * Mathf.Deg2Rad); y = h * Mathf.Sin(i * Mathf.Deg2Rad); vec [i] = trans.position + new Vector3 (x,0,y); } SetLine (); } void SetLine() { line = gameObject.AddComponent<LineRenderer> (); //设置线由多少个点构成 line.SetVertexCount (angle); //绘制点的坐标 line.SetPositions (vec); } void Update() { trans.position = vec [index]; if ((index += speed) >= vec.Length) { index = 0; } }