Need Help combining cubes and exporting
I am at wits end....
The project is to read a data file and place cubes. It then should combine all the cubes into one big mesh and export it to an .obj file.
I can read the file. I can place the cubes. However, combining them and exporting them does not work. I went through just about every post on here related to these two issues I could find. I copy and pasted the code, tried playing with it tried fooling it.... Still can not get the last two pieces of the puzzle.
Here is the code....
using UnityEngine;
using System.Collections;
using System.Text;
using System.IO;
using System.Collections.Generic;
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class PlaceCubes : MonoBehaviour
{
void Start()
{
Debug.Log("I Am Alive");
//var FinalMesh = new Mesh();
int i = 0;
var fileName = "c:\\assets\\NewAsteroid001.raw";
var sr = new StreamReader(fileName);
var fileContents = sr.ReadToEnd();
sr.Close();
var lines = fileContents.Split("\n"[0]);
GameObject[] cube = new GameObject[lines.Length];
Debug.Log(lines.Length + " Total lines Read");
while (i < (lines.Length-1))
{
var Elements = lines[i].Split(',');
if (Elements.Length > 0) {
var XCoord = float.Parse(Elements[0]);
var YCoord = float.Parse(Elements[1]);
var ZCoord = float.Parse(Elements[2]);
var XLen = float.Parse(Elements[3]);
var YLen = float.Parse(Elements[4]);
var ZLen = float.Parse(Elements[5]);
cube[i] = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube[i].transform.position = new Vector3(XCoord, YCoord, ZCoord);
cube[i].transform.localScale = new Vector3(XLen, YLen, ZLen);
i++;
}
}
MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
CombineInstance[] combine = new CombineInstance[meshFilters.Length];
i = 0;
Debug.Log(meshFilters.Length + " Mesh Filters detected");
while (i < meshFilters.Length)
{
combine[i].mesh = meshFilters[i].sharedMesh;
combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
i++;
}
transform.GetComponent<MeshFilter>().mesh = new Mesh();
transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
ObjExporter.MeshToFile(GetComponent<MeshFilter>(), "c:\\assets\\NewAsteroid001.obj");
}
}
Debug output seems right until the combine part where it tells me there is only one meshfilter despite there being 15039 cubes.
As far as the export goes.... It exports precisely one cube....
Any help would be appreciated. Thanks
Comment