Loading .OBJ into Unity at runtime

My job is to write code that loads the .OBJ into Unity at runtime. Unity provided a sample wiki code . I used the following code to use the class in the link:

public class Main : MonoBehaviour {

    // Use this for initialization
    void Start () {
        Mesh holderMesh = new Mesh ();
        ObjImporter newMesh = new ObjImporter();
        holderMesh = newMesh.ImportFile("C:/Users/cvpa2/Desktop/ng/output.obj");
    }

      

I don't get any errors in Unity Monodevelop, but none of them are loaded. What is the likely solution?

+3


source to share


1 answer


Just creating a Mesh object isn't enough. You will need to do at least two more things:

  • Create a MeshRenderer component
  • Create a MeshFilter component

So, if you change the code to the following, you should at least see your grid if it was successfully created.



using UnityEngine;
using System.Collections;

public class Main : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {
        Mesh holderMesh = new Mesh();
        ObjImporter newMesh = new ObjImporter();
        holderMesh = newMesh.ImportFile("C:/Users/cvpa2/Desktop/ng/output.obj");

        MeshRenderer renderer = gameObject.AddComponent<MeshRenderer>();
        MeshFilter filter = gameObject.AddComponent<MeshFilter>();
        filter.mesh = holderMesh;
    }
}

      

From there, you still have to assign stuff (if loaded / created) and other such things, but that will be the start.

+3


source







All Articles