forked from fholm/unityassets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DumpMesh.cs
52 lines (40 loc) · 1.32 KB
/
DumpMesh.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
public static class DumpMesh {
[MenuItem("Assets/Create/Dump Mesh To C#")]
public static void Dump() {
if (Selection.activeObject is Mesh) {
Dump(Selection.activeObject as Mesh);
}
}
public static void Dump(Mesh original) {
Mesh m = new Mesh();
m.vertices = original.vertices;
m.triangles = original.triangles;
m.Optimize();
var path = Path.Combine("Assets", m.name + "_Mesh.cs");
var sb = new StringBuilder();
var v = m.vertices;
var t = m.triangles;
sb.AppendLine("using UnityEngine;");
// BEGIN CLASS
sb.AppendLine(string.Format("public static class {0}_Mesh {{", m.name));
// VERTICES
sb.AppendLine(string.Format("public static readonly Vector3[] Vertices = new Vector3[{0}] {{", v.Length));
for (int i = 0; i < v.Length; ++i) {
sb.AppendLine(string.Format("new Vector3({0}f, {1}f, {2}f), ", v[i].x, v[i].y, v[i].z));
}
sb.AppendLine("};");
// TRIANGLES
sb.AppendLine(string.Format("public static readonly int[] Triangles = new int[{0}] {{", t.Length));
for (int i = 0; i < t.Length; ++i) {
sb.AppendLine(t[i].ToString() + ", ");
}
sb.AppendLine("};");
// END CLASS
sb.AppendLine("}");
File.WriteAllText(path, sb.ToString());
}
}