Unity動態建立的Mesh,匯出為Obj模型檔案,並生成Prefab檔案
阿新 • • 發佈:2018-12-26
Unity執行時,動態建立的Mesh掛載到MeshFilter元件上,並不能儲存到本地Prefab檔案裡。在執行的場景裡,拖拽正確配置的MeshFilter物件到Unity資源管理器。生成的Prefab檔案,裡面的Mesh物件會missing。所以,我們需要在執行狀態,匯出Mesh到本地生成一個obj模型檔案。
原理,就是根據obj檔案的屬性,把執行時Mesh的頂點,索引,貼圖資料轉化為固定格式流寫入檔案,生成obj模型檔案。
- privatestring MeshToString(MeshFilter mf, Vector3 scale)
- {
-
Mesh mesh = mf.mesh;
- Material[] sharedMaterials = mf.GetComponent<Renderer>().sharedMaterials;
- Vector2 textureOffset = mf.GetComponent<Renderer>().material.GetTextureOffset("_MainTex");
-
Vector2 textureScale = mf.GetComponent<Renderer>().material.GetTextureScale ("_MainTex"
- StringBuilder stringBuilder = new StringBuilder().Append("mtllib design.mtl")
- .Append("\n")
- .Append("g ")
- .Append(mf.name)
- .Append("\n");
- Vector3[] vertices = mesh.vertices;
- for (int i = 0; i < vertices.Length; i++)
- {
-
Vector3 vector = vertices[i];
- stringBuilder.Append(string.Format("v {0} {1} {2}\n", vector.x * scale.x, vector.y * scale.y, vector.z * scale.z));
- }
- stringBuilder.Append("\n");
- Dictionary<int, int> dictionary = new Dictionary<int, int>();
- if (mesh.subMeshCount > 1)
- {
- int[] triangles = mesh.GetTriangles(1);
- for (int j = 0; j < triangles.Length; j += 3)
- {
- if (!dictionary.ContainsKey(triangles[j]))
- {
- dictionary.Add(triangles[j], 1);
- }
- if (!dictionary.ContainsKey(triangles[j + 1]))
- {
- dictionary.Add(triangles[j + 1], 1);
- }
- if (!dictionary.ContainsKey(triangles[j + 2]))
- {
- dictionary.Add(triangles[j + 2], 1);
- }
- }
- }
- for (int num = 0; num != mesh.uv.Length; num++)
- {
- Vector2 vector2 = Vector2.Scale(mesh.uv[num], textureScale) + textureOffset;
- if (dictionary.ContainsKey(num))
- {
- stringBuilder.Append(string.Format("vt {0} {1}\n", mesh.uv[num].x, mesh.uv[num].y));
- }
- else
- {
- stringBuilder.Append(string.Format("vt {0} {1}\n", vector2.x, vector2.y));
- }
- }
- for (int k = 0; k < mesh.subMeshCount; k++)
- {
- stringBuilder.Append("\n");
- if (k == 0)
- {
- stringBuilder.Append("usemtl ").Append("Material_design").Append("\n");
- }
- if (k == 1)
- {
- stringBuilder.Append("usemtl ").Append("Material_logo").Append("\n");
- }
- int[] triangles2 = mesh.GetTriangles(k);
- for (int l = 0; l < triangles2.Length; l += 3)
- {
- stringBuilder.Append(string.Format("f {0}/{0} {1}/{1} {2}/{2}\n", triangles2[l] + 1, triangles2[l + 2] + 1, triangles2[l + 1] + 1));
- }
- }
- return stringBuilder.ToString();
- }
- using (StreamWriter streamWriter = new StreamWriter(string.Format("{0}{1}.obj", datPath, this.meshGO.name)))
- {
- streamWriter.Write(MeshToString(mf, new Vector3(-1f, 1f, 1f)));
- streamWriter.Close();
- }
- AssetDatabase.Refresh();
第二部分,生成了obj模型檔案以後,我們可以通過這個檔案載入一個Mesh物件。動態生成一個Prefab到本地,把obj模型檔案中的Mesh物件賦值給它,成為一個正確載入Mesh的Prefab。
- // create prefab
- Mesh mesh = AssetDatabase.LoadAssetAtPath<Mesh>(string.Format("{0}{1}.obj", projectPath, this.meshGO.name));
- mf.mesh = mesh;
- PrefabUtility.CreatePrefab(string.Format("{0}{1}.prefab", projectPath, this.meshGO.name), this.meshGO);
- AssetDatabase.Refresh();
主要通過,AssetDatabase.LoadAssetAtPath的泛型方法來記載obj模型檔案裡的Mesh物件。然後動態建立一個Prefab這裡需要注意Refresh一下,才能正確儲存Mesh物件到Prefab上。