Unity绘制圆、椭圆、多边形

 unity画圆原理是绘制多边形,增加多边形的边数,当边数到达一定的数量后,看起来就是一个光滑的圆

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//该物体需要 LineRenderer组件,也可以手动添加LineRenderer组件,把这个注释掉
[RequireComponent(typeof(LineRenderer))]
public class CirclesDrawer : MonoBehaviour
{
    public int segments;//所用的线条(线条越多,画出来的圆越圆)
    // X轴与Y轴半径相等时绘制的是正圆(正多边形),否则是椭圆;同时也会影响圆的大小
    public float xradius;//X轴 半径
    public float yradius;//Y轴 半径
    public float zradius;
    LineRenderer line;

    void Start()
    {
        line = gameObject.GetComponent<LineRenderer>();
        line.positionCount = (segments + 1);//设置 LineRenderer 组件的画圆线条的数量
        line.useWorldSpace = false;//不使用世界坐标
        CreatePoints();
    }

    void CreatePoints()//创建圆
    {
        float x;
        float y = 0;
        float z;
        float angle = 0;
        for (int i = 0; i < (segments + 1); i++)
        {
            x = Mathf.Sin(Mathf.Deg2Rad * angle) * xradius;
            z = Mathf.Cos(Mathf.Deg2Rad * angle) * zradius;
            y = Mathf.Cos(Mathf.Deg2Rad * angle) * yradius;
            line.SetPosition(i, new Vector3(x, y, z));//设置每个点的坐标

            angle += (360f / segments);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值