Unity 在代码中利用Mesh实时生成圆环/空心圆柱

    本篇文章主要介绍了利用Unity中的mesh 实时生成圆环的过程以及思想,我会在开头直接放出源码。如若有任何疑问,可向后继续观看详细解说~~~

    (PSPSPS:Mesh生成的顺序,方式等有很多,当前代码中部分逻辑若不理解,可自行推导一下圆的方程,即可得出解答   //顺带说一下,代码中的英文是我锻炼英文用的。。。。遇见语法错误不准笑……)

首先 放上效果图:

效果图

 

代码如下:

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

[RequireComponent(typeof(MeshFilter) , typeof(MeshRenderer))]
public class CylinderGenerateScript : MonoBehaviour {

    //the outside radius must lager than the inside one
    [Range(0, 100)] public float innerRadius;
    [Range(1, 100)] public float outsideRadius;
    public int blockCounts = 80;          //how many blocks that the obj will be split into?
    public float height = 10;             //the height of the obj.


    private float increment;
    private float currentAngle = 0;
    

    private MeshFilter meshFilter;

    void Start () {
        meshFilter = transform.GetComponent<MeshFilter>();

        GenerateMesh();

        //show the order we generate the obj.
        StartCoroutine(SequenceTest());

    }


	private void GenerateMesh()
    {
        //declare all the array we need
        List<Vector3> vertices = new List<Vector3>();
        List<Vector2> uvs = new List<Vector2>();
        List<int> triangles = new List<int>();


        //initialize the parameters
        increment = 2 * Mathf.PI / blockCounts;

        //Generate the vertex we need
        vertices = GenerateVertics();
        //Fill the triangles in order
        triangles = FillTriangles(vertices.Count);

        meshFilter.mesh.vertices = vertices.ToArray();
        meshFilter.mesh.tria
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值