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

Loading images, models, maps etc. from disk during runtime.

Discussion in 'Editor & General Support' started by DatCaptainHorse, Mar 6, 2016.

  1. DatCaptainHorse

    DatCaptainHorse

    Joined:
    Mar 29, 2014
    Posts:
    13
    Trying to make simple sandbox game, where you could put models, images and such into folders and use them ingame (Not so simple anymore). I have no idea how to do this though. How would you load any .jpg image from a specific folder, and use that image as a texture for examle RawImage UI GameObject? How would you load 3D Models and make them spawnable. And how would you make the maps? I was thinking about 3D Models with data where to spawn lights, objects and such.

    I am still a complete noob, even though I have been messing with Unity as a hobby for years.
     
  2. Kronnect

    Kronnect

    Joined:
    Nov 16, 2014
    Posts:
    2,876
    Just use prefabs. A prefab is a set of gameobjects plus components (scripts, materials, ...) that reside inside Resources folder and can be loaded and instanced (Resources.Load<GameObject> and Instance are your friends here).
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,798
    Building on what @Thrawn75 said above, you can also make a folder for each type of thing, such as "Weapons" and "Tools" and put your prefabs into those folders.

    As long as those folders are underneath a folder named "Resources," then you can load them at runtime with:

    Code (csharp):
    1.  GameObject[] Weapons = Resources.LoadAll<GameObject>( "Weapons/");
    2. GameObject[] Tools = Resources.LoadAll<GameObject>( "Tools/");
    Then your code can remain the same no matter how many tools or weapons you add in the future, as long as it is set up to display a list of what can be used.
     
  4. DatCaptainHorse

    DatCaptainHorse

    Joined:
    Mar 29, 2014
    Posts:
    13
    I've thought about the Resources method, but would you be able to change them when the game is built? Like modding kind of thing, you could add a new tool or weapon. Am I going too far with this then?



    Edit:

    I got a simple script, which loops through images in Resources/background folder, and assigns the image to RawImage component.

    I cannot edit the images after build, since they're packed in a file. I need to use another method than Resources.

    One way is to just make a script to look for the background images folder, load the images and assign them. Con is that I would have to build the game everytime I change something, instead of previewing it in the editor.

    Is there a way to make Unity not pack certain folders in build?
     
    Last edited: Mar 9, 2016
  5. DarthHawk13

    DarthHawk13

    Joined:
    Feb 24, 2016
    Posts:
    63
    I have a tutorial on YouTube that shows how to do this with images on the user's hard drive. It's in three parts. The link is to the introduction of the tutorial:


    Importing 3d models into your game/project at runtime is doable but I don't cover that in my tutorial. There should be assets on the Unity Asset Store that you could modify and incorporate into your project for importing 3d models from a specified "Mods" folder on the end user's hard drive.

    This forum thread may be useful for importing 3d models at runtime: https://forum.unity.com/threads/free-runtime-obj-loader.365884/