- Home /
Problem loading .obj runtime
I save my gameobject on disk using http://wiki.unity3d.com/index.php?title=ExportOBJ.
all is good - Blender opens nicely.
I load from disk using http://wiki.unity3d.com/index.php?title=ObjImporter
Loaded object looks like broken or empty
I am struggling with it for a week now. I have googled and tried but nothing works. I think I am missing something simple here.
ObjImporter oi = new ObjImporter ();
objToSpawn = new GameObject("TAG_0");
//Add Components
objToSpawn.transform.parent = o.transform;
objToSpawn.AddComponent<MeshFilter>();
objToSpawn.AddComponent<MeshRenderer>();
var asd = oi.ImportFile (filepath);
objToSpawn.GetComponent<MeshFilter> ().mesh = asd;
objToSpawn.AddComponent<MeshCollider>().mesh = asd;
//http://docs.unity3d.com/ScriptReference/Mesh-uv.html
Vector3[] vertices = objToSpawn.GetComponent<MeshFilter>().mesh.vertices;
Vector2[] uvs = new Vector2[vertices.Length];
int i = 0;
while (i < uvs.Length) {
uvs[i] = new Vector2(vertices[i].x, vertices[i].z);
i++;
}
objToSpawn.GetComponent<MeshFilter>().mesh.uv = uvs;
objToSpawn.GetComponent<MeshFilter> ().mesh.RecalculateNormals();
objToSpawn.GetComponent<MeshFilter> ().mesh.RecalculateBounds ();
GameObject primitive = GameObject.CreatePrimitive(PrimitiveType.Plane);
primitive.active = false;
Material diffuse = primitive.GetComponent<MeshRenderer>().sharedMaterial;
DestroyImmediate(primitive);
//fix diffuse
objToSpawn.renderer.sharedMaterial = diffuse;
objToSpawn.renderer.material.color = Color.red;
thank in advance fro anykind of help
Looks like inverted normals, possible unintended edge splitting - anything else?
Are you making any edits in Blender? What happens if you export then import without making any changes whatsoever?
I made no changes in Blender - I opened in Blender only to confirm visually the result, did not save/export in Blender. This object is generated with unity#d obj exporter.
Other user case - I also made simple cube in Blender - exported to .obj and imported using objImport - visually same case, seems broken or inverted somehow.
But thank You AlwaysSunny, I will gooogle "inverted normals" and see what comes out.
In blender go into edit mode select everything and press Ctrl+N and it will set or normals to face outwards.
actually problem has quite nothing to di with blender. Export is done using ExportOBJ and import is done using ObjImport.
$$anonymous$$esh itself seems to be right in editor view. I try now to add material or texture to it runtime.
Answer by Gf15a4 · Nov 04, 2014 at 10:41 PM
Why are you loading objects like that? Theres a much simpler way... Using resources load... You can load any object as long as its prefab or imported properly