Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Importing Objects through runtime with this file explorer code.

Discussion in 'Scripting' started by chronochris, Jul 26, 2015.

  1. chronochris

    chronochris

    Joined:
    Jun 7, 2015
    Posts:
    61
    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.
    Code (CSharp):
    1.         if (WinType == WindowType.Open && GUI.Button(bottom, "Open"))
    2.         {
    3.             String FileToOpen = OpenPaths[OpenPaths.Length - 1];
    4.             if (File.Exists(FileToOpen) || Directory.Exists(FileToOpen))
    5.             {
    6.                 //////////////////////////////////
    7.                 //Run Your Open file code here! Have your open file code in a function that accepts a path (string) and pass to it: FileToOpen.
    8.                 ////////////////////////////////
    9.                 Debug.Log("Opening File: " + FileToOpen);
    10.             }
    11.             else Debug.LogError("The File you are trying to open must have gotten moved, or deleated.");
     
    Last edited: Jul 26, 2015
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    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.
     
  3. chronochris

    chronochris

    Joined:
    Jun 7, 2015
    Posts:
    61
    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.
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    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.
     
  5. chronochris

    chronochris

    Joined:
    Jun 7, 2015
    Posts:
    61
    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.
     
  6. chronochris

    chronochris

    Joined:
    Jun 7, 2015
    Posts:
    61
    I seriously don't understand why this isn't something like
    Code (CSharp):
    1.     void Start () {
    2.         var assetimp = AssetImporter.GetAtPath("Assets/TestObject/FinalBaseMesh.obj");
    3. assetimp.Instantiate();
    4.  
    5.     }
     
  7. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
  8. chronochris

    chronochris

    Joined:
    Jun 7, 2015
    Posts:
    61
    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.

    Code (CSharp):
    1.         if (WinType == WindowType.Open && GUI.Button(bottom, "Open"))
    2.         {
    3.             String FileToOpen = OpenPaths[OpenPaths.Length - 1];
    4.             if (File.Exists(FileToOpen) || Directory.Exists(FileToOpen))
    5.             {
    6.                 Debug.Log(obj);
    7.                 obj.ImportFile(FileToOpen);
    8.                 //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 :p
     
  9. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    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.
     
  10. anithahoney657

    anithahoney657

    Joined:
    Apr 24, 2019
    Posts:
    1
    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!!