Hi,
I am trying to load a mesh from a text file that contains the vertices and triangles, but when applying the values to the mesh filter, the mesh becomes blocky. The object is saved after the changed and then can be loaded. The first image below is the original object before it is loaded and the second is after it is loaded. I also have included the save and load C# script.
Thanks
using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
public class save : MonoBehaviour {
public GameObject planet;
void Start()
{
}
public void Save()
{
System.IO.StreamWriter file = new System.IO.StreamWriter(Application.dataPath + @"\pl128.txt");
for(int i = 0; i < planet.GetComponent<MeshFilter>().mesh.vertices.Length; i++)
{
file.WriteLine(planet.GetComponent<MeshFilter>().mesh.vertices*);*
-
}*
-
for(int i = 0; i < planet.GetComponent<MeshFilter>().mesh.uv.Length; i++)*
-
{*
_ file.WriteLine(planet.GetComponent().mesh.uv*);_
_ }_
_ for(int i = 0; i < planet.GetComponent().mesh.triangles.Length; i++)_
_ {_
_ file.WriteLine(planet.GetComponent().mesh.triangles);
}*_
* file.Close();*
* }*
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class loadPlanet : MonoBehaviour
{
* public GameObject planet;*
* public List triangles = new List();*
* public List vert = new List();*
* public List uv = new List();*
* public int counter = 0;*
* void Start()*
* {*
* }*
* public void Load()*
* {*
* string curline;*
* if(File.Exists(Application.dataPath + @“\pl128.txt”))*
* {*
* System.IO.StreamReader file = new System.IO.StreamReader(Application.dataPath + @“\pl128.txt”);*
* while((curline = file.ReadLine()) != null)*
* {*
* counter = 0;*
* char[] temp;*
* for(int i = 0; i < curline.Length; i++)*
* {*
* temp = curline.ToCharArray();*
if(temp == ‘,’ || temp == ‘(’ || temp == ‘)’)
* {*
* counter++;*
* }*
* }*
* if(counter == 4)*
* {*
* ReadVert(curline);*
* }*
* if(counter == 3)*
* {*
* ReadUV(curline);*
* }*
* if(counter == 0)*
* {*
* ReadTri(curline);*
* }*
* }*
* file.Close();*
* planet.GetComponent().mesh.vertices = vert.ToArray();*
* planet.GetComponent().mesh.uv = uv.ToArray();*
* planet.GetComponent().mesh.triangles = triangles.ToArray();*
* planet.GetComponent().mesh.RecalculateNormals();*
* planet.GetComponent().mesh.RecalculateBounds();*
* planet.GetComponent().sharedMesh = null;*
* planet.GetComponent().sharedMesh = planet.GetComponent().mesh;*
* }*
* else*
* {*
* }*
* }*
* public void ReadVert(string s)*
* {*
* float tempx;*
* float tempy;*
* float tempz;*
* var charsToRemove = new string[] { “@”, “,”, “;”, “'”, “(”, “)”};*
* foreach (var c in charsToRemove)*
* {*
* s = s.Replace(c, string.Empty);*
* }*
* var commands = s.Split (’ ');*
* tempx = float.Parse(commands[0]);*
* tempy = float.Parse(commands[1]);*
* tempz = float.Parse(commands[2]);*
* vert.Add (new Vector3(tempx,tempy,tempz));*
* }*
* public void ReadUV(string s)*
* {*
* float tempx;*
* float tempy;*
* var charsToRemove = new string[] { “@”, “,”, “;”, “'”, “(”, “)”};*
* foreach (var c in charsToRemove)*
* {*
* s = s.Replace(c, string.Empty);*
* }*
* var commands = s.Split (’ ');*
* tempx = float.Parse(commands[0]);*
* tempy = float.Parse(commands[1]);*
* uv.Add (new Vector2(tempx,tempy));*
* }*
* public void ReadTri(string s)*
* {*
* triangles.Add(int.Parse(s));*
* }*
}