How to use ObjImporter script available on unity wiki?

  1. I copied this (http://wiki.unity3d.com/index.php?title=ObjImporter) into a script.

  2. It gave an error (type should be of a monobehaviour to attach a component). So i added “monobehaviour” to the class defination
    public class ObjImporter : MonoBehaviour{
    }

  3. I used the following script

    using UnityEngine;
    using System.Collections;
    using System.IO;

    public class importOBJ : MonoBehaviour {

    // Use this for initialization
    public GameObject emptyPrefabWithMeshRenderer;
    public string meshPath;
    public GameObject spawnedPrefab;
    	
    void Start () {
    	Mesh importedMesh= GetComponent("ObjImporter").ImportFile(meshPath);
    	spawnedPrefab=Instantiate(emptyPrefabWithMeshRenderer,transform.position,transform.rotation);
    	spawnedPrefab.GetComponent(MeshFilter).mesh=importedMesh;
    
    }
    
    // Update is called once per frame
    void Update () {
    
    }
    

    }

  4. It gives the error “UnityEngine.Component does not contain the definition for ImportFile”

Mesh importedMesh= GetComponent().ImportFile(meshPath);

This should make it work.

Using ObjImporter and a sampling of some OBJ test files found online I’m not seeing the uv maps after calling ImportFile()

After assigning the mesh I am calling the following:
GetComponent().mesh.RecalculateBounds();
GetComponent().mesh.RecalculateNormals();

It may be the models that I’m testing with. At first the normals were incorrect, but the Recalculate commands resolved that.

@Chitransh You’re not supposed to use ObjImporter in Mono Behaviour that’s why you get that error at the beginning, it is its own public class that could be accessed by calling ObjImporter()

    //create a ObjImporter Object
    ObjImporter ImporterMesh = new ObjImporter();
    // create a mesh and use ObjImporter to import your obj file and make it a mesh
    Mesh MyMesh  =  ImporterMesh.ImportFile(ObjPath);
    // then asign the mesh object to a mesh filter in your scene
    GameObject.GetComponent<MeshFilter>().mesh = MyMesh;