Unity 绘制Mesh线条



using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
public struct SegmentPoint
{
public Vector3 normal;
public Vector3 pos;
};
 
public class DrawMesh_Plane : MonoBehaviour {
 
MeshFilter meshFilter;
/// <summary>
/// 面片中的最后一个点
/// </summary>
SegmentPoint lastPoint;
SegmentPoint penultPoint;
 
bool startDraw = false;
bool twoPoint = false;
 
/// <summary>
/// 线条半径
/// </summary>
float r;
/// <summary>
/// 包含meshfilter、meshrenderer、材质球组件的预设体
/// </summary>
public GameObject linePrefab;
public Transform lineParent;
/// <summary>
/// 平滑点数
/// </summary>
public int smooth = 3;
 
[HideInInspector]
public Color lineColor;
 
public VRTracketObjManager vrtom;
public bool usePressure;
 
public DrawMeshManager drawMeshManager;
 
 
void Start()
{
lineColor = drawMeshManager.color;
}
 
 
void Update()
{
 
DrawMeshByCollider();
}
 
 
void DrawMeshVR()
{
var device = SteamVR_Controller.Input((int)vrtom.trackedObj_Left.index);
if (device.GetPress(SteamVR_Controller.ButtonMask.Trigger))
{
if (usePressure)
{
r = device.GetAxis(Valve.VR.EVRButtonId.k_EButton_Axis1).x * drawMeshManager.brushRadius;
}
else
{
r = drawMeshManager.brushRadius;
}
if (!startDraw && !twoPoint)
{
GameObject line = Instantiate(linePrefab);
line.transform.SetParent(lineParent);
meshFilter = line.GetComponent<MeshFilter>();
line.GetComponent<MeshRenderer>().material.SetColor("_Color", lineColor);
if (drawMeshManager.addStep != null)
{
drawMeshManager.addStep(line);
}
}
float dis = Vector3.Distance(vrtom.leftPenPoint.position, lastPoint.pos);
if (!startDraw)
{
lastPoint.pos = vrtom.leftPenPoint.position;
lastPoint.normal = -vrtom.leftPenPoint.up;
startDraw = true;
}
else if (!twoPoint && dis > drawMeshManager.spaceDis)
{
SegmentPoint hitP;
hitP.pos = vrtom.leftPenPoint.position;
hitP.normal = -vrtom.leftPenPoint.up;
meshFilter.mesh = CreateStartMesh(lastPoint, hitP);
penultPoint = lastPoint;
lastPoint = hitP;
twoPoint = true;
}
else if (dis > drawMeshManager.spaceDis)
{
SegmentPoint hitP;
hitP.pos = vrtom.leftPenPoint.position;
hitP.normal = -vrtom.leftPenPoint.up;
SegmentPoint[] newSP = SmoothPoints(lastPoint, penultPoint, hitP);
List<Vector3> nv = new List<Vector3>();
for (int i = 0; i <= newSP.Length - 2; i++)
{
nv.AddRange(ComputeVertex(newSP[i], newSP[i + 1]));
}
penultPoint = newSP[newSP.Length - 2];
lastPoint = hitP;
SetMesh(nv.ToArray());
}
}
if (device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger))
{
startDraw = false;
twoPoint = false;
meshFilter = null;
}
}
 
/// <summary>
/// 贴合模型画线
/// </summary>
void DrawMeshByCollider()
{
r = drawMeshManager.brushRadius;
if (Input.GetMouseButton(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit) && hit.collider.tag == TagManager.Line)
{
if (!startDraw && !twoPoint)
{
GameObject line = Instantiate(linePrefab);
line.transform.SetParent(lineParent);
meshFilter = line.GetComponent<MeshFilter>();
line.GetComponent<MeshRenderer>().material.SetColor("_Color", lineColor);
if (drawMeshManager.addStep != null)
{
drawMeshManager.addStep(line);
}
}
float dis = Vector3.Distance(hit.point, lastPoint.pos);
if (!startDraw) //开始创建第一个点
{
lastPoint.pos = hit.point;
lastPoint.normal = hit.normal;
startDraw = true;
}
else if (!twoPoint && dis > drawMeshManager.spaceDis) //开始创建第一个面片
{
SegmentPoint hitP;
hitP.pos = hit.point;
hitP.normal = hit.normal;
meshFilter.mesh = CreateStartMesh(lastPoint, hitP);
penultPoint = lastPoint;
lastPoint = hitP;
twoPoint = true;
}
else if (dis > drawMeshManager.spaceDis)
{
SegmentPoint hitP;
hitP.pos = hit.point;
hitP.normal = hit.normal;
SegmentPoint[] newSP = SmoothPoints(lastPoint, penultPoint, hitP);
List<Vector3> nv = new List<Vector3>();
for (int i = 0; i <= newSP.Length - 2; i++)
{
nv.AddRange(ComputeVertex(newSP[i], newSP[i + 1]));
}
penultPoint = newSP[newSP.Length - 2];
lastPoint = hitP;
SetMesh(nv.ToArray());
}
}
else
{
startDraw = false;
twoPoint = false;
meshFilter = null;
}
}
if (Input.GetMouseButtonUp(0))
{
startDraw = false;
twoPoint = false;
meshFilter = null;
}
}
 
/// <summary>
/// 创建第一个面片
/// </summary>
/// <param name="startP"></param>
/// <param name="endP"></param>
/// <returns></returns>
Mesh CreateStartMesh(SegmentPoint startP,SegmentPoint endP)
{
Vector3 v1 = endP.pos - startP.pos;
Vector3 d1 = Vector3.Cross(v1.normalized, startP.normal);
Vector3 d2 = Vector3.Cross(v1.normalized, endP.normal);
d1 = d1.normalized;
Vector3 p0, p1, p2, p3;
p0 = startP.pos - d1 * r * 0.5f + startP.normal * drawMeshManager.expand;
p1 = startP.pos + d1 * r * 0.5f + startP.normal * drawMeshManager.expand;
p2 = endP.pos - d2 * r * 0.5f + endP.normal * drawMeshManager.expand;
p3 = endP.pos + d2 * r * 0.5f + endP.normal * drawMeshManager.expand;
Vector3[] newVertices = {p0, p1, p2, p3 };
Vector2[] newUV = { new Vector2(p0.x, p0.y), new Vector2(p1.x, p1.y), new Vector2(p2.x, p2.y), new Vector2(p3.x, p3.y) };
int[] newTriangles = { 0, 1, 2, 1, 3, 2 };
Mesh mesh = new Mesh();
mesh.vertices = newVertices;
mesh.uv = newUV;
mesh.triangles = newTriangles;
return mesh;
}
 
 
/// <summary>
/// 获取原先面片
/// </summary>
/// <param name="vert"></param>
/// <param name="uvs"></param>
/// <param name="tri"></param>
void GetMesh(out List<Vector3> vert,out List<Vector2> uvs,out List<int> tri)
{
vert = new List<Vector3>();
uvs = new List<Vector2>();
tri = new List<int>();
vert.AddRange(meshFilter.mesh.vertices);
uvs.AddRange(meshFilter.mesh.uv);
tri.AddRange(meshFilter.mesh.triangles);
}
 
public void GenerateMesh(LineData ld)
{
GameObject l = Instantiate(linePrefab);
l.AddComponent<MeshFilter>();
l.AddComponent<MeshRenderer>();
l.transform.position = ld.position;
l.transform.eulerAngles = ld.rotation;
Vector3[] newVertices = ld.vertex;
Vector2[] newUV = ld.uv;
int[] newTriangles = ld.triangle;
Mesh mesh = new Mesh();
mesh.vertices = newVertices;
mesh.uv = newUV;
mesh.triangles = newTriangles;
l.GetComponent<MeshFilter>().mesh = mesh;
l.GetComponent<MeshRenderer>().material.color = ld.color;
}
 
/// <summary>
/// 使用贝塞尔平滑线段
/// </summary>
/// <param name="last"></param>
/// <param name="penult"></param>
/// <param name="current"></param>
/// <returns></returns>
SegmentPoint[] SmoothPoints(SegmentPoint last, SegmentPoint penult, SegmentPoint current)
{
float d = 1f / (float)smooth;
 
SegmentPoint[] ps = new SegmentPoint[smooth - 1];
for (int i = 0; i < ps.Length; i++)
{
float t = d * (i + 1);
ps[i].pos = (1 - t) * (1 - t) * penult.pos + 2 * t * (1 - t) * last.pos + t * t * current.pos;
ps[i].normal = Vector3.LerpUnclamped(penult.normal, current.normal, t);
}
List<SegmentPoint> segs = new List<SegmentPoint>();
segs.Add(penult);
segs.AddRange(ps);
segs.Add(current);
return segs.ToArray();
}
 
Vector3[] ComputeVertex(SegmentPoint p1,SegmentPoint p2)
{
Vector3 dir = p2.pos - p1.pos;
 
Vector3 d = Vector3.Cross(dir, p2.normal).normalized;
Vector3[] ps = new Vector3[2];
ps[0] = p2.pos - d * 0.5f * r + p2.normal * drawMeshManager.expand;
ps[1] = p2.pos + d * 0.5f * r + p2.normal * drawMeshManager.expand;
 
return ps;
}
 
/// <summary>
/// 添加mesh
/// </summary>
/// <param name="vetexes"></param>
void SetMesh(Vector3[] vertexes)
{
 
List<Vector3> vert;
List<Vector2> uvs;
List<int> tri;
GetMesh(out vert, out uvs, out tri);
Vector3[] newVert = vertexes;
Vector2[] newUv = new Vector2[vertexes.Length];
for (int i = 0; i < newVert.Length; i++)
{
newUv[i] = newVert[i];
}
 
vert.RemoveAt(vert.Count - 1);
vert.RemoveAt(vert.Count - 1);
vert.AddRange(newVert);
 
uvs.RemoveAt(uvs.Count - 1);
uvs.RemoveAt(uvs.Count - 1);
uvs.AddRange(newUv);
 
int[] newTri = new int[vert.Count * 3 - 6];
for (int i = 0; i < newTri.Length / 6; i++)
{
if (i == 0)
{
newTri[i] = 0;
newTri[i + 1] = 1;
newTri[i + 2] = 2;
newTri[i + 3] = 1;
newTri[i + 4] = 3;
newTri[i + 5] = 2;
}
else
{
newTri[i * 6] = newTri[i * 6 - 6] + 2;
newTri[i * 6 + 1] = newTri[i * 6 - 5] + 2;
newTri[i * 6 + 2] = newTri[i * 6 - 4] + 2;
newTri[i * 6 + 3] = newTri[i * 6 - 3] + 2;
newTri[i * 6 + 4] = newTri[i * 6 - 2] + 2;
newTri[i * 6 + 5] = newTri[i * 6 - 1] + 2;
}
}
Mesh m = new Mesh();
m.SetVertices(vert);
m.uv = uvs.ToArray();
m.triangles = newTri;
meshFilter.mesh.Clear();
meshFilter.mesh = m;
}
}

=====================================================================
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;
using System.Text;
using System.IO;
using UnityEditor;
 
public class DrawMeshManager : MonoBehaviour {
 
/// <summary>
/// 线条半径
/// </summary>
public float brushRadius = 0.01f;
/// <summary>
/// 点之间最小间隔
/// </summary>
public float spaceDis = 0.04f;
/// <summary>
/// 面片向外扩展距离
/// </summary>
public float expand = 0.01f;
public Color color;
public float alpha;
 
public delegate void AddStep(GameObject gameObj);
public AddStep addStep;
 
/// <summary>
/// 最大保存步数
/// </summary>
public int maxCount = 10;
 
public GameObject line;
public GameObject tips;
 
List<GameObject> allLine = new List<GameObject>();
 
LineData[] allLineData ;
StringBuilder stringB;
string filepath;
 
// Use this for initialization
void Start () {
filepath = Application.dataPath + @"/StreamingAssets/json_line.txt";
addStep = new AddStep(AddDrawStep);
}
 
// Update is called once per frame
void Update () {
 
}
 
 
void AddDrawStep(GameObject mesh)
{
if (allLine.Count == maxCount)
{
allLine.RemoveAt(maxCount - allLine.Count);
}
allLine.Add(mesh);
}
 
void Undo()
{
if (Input.GetKeyDown(KeyCode.Z))
{
if (allLine.Count > 0)
{
Destroy(allLine[allLine.Count - 1]);
allLine.RemoveAt(allLine.Count - 1);
}
}
}
 
public void SaveI()
{
SaveAsyn();
}
 
 
void SaveAsyn()
{
allLineData = new LineData[line.transform.childCount];
print(allLineData.Length);
for (int i = 0; i < allLineData.Length; i++)
{
allLineData[i] = new LineData();
allLineData[i].name = line.transform.GetChild(i).name;
allLineData[i].position = line.transform.GetChild(i).position;
allLineData[i].rotation = line.transform.GetChild(i).eulerAngles;
allLineData[i].vertex = line.transform.GetChild(i).GetComponent<MeshFilter>().mesh.vertices;
allLineData[i].uv = line.transform.GetChild(i).GetComponent<MeshFilter>().mesh.uv;
allLineData[i].triangle = line.transform.GetChild(i).GetComponent<MeshFilter>().mesh.triangles;
allLineData[i].color = line.transform.GetChild(i).GetComponent<MeshRenderer>().material.color;
}
 
StringBuilder sb = LineData2Jason(allLineData);
stringB = sb;
 
 
//Loom.RunAsync(()=> { WriteToText(sb,filepath); });
WriteToText(sb, filepath);
//AssetDatabase.Refresh();
 
}
 
void WriteToText(StringBuilder sb,string filepath)
{
FileInfo t = new FileInfo(filepath);
if (!File.Exists(filepath))
{
File.Delete(filepath);
}
 
StreamWriter sw = t.CreateText();
sw.WriteLine(sb.ToString());
sw.Close();
sw.Dispose();
}
 
/// <summary>
/// 所有line转换为json
/// </summary>
/// <param name="ld"></param>
/// <returns></returns>
StringBuilder LineData2Jason(LineData[] ld)
{
StringBuilder sb = new StringBuilder();
JsonWriter writer = new JsonWriter(sb);
writer.WriteObjectStart();
writer.WritePropertyName("allLine");
writer.WriteArrayStart();
foreach (LineData data in ld)
{
writer.WriteObjectStart();
writer.WritePropertyName("name");
writer.Write(data.name);
 
writer.WritePropertyName("position");
writer.WriteArrayStart();
writer.WriteObjectStart();
writer.WritePropertyName("x");
writer.Write(data.position.x.ToString("F5"));
writer.WritePropertyName("y");
writer.Write(data.position.x.ToString("F5"));
writer.WritePropertyName("z");
writer.Write(data.position.x.ToString("F5"));
writer.WriteObjectEnd();
writer.WriteArrayEnd();
 
writer.WritePropertyName("rotation");
writer.WriteArrayStart();
writer.WriteObjectStart();
writer.WritePropertyName("x");
writer.Write(data.rotation.x.ToString("F5"));
writer.WritePropertyName("y");
writer.Write(data.rotation.y.ToString("F5"));
writer.WritePropertyName("z");
writer.Write(data.rotation.z.ToString("F5"));
writer.WriteObjectEnd();
writer.WriteArrayEnd();
 
writer.WritePropertyName("vertex");
writer.WriteArrayStart();
for (int i = 0; i < data.vertex.Length; i++)
{
writer.WriteObjectStart();
writer.WritePropertyName("index");
writer.Write(i.ToString());
 
writer.WritePropertyName("x");
writer.Write(data.vertex[i].x.ToString("F5"));
writer.WritePropertyName("y");
writer.Write(data.vertex[i].y.ToString("F5"));
writer.WritePropertyName("z");
writer.Write(data.vertex[i].z.ToString("F5"));
 
writer.WriteObjectEnd();
}
writer.WriteArrayEnd();
 
writer.WritePropertyName("uv");
writer.WriteArrayStart();
for (int i = 0; i < data.uv.Length; i++)
{
writer.WriteObjectStart();
writer.WritePropertyName("index");
writer.Write(i.ToString());
 
writer.WritePropertyName("x");
writer.Write(data.uv[i].x.ToString("F5"));
writer.WritePropertyName("y");
writer.Write(data.uv[i].y.ToString("F5"));
 
writer.WriteObjectEnd();
}
writer.WriteArrayEnd();
 
writer.WritePropertyName("triangle");
writer.WriteArrayStart();
for (int i = 0; i < data.triangle.Length; i++)
{
writer.WriteObjectStart();
writer.WritePropertyName("index");
writer.Write(i.ToString());
 
writer.WritePropertyName("value");
writer.Write(data.triangle[i].ToString());
 
writer.WriteObjectEnd();
}
writer.WriteArrayEnd();
 
writer.WritePropertyName("color");
writer.WriteArrayStart();
writer.WriteObjectStart();
writer.WritePropertyName("r");
writer.Write(data.color.r.ToString("F5"));
writer.WritePropertyName("g");
writer.Write(data.color.g.ToString("F5"));
writer.WritePropertyName("b");
writer.Write(data.color.b.ToString("F5"));
writer.WritePropertyName("a");
writer.Write(data.color.a.ToString("F5"));
writer.WriteObjectEnd();
writer.WriteArrayEnd();
 
writer.WriteObjectEnd();
}
writer.WriteArrayEnd();
writer.WriteObjectEnd();
return sb;
}
 
 
 
public void Read()
{
LineData[] ld;
 
StreamReader sr = File.OpenText(filepath);
string strLine = sr.ReadToEnd();
JsonData jd = JsonMapper.ToObject(strLine);
JsonData lineArray = jd["allLine"];
 
ld = new LineData[lineArray.Count];
 
for (int i = 0; i < lineArray.Count; i++)
{
ld[i] = new LineData();
JsonData name, px, py, pz, rx, ry, rz, cr, cg, cb, ca;
 
name = lineArray[i]["name"];
 
JsonData position = lineArray[i]["position"];
px = position[0]["x"];
py = position[0]["y"];
pz = position[0]["z"];
 
JsonData rotation = lineArray[i]["rotation"];
rx = rotation[0]["x"];
ry = rotation[0]["y"];
rz = rotation[0]["z"];
 
JsonData color = lineArray[i]["color"];
cr = color[0]["r"];
cg = color[0]["g"];
cb = color[0]["b"];
ca = color[0]["a"];
 
ld[i].name = (string)name;
 
ld[i].position.x = float.Parse((string)px);
ld[i].position.y = float.Parse((string)py);
ld[i].position.z = float.Parse((string)pz);
 
ld[i].rotation.x = float.Parse((string)rx);
ld[i].rotation.y = float.Parse((string)ry);
ld[i].rotation.z = float.Parse((string)rz);
 
ld[i].color.r = float.Parse((string)cr);
ld[i].color.g = float.Parse((string)cg);
ld[i].color.b = float.Parse((string)cb);
ld[i].color.a = float.Parse((string)ca);
 
JsonData vertex = lineArray[i]["vertex"];
ld[i].vertex = new Vector3[vertex.Count];
ld[i].uv = new Vector2[vertex.Count];
for (int j = 0; j < vertex.Count; j++)
{
ld[i].vertex[j] = new Vector3();
JsonData vx, vy, vz;
vx = vertex[j]["x"];
vy = vertex[j]["y"];
vz = vertex[j]["z"];
 
ld[i].uv[j] = new Vector2();
ld[i].vertex[j].x = float.Parse((string)vx);
ld[i].vertex[j].y = float.Parse((string)vy);
ld[i].vertex[j].z = float.Parse((string)vz);
 
ld[i].uv[j] = ld[i].vertex[j];
 
}
 
JsonData triangle = lineArray[i]["triangle"];
ld[i].triangle = new int[triangle.Count];
for (int j = 0; j < triangle.Count; j++)
{
ld[i].triangle[j] = int.Parse((string)triangle[j]["value"]);
}
 
}
CreateMesh(ld);
}
 
 
 
void CreateMesh(LineData[] ld)
{
for (int i = 0; i < ld.Length; i++)
{
//drawMesh.GenerateMesh(ld[i]);
}
}
 
}

原文地址:https://www.cnblogs.com/alps/p/11992997.html

时间: 2024-11-12 19:30:19

Unity 绘制Mesh线条的相关文章

Unity自定义mesh以及编译器

Star 自定义编辑器简易教程 an introduction to custom editors 原文地址 http://catlikecoding.com/unity/tutorials/star/ http://blog.csdn.net/lilanfei/article/details/7680802 简介 Introduction 这个教程将让你学会如何创建一个星型控件以及如何制作这个控件的自定义编辑器.你将学会: 动态的建立Mesh. 使用一个嵌套类. 建立一个自定义编辑器. 使用S

Quartz2D常见图形的绘制:线条、多边形、圆

UI高级 Quartz2D http://ios.itcast.cn  iOS学院 掌握 drawRect:方法的使用 常见图形的绘制:线条.多边形.圆 绘图状态的设置:文字颜色.线宽等 图形上下文状态的保存与恢复(图形上下文栈) 图片裁剪 截图 什么是Quartz2D Quartz 2D是一个二维绘图引擎,同时支持iOS和Mac系统 Quartz 2D能完成的工作 Ø绘制图形 : 线条\三角形\矩形\圆\弧等 Ø绘制文字 Ø绘制\生成图片(图像) Ø读取\生成PDF Ø截图\裁剪图片 Ø自定义

Unity中Mesh分解与边缘高亮加上深度检测

一个比较简单的需求,不过遇到些坑,记录下. 房间有多个模型,每个模型可能多个SubMesh,点击后,需要能具体到是那个SubMesh,并且在这个SubMesh上显示边缘高光,以及能个性这单个SubMesh对应的Material.如一个桌子的Mesh,其实有二个材质,分别对应二个SubMesh,一个桌面和一个桌脚,点击桌面后,只有这个桌面高光,而不是整个桌子,并且能单独更换这个桌面的Material. 我们知道Unity中,Mesh和Ogre一样,也是可以有多个SubMesh,每个SubMesh有

HTML5 SVG简单的动态绘制轮廓线条动画插件

walkway是一款非常简单的HTML5 SVG动态绘制轮廓线条动画插件.它支持SVG的path,line 和 polyline元素.walkway使用起来简单方便,可以制作出非常酷的SVG线条动态绘制效果.更好的动态绘制线条插件可以参考:html5 svg线条动态绘制文字轮廓边框动画.HTML5 SVG图形轮廓线条绘制动画插件-vivus 和 html5 svg线条动态绘制iphone边框动画效果. 在线演示:http://www.htmleaf.com/Demo/201501261282.h

Unity 绘制多边形

最近工程需要用到一个多边形用来查看角色属性,于是就研究了下Mesh用网格做了一个.遗憾的的 UGUI 渲染不了 3D 物体,然后又用了一段时间研究了下UGUI的网格绘制. 不过终于还是完成了,虽然有些瑕疵…… 好吧 有很大的问题 UV 需要自己计算.(如果有朋友精通这一块,希望能帮忙改进一下) 下边是5.2以下版本使用 的 5.2(包括)以上的 请点击这个连接 在Unity中一个Mesh使用3个顶点就能画出来,但是UGUI需要使用四个顶点才行,所以在画一些特殊模型的时候就会产生一些废点(对图形没

unity绘制线和绘制面

绘制线条代码,其实就是指定至少两个点,然后赋予贴图即可,不废话,上代码: using UnityEngine; using System.Collections; public class LineT_1 : MonoBehaviour { private LineRenderer lineRender; private int lineLenght=4; private Vector3 v0 =new Vector3(1.0f,0f,0f); private Vector3 v1= new V

关于Unity中Mesh网格的详解

3D模型 通过3D建模软件所建出来的点和面,如以三角形为主的点和面,比如人的脑袋一个球,就是由各种各样的三角形组成的点和面. 点和面以及纹理坐标都是通过3D建模软件建模出来的. Unity会帮我们把模型的信息存到Mesh里面来,Mesh翻译成中文是网格. 顶点,三角形,纹理坐标,法线和切线. 3D建模软件 1:Autodesk 3D Studio Max 支持mac os windows;2: Autodesk 3D Maya 支持windows3: Cinema4D 支持mac os wind

用 Codea 绘制的线条飞鸟动画代码

使用说明:先在 Codea 内新建一个空白项目,把该文件内容拷贝到 main 标签页内,即可运行 使用了 Codea 提供的音效包(需要在 Codea 中点击下载) 截图: 代码如下: -- main -- Use this function to perform your initial setup function setup() displayMode(FULLSCREEN) x = WIDTH/2 y = HEIGHT/2 img = sprite("Cargo Bot:Starry B

Unity - 绘制正五边形网格

本文简述了Unity中绘制正五边形网格的基本方法:计算顶点信息.设置三角形覆盖信息.创建配置mesh 绘制方法 基本思路:计算出五边形顶点坐标信息作为数组,设置三角形包围方式,再创建新的mesh配置vertices.triangle参数,最终赋值到当前mesh上 项目实现: 创建DrawPentagon.cs,挂在于带有mesh的物体上(本例为Quad 编写代码如下: 查看所创建的mesh信息 public class DrawPentagon : MonoBehaviour { private