Search Unity

[C#] How to load 3D files from folder & display them in a "3D list"

Discussion in 'Scripting' started by Deleted User, Oct 5, 2014.

  1. Deleted User

    Deleted User

    Guest

    Hi everyone :)

    I do need some help please, for building an application to view & select 3D .fbx gameobjects found in a specific folder, that automatically updates itself when new 3d models are added to the folder (might requires a new "build", doesn't matter for me at this time) & displayed in a kind of "3D list" thing.

    Something exactly like the PS2 MC/HDD save viewer :


    I hope that I'm clear enough (sorry if it's not the case)

    Can someone help me or give me some tips on how to do that (in C sharp)?

    Thanks in advance.:cool:
     
  2. Deleted User

    Deleted User

    Guest

    Some help, please? :p
     
  3. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    If you want to import FBX files at runtime, you'll need to use the Autodesk FBX SDK. This will require Unity Pro and it'll be a lot of work to implement.

    http://answers.unity3d.com/questions/47616/import-fbx-using-autodesk-fbx-sdk-in-real-time.html

    If you wanted OBJ files, there are a few open source libraries that will help with it, and at least 1 really good non-free library on the asset store for it. This would *not* require Unity Pro and would be a lot easier to implement. Especially if you bought the non-free one that Eric5h5 makes, as he seems to support it well.
     
  4. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    docs.unity3d.com/ScriptReference/Resources.html
    Often, questions can be answered by looking through the API docs.
     
  5. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    That's not how you import external fbx files.
     
    Last edited: Oct 13, 2014
  6. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    He didn't specify that he wanted to import them, only that he wanted a list of them.
     
  7. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    ...which that's still not actually the right class for, but eh, the answer to listing them is still in the API docs. If importing them is what he wants, that's a whole different beast.
     
  8. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    He implied it by saying that he wanted to import FBX files from a specific folder.
    But yes, you can use Resouces.Load, but you wouldn't be able to have a system like with the PS2.
    You can't load a model that wasn't present at build time.
    I think you can use this to load pre-packaged prefabs though.
     
  9. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    He said he wanted to view and select, never used the word import, or said what he wanted to do once selected.
     
  10. Deleted User

    Deleted User

    Guest

    wow, thanks everyone!

    First off, I actually wanted to be able to open .fbx files "on the fly". But if it's not possible, nevermind! As long as it will mimic the PS2 system, that's okay. :) (perhaps I'll be able to do it when I'll gain more experience in C#).

    I've made several things (people helped me) and now it successfully load models (using the "Resources" folder isn't the most appropriate folder for me, but I wasn't able to find another way).

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Loading : MonoBehaviour {
    5.  
    6.     private Object[] objects;
    7.  
    8.     private GameObject[] model3d;
    9.  
    10.    
    11.     void Start()
    12.     {
    13.         //Load 3D models in "Resources" folder.
    14.         this.objects = Resources.LoadAll("List", typeof(GameObject));
    15.        
    16.        
    17.         this.model3d = new GameObject[objects.Length];
    18.        
    19.        
    20.         for(int i=0; i < objects.Length;i++)
    21.         {
    22.             this.model3d[i] = (GameObject)this.objects[i];
    23.             // Instantiate(model3d[i], transform.position, transform.rotation);
    24.             Instantiate (model3d[i],new Vector3(i * 2.0F, 0, 0), transform.rotation );
    25.         }
    26.     }
    27.  
    28.  
    29.     void Update () {
    30.    
    31.     }
    32. }
    Right now, I would like to be able to select one, to add some values to each file and to display them somewhere on the screen (like the name, date, etc.), really like the PS2 menu. But don't know how to do that :( Perhaps a dictionary that'll extract 3D Models' names?

    Any help in this case? Thanks in advance :)
     
  11. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    You could create a class for it, and store that in the array.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ModelContainer {
    5.     public GameObject prefab;
    6.     public string name
    7.  
    8.     public ModelContainer(GameObject _prefab, string _name) {
    9.         this.prefab = _prefab;
    10.         this.name = _name;
    11.     }
    12. }
    13.  
    14. public class Loading : MonoBehaviour {
    15.     private Object[] objects;
    16.     private ModelContainer[] model3d;
    17.  
    18.     void Start()
    19.     {
    20.         //Load 3D models in "Resources" folder.
    21.         this.objects = Resources.LoadAll("List", typeof(GameObject));
    22.    
    23.         this.model3d = new GameObject[objects.Length];
    24.    
    25.         for(int i=0; i < objects.Length;i++)
    26.         {
    27.             GameObject currentObj = (GameObject)this.objects[i];
    28.             this.model3d[i] = new ModelContainer(currentObj, currentObj.name);
    29.             Instantiate (model3d[i].prefab, new Vector3(i * 2.0F, 0, 0), transform.rotation );
    30.         }
    31.     }
    32.     void Update () {
    33.  
    34.     }
    35. }
    If you want to store more advanced data types, then add a component with the data on the prefab. Then get the component when loading it. Like that you can easily request the information from the given component.

    He wanted to read it from the hdd(ps2 example). Please stop taking everything so litteral ~_~
     
  12. Deleted User

    Deleted User

    Guest

    Wow, thank you very much, this fit perfectly my needs :)
    I'll try it out later and get back here if I need more help.
    Thanks again :)
     
  13. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    I'm a programmer! What do you expect? ;)
     
  14. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    But really, the Resources class isn't your only option. Another option would be to use the System.IO class. It wouldn't work with the web player, but you can use whichever folder you want, or browse through various folders. It'd actually probably fit your needs better, since you can get specific info on each file (size, date modified, etc.)

    https://msdn.microsoft.com/en-us/library/dd997370(v=vs.110).aspx
     
    Deleted User likes this.
  15. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    And for one final pro tip, you could store image previews with the same file name of each model but a .png extension, and show those on the cubes. So that, for example, when you find MyFbxFile.fbx, you look for MyFbxFile.png and display it if found.
     
  16. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    ot: You can edit your posts, instead of making a new one.
     
  17. Deleted User

    Deleted User

    Guest

    Thank you for those tips Dameon_, I'll try it out in the next version of my app. Now I have something at least that can run and load models + generate data =)
    But you're right, the System.IO class is a better solution for my goals.
     
  18. PolymorphiK Games

    PolymorphiK Games

    Joined:
    Oct 5, 2014
    Posts:
    51
    You could also load from a .asset object and use XML data based on the ID you give it. Creating a list like the one you you have is just a simple texture or 3D object. Nothing complicated, you can create that list on the fly like the PS2 UI system. The only thing is what exactly do you want to do, I am a little lost as to what it is you are trying to accomplish. If you just want to see the data like when it was created or the name of the file, thats easy. If you need help with the GUI I can help you make it. If its something like Dynamically changing the .FBX file, thats a little more complicate.