平时我们都是在Unity的软件里新增Cube、Sphere等三维模型,但是很不灵活,形状也比较单一,下面我们用C#代码来实现:
把下面的C# script附着到主摄像头就行
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mesh10 : MonoBehaviour {
private GameObject simpleMesh;
//法线
Mesh mesh;
public bool sophisticated = false; //圆滑曲面
public bool star = true; //星形
public int line = 5; //边数
public float[] maxsize = { 0, 0, 5, 0, 0 }; //外角大小
public float[] minSize = { 0, 0, 1, 0, 0 }; //内角大小
public float high = 1; //高度
public float low = -1; //低度
private Vector3[] vs; //顶点坐标
private int[] ts; //顶点序列
private Vector2[] newUVs; //UV贴图
private Vector3[] newNormals;
// Use this for initialization
void Start () {
simpleMesh = new GameObject();
// 定义对象名为 Start5
simpleMesh.name = "Start5";
simpleMesh.transform.position = new Vector3(688, -30, -645);
// 添加MeshFilter
simpleMesh.AddComponent<MeshFilter>();
// 添加MeshRenderer
simpleMesh.AddComponent<MeshRenderer>();
// 创建shader为Unlit/shader7的Material
Material simpleMaterial = new Material(Shader.Find("Unlit/shader7"));
simpleMesh.GetComponent<MeshRenderer>().material = simpleMaterial;
}
void setMesh()
{
line = Mathf.Clamp(line, 2, 300);
high = Mathf.Clamp(high, low, high);
low &