Importing Objects through runtime with this file explorer code.

I am trying to import an object into an application while at runtime. I found this code for opening a file explorer, it can only open the directory where the application is, but that’s fine. I’m having an issue with understanding how to utilize the open button here is the code that matters.

        if (WinType == WindowType.Open && GUI.Button(bottom, "Open"))
        {
            String FileToOpen = OpenPaths[OpenPaths.Length - 1];
            if (File.Exists(FileToOpen) || Directory.Exists(FileToOpen))
            {
                //////////////////////////////////
                //Run Your Open file code here! Have your open file code in a function that accepts a path (string) and pass to it: FileToOpen.
                ////////////////////////////////
                Debug.Log("Opening File: " + FileToOpen);
            }
            else Debug.LogError("The File you are trying to open must have gotten moved, or deleated.");

The entirety of the file explorer’s job was to get you a string path+filename. All of that GUI stuff is just for that little string, and that the user gets to select and know what it’s pointing at. What you do with that information afterward though is up to you, and you’ll have to use a different process to actually reach out and read it / load it / save to it.

I figured as much, which API’s are in charge of importing during runtime?

Edit: I just wanted to be sure on the wording of that code’s comment, I can take it from there, I’m just relatively new to Unity.

I spent about a month in a different incorrect direction, and this time I want to be sure what I’m doing is correct with the tool.

WWW is the main one I believe (it says web pages but I believe it is what’s used to load most files external to the build itself). I really haven’t used it much, so you may start your research there or wait for someone more knowledgeable here to come along.

This resource will not be a part of the build. This is something external that the user wants to see in unity. It is either a .3ds .obj etc.

I seriously don’t understand why this isn’t something like

    void Start () {
        var assetimp = AssetImporter.GetAtPath("Assets/TestObject/FinalBaseMesh.obj");
assetimp.Instantiate();

    }

Here’s something I found that may be helpful, I admit I was somewhat dubious that anything more complicated than a texture file could actually be imported at runtime this way.

http://wiki.unity3d.com/index.php?title=ObjImporter

That was extremely helpful. I’m confident I have imported the Object. Here is the code.

public ObjImport obj; /// The game object containing the ObjImport script was dragged onto this spot.

        if (WinType == WindowType.Open && GUI.Button(bottom, "Open"))
        {
            String FileToOpen = OpenPaths[OpenPaths.Length - 1];
            if (File.Exists(FileToOpen) || Directory.Exists(FileToOpen))
            {
                Debug.Log(obj);
                obj.ImportFile(FileToOpen);
                //basego = GameObject.Instantiate(obj.ImportFile(FileToOpen), new Vector3(0,0,0), this.gameObject.transform.rotation) as GameObject;

I’m confident the code works. I tried to import an object that had more than 6500 vertices and received an error, I will work on that later, but I tried a much smaller .3ds and returned no errors, but I don’t see the object. I tried to instantiate it into the scene, but that didnt work. The commented out basego :stuck_out_tongue:

A GameObject is basically just an empty shell with a transform (position/rotation/scale) component by default. It’s not a mesh, and GameObject.Instantiate() basically just clones an existing GameObject and any attached components and returns the clone. In order to display the imported mesh, you’d need to create a GameObject then add a MeshRenderer or SkinnedMeshRenderer component to it, then assign the mesh to that component. I think, at least, as I’ve never tried to do such a thing from a script.

Hi @chronochris
can you just bit explain about the ObjImport script - what script of lines it contains and how it works
so that it will be very helpful for me

Advance Thank you!!