Can a Unity game create/load assets at runtime?

Hello, everyone.

I want to have a very moddable game, in the form of having a set of folders with text files and images, that the game can read an turn into textures and scripts components to apply to game objects. Is this possible at all? And if so, could you guide me to how to do this?

Yes you can load assets at runtime. Though you have to distinguish between “raw” assets and AssetBundles. While AssetBundles can contain any kind of asset, “raw” assets are quite limited. That’s because the Unity engine itself doesn’t have all the asset importers that the editor has. An AssetBundle contains the assets in Unity’s proprietary asset format.

The only assets that the Unity engine can load natively at runtime are:

  • Images in the format JPG or PNG. No other image formats are supported currently
  • Audio as WAV, OGG, XM, IT, MOD or S3M. Some platforms which have hardware mpeg decoders (like mobile platforms) can stream and play a single mp3 file as background music. But in general Unity can’t load mp3 files at runtime.

As i said Assetbundles can contain any assets that the Untiy editor supports as the editor will convert them into the internal asset format.

Of course you can write your own “loaders” for any format you want to support. Though you have to do the parsing of the data yourself and use Unity’s APIs to actually create assets on the fly.

There are many custom file format loaders out there. Some examples are loaders for OBJ files, BMP files, JSON and others.

Unity’s native support for texture loading is directly build into the Texture2D class. However AudioClips can only be loaded with the WWW class. The WWW class can be used to load files from webservers but also from a local file path. Just have a look at the example in the docs.

Apart from using the WWW class, Unity now has a specialised / more advanced HTTP loader called UnityWebRequest. Though it doesn’t add more support for other formats. Finally you can also use the usual System.IO classes to access files on the users device. Of course this is not available in web builds for security reasons.

It is possible. You can use standard c# libraries - basic I/O - or you can look at Unity - Scripting API: Resources.Load