本篇文章主要介绍了利用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