- Home /
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?
Answer by Bunny83 · Mar 02, 2017 at 02:10 PM
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.
That's good info to have. So if I want to work with say .dds files I can either write a loader for them to be parsed and reconstructed as unity assets on the fly during game time, or create a program that turns the external files into assetbundles, then make the game account for these. However, if I'm not mistaken, asset bundles require the pro version of unity, so that's off-limits for me.
What about scripts? Is there any way that I can parse a txt file and make unity create new scripts components for existing objects based off the result of said parsing?
Alternatively to creating new scripts components, is there a way to define new functions methods, or essentially code blocks during game time? I want to let modders use a custom scripting language to add "events" into the game, which would be basically user-created blocks which are called in certain circumstances by the game.
AssetBundles no longer require Unity Pro. Actually all license models are almost the same now. The only restriction that the personal license has is that you can't remove the "$$anonymous$$ade with Unity" splash screen (and of course the $100k limit). There are some $$anonymous$$or things like the dark editor skin. However the feature set is equal for all licenses since Unity 5.0 if i remember correctly.
AssetBundles have to be created with the Untiy editor. They can be created using the batchmode of the editor if you need to automate the creation of the AssetBundles.
Yes, if you can read and "decode" a dds file yourself (or with help of third party libraries) so you get a simple color array of all pixels you can simply load that image data into a Texture2d object at runtime.
About scripting, well, Unity uses $$anonymous$$ono as scripting backend. $$anonymous$$ono / .NET scripts need to be compiled by a compiler. C# / $$anonymous$$ono is not a good choice for a dynamic scripting language. It's easier to use a simple scripting language like LUA and integrate that into your game. The main problem with C# / $$anonymous$$ono is not the compiling and loading but "unloading". You can't unload a single class or assembly. You would need to create a seperate AppDomain which can be unloaded. However communication between AppDomains is somewhat complicated / restricted. So a seperate scripting approach might be easier.
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
On unity site we have list of assets that are import automatically :
https://unity3d.com/unity/editor
What do you mean saying "load natively at runtime" ? It's the same?
@uvivagabond: The list of assets on that site are related to the Unity editor. The editor can import a lot of different formats, but the Unity engine (the runtime) can not. The editor imports all those different assets and stores them in a proprietary asset format which the runtime can read. That's the format in which the asset you have imported are stored inside a build you create. This is basically the same format that AssetBundles use. AssetBundles need to be created with the Untiy editor but can then stored seperately of the actual game. The game can load AssetBundles at runtime and use the assets in them just as they were already packed in the build.
At runtime Unity can only load a few formats. As you can read on the WWW.texture page:
Returns a Texture2D generated from the downloaded data (Read Only).
The data must be an image in JPG or PNG format.
Further more Unity doesn't support any model formats at runtime. A model is represented as a $$anonymous$$esh object if you import a model inside the editor. The $$anonymous$$esh class doesn't have any "load" function which you could use at runtime. You can create a $$anonymous$$esh object from scratch by providing a vertex array and a triangle array. You could parse them from arbitrary model formats but you have to do this yourself.
Answer by juicyz · Jan 17, 2017 at 04:58 PM
It is possible. You can use standard c# libraries - basic I/O - or you can look at https://docs.unity3d.com/ScriptReference/Resources.Load.html
@juicyz : But, can I use I/O to load an external image (given it's absolute path) during game time and use it as a texture? Do I need to do anything particular for that? Would you $$anonymous$$d providing a small code snippet of it?
Your answer
Follow this Question
Related Questions
Is this possible with unity3D. (Havent found in forum) 0 Answers
How to read an OBJ file outside of unity,How do i read an OBJ file outside of unity? 1 Answer
Stealth Tutorial mod 0 Answers
Any way to let users create scenes or prefab in one project and load them at runtime in another? 1 Answer
How do I expose a folder in my build 1 Answer