Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by gsgeek · Jan 17, 2017 at 11:38 AM · modding

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gsgeek · Mar 02, 2017 at 03:45 PM 0
Share

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.

avatar image Bunny83 gsgeek · Mar 03, 2017 at 01:46 AM 0
Share

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.

avatar image unityBerserker · Mar 02, 2017 at 11:24 PM 0
Share

@Bunny83

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?


avatar image Bunny83 unityBerserker · Mar 03, 2017 at 02:00 AM 0
Share

@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.

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gsgeek · Mar 02, 2017 at 01:17 PM 0
Share

@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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges