Before updating to Windows 10 you may need to return your Pro license. More information here

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.

more ▼

asked Oct 31, 2009 at 10:51 AM

avatar image

146 2 4 7

(comments are locked)
10|3000 characters needed characters left

6 answers: sort voted first

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.

more ▼

answered Oct 31, 2009 at 08:44 PM

avatar image

102 1 1 5

its an Unity PRO only thing, right?

Jun 19, 2011 at 10:50 PM BerggreenDK

no matter how many times i read that...

i still cant do it

Mar 19, 2012 at 01:26 AM VonTalavang
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Apr 24, 2010 at 08:55 AM

avatar image

380 20 32 24

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Nov 14, 2009 at 11:53 AM

avatar image

67 3 2 7

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.

Jan 17, 2012 at 10:29 AM Lenn Dolling
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered May 25, 2010 at 06:45 PM

avatar image

393 26 26 38

(comments are locked)
10|3000 characters needed characters left

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;

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.

more ▼

answered Jun 06, 2010 at 02:05 AM

avatar image

499 17 19 33

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.

Jun 06, 2010 at 02:59 AM Tetrad

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 300MB of ogg and dds files out of Resources. None of these files are loaded in the scene. Both exe's take up the same 518MB of memory when run, which shows that 300MB 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.)

Oct 06, 2012 at 06:45 AM guavaman
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Oct 31, 2009 at 10:51 AM

Seen: 61203 times

Last Updated: Oct 06, 2012 at 07:14 AM