Search Unity

Adding Positions to the Unity OBJImporter

Discussion in 'Scripting' started by Vastatio, Mar 21, 2015.

  1. Vastatio

    Vastatio

    Joined:
    Mar 21, 2015
    Posts:
    7
    So, I'm trying to dissect the Unity Wiki's .obj Importer. I can see how the whole thing works and runs, but I want the importer to have some other features, and I have been stuck on how to do it. I was just wondering how I would add the .obj file into the Unity scene, since the .obj Importer already loads the file and checks all the contents. http://wiki.unity3d.com/index.php/ObjImporter

    I want to add it as a GameObject, but how would I add the .obj file instantly? is there a built in method?
     
  2. Vastatio

    Vastatio

    Joined:
    Mar 21, 2015
    Posts:
    7
    Anyone know?
     
  3. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    ImportFile() seems to return a Mesh. Assign that mesh to a MeshFilter.
     
  4. Vastatio

    Vastatio

    Joined:
    Mar 21, 2015
    Posts:
    7
    Could this work then?

    Code (CSharp):
    1. public void Start() {
    2. meshFilter = new MeshFilter();
    3. OBJImporter importer = new OBJImporter();
    4. GameObject go = new GameObject();
    5. meshFilter.mesh = importer.ImportFile(path);
    6. meshFilter.transform.Translate(playerObj.x - 100, playerObj.y - 100, playerObj.z);
    7. meshFilter.gameObject = go;
    8.  
    9. }
    10.  
    11. public void Update() {
    12.  if(....) Instantiate(meshFilter.gameObject);
    13. }
    14.  
    If I understand this correctly, this will create a meshFilter, set the meshFilter's mesh to the current path variable that is assigned, creates a base GameObject, sets that to the meshFilter's gameObject, then sets the position of it to my players x-100, y-100, and z axis.

    Then the update function checks for something and Creates the current meshFilter gameObject that is assigned to it.

    I was thinking of creating a SelectionGrid to find out the path through user input, then assigning everything to the mesh filter. Could this work?
     
    Last edited: Mar 22, 2015
  5. Vastatio

    Vastatio

    Joined:
    Mar 21, 2015
    Posts:
    7
    So, would this work?
     
  6. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Try it.
     
  7. Vastatio

    Vastatio

    Joined:
    Mar 21, 2015
    Posts:
    7
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3. using System.Collections;
    4. using System.IO;
    5.  
    6. public class GUIObjectSelection
    7. {
    8.  
    9.     private OBJImporter importer;
    10.     private string[] filePaths;
    11.     private int selectionGridInteger;
    12.     private MeshFilter meshFilter;
    13.     private string filePath = "C:/Program Files(x86)/Steam/SteamApps/common/NanoForge/Models";
    14.     private GameObject obj;
    15.  
    16.     public void Start()
    17.     {
    18.         importer = new OBJImporter();
    19.         filePaths = getFilePaths(filePath);
    20.         meshFilter = new MeshFilter();
    21.         meshFilter.mesh = importer.ImportFile(filterReturn(selectionGridInteger));
    22.         obj = new GameObject();
    23.         meshFilter.gameObject = obj;
    24.  
    25.     }
    26.  
    27.     public string[] getFilePaths(string path)
    28.     {
    29.         string[] newFilePaths = Directory.GetFiles(@path, "*.obj", SearchOption.AllDirectories);
    30.         return newFilePaths;
    31.  
    32.     }
    33.  
    34.    
    35.  
    36.     public void Update()
    37.     {
    38.  
    39.     }
    40.  
    41.     public void OnGUI()
    42.     {
    43.         if (Input.GetKeyDown("O"))
    44.         {
    45.             GUILayout.BeginVertical("Box");
    46.             selectionGridInteger = GUILayout.SelectionGrid(selectionGridInteger, filePaths, 1);
    47.             if (GUILayout.Button("Start")) obj = Instantiate(meshFilter.mesh);
    48.             GUILayout.EndVertical();
    49.         }
    50.     }
    51.  
    52.     public string filterReturn(int selection)
    53.     {
    54.         return filePaths[selection];
    55.     }
    56.  
    57. }
    58.  
    59.  
    Ok, well I tried this code.

    Got some errors:

    Assets/Scripts/GUIObjectSelection.cs(23,20): error CS0200: Property or indexer `UnityEngine.Component.gameObject' cannot be assigned to (it is read only)

    Assets/Scripts/GUIObjectSelection.cs(29,43): error CS1501: No overload for method `GetFiles' takes `3' arguments

    Assets/Scripts/GUIObjectSelection.cs(47,62): error CS0103: The name `Instantiate' does not exist in the current context
     
  8. Vastatio

    Vastatio

    Joined:
    Mar 21, 2015
    Posts:
    7
    So, why am I getting these errors?