- Home /
Dynamic Asset loading?
Is is possible to load assets (models/textures/sounds) on runtime?
Been peeking the forums and saw some old posts where it wasn't but then i saw Resource.load, so i'm confused.
Answer by DannyL · Oct 31, 2009 at 08:44 PM
Yes, this is possible - check out the AssetBundle class (http://unity3d.com/support/documentation/ScriptReference/AssetBundle.html). This class is for loaded via the web specifically, I assume this is what you had in mind? Resource.Load() is used for loading assets by path name in the resources folder.
AssetBundle has been introduced fairly recently, hence the old forum posts where this was not possible.
no matter how many times i read that...
i still cant do it
Answer by baha · Apr 24, 2010 at 08:55 AM
hi, actually when using the resources folder the asset is encapsulated within the unity file (as part of it) this means that it is not fully dynamically loaded thus if you want to load a file that is not in the resources folder then you need to rebuild your application after adding it to the resources folder, resources folder is always attached and deployed with the unity file but if you want really dynamic loading of assets you need to use the WWW Class either you pick asset bundles for your content streaming (Pro) or you might use its functions to load Textures, Audio, Video and to load models you mights take a look at Mesh Serializer2 at unifycommunity.com.
resources folder will always combine your files and assets within the unity file thus you can't load them unless they are built within the unity file, other content deployed somewhere else can't be loaded through Resources.Load() function.
Answer by Pixelstudio · Nov 14, 2009 at 11:53 AM
yes it is very simple. All you resources got into the directory "resources". Then you can use resources.load to load them into you scene at runtime.
yes.. very simple. if yo don't have the directory/folder "resources" in your assets folder then you should make it and put all your prefabs in there. I found it golden.
Answer by kilgore · Jun 06, 2010 at 02:05 AM
I just recently ran into the same issue and wanted to post the following code, it is basically an extenstion of what xeophin posted and I can confirm that it is working.
In this example, I have a GameObject with an AudioSource component attached and a script with a function called PlayClip. I pass the audio clips name that then loads clip from the Resources fold.
public void PlayClip(string clipName){
AudioSource source = gameObject.GetComponent<AudioSource>();
source.rolloffFactor = 0;
AudioClip clip = Resources.Load(clipName)as AudioClip;
source.clip = clip;
audioSource.Play();
}
note - I am making the AudioSource rolloffFactor 0 so that it can be heard everywhere without, well, fallOff. That one got me for a few minutes.
It's important to note two things about the Resources class. 1) Filenames passed in should not use extensions. 2) Everything in your Resources folder is loaded at run time and not on demand. It doesn't actually stream it in or block to load the file.
2) Everything in your Resources folder is loaded at run time and not on demand
This is not true. Everything in your resources folder is included in the final game build whether used or not, but the contents are not loaded into memory at runtime. Objects will only be loaded into memory if they're referenced in the scene or Resources.Load() is called on them. That's the whole point of the class. Resources.UnloadAssets() and Resources.UnloadUnusedAssets() exist so you can reclaim memory used by dynamically loaded assets if needed. You can always reload them again with another Resources.Load() call.
Just to make sure, I built two test exe's from my game, one with all my current stuff in Resources, and one where I moved about 300$$anonymous$$B of ogg and dds files out of Resources. None of these files are loaded in the scene. Both exe's take up the same 518$$anonymous$$B of memory when run, which shows that 300$$anonymous$$B of data I moved out of Resources was not being loaded at runtime. (Thank goodness, I was worried you might be right and I had been misusing Resources for almost 3 years.)
Answer by xeophin · May 25, 2010 at 06:45 PM
Sorry just seconds after I posted the comment, I got it to work, using the following code (assuming the same example you used):
AudioClip clipObj = Resources.Load("GoodAfternoon") as AudioClip;
That should work.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How I can get an instace of a material, created in a project panel 1 Answer
Asset Managing for Mobile Devices 0 Answers
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers