I would like to programmatically open all .asset files from a given folder in StreamingAssets and get a list of the corresponding ScriptableObjects. The work flow being that our designers can just generate and dump the assets in the folder and at Runtime I generate the list based on what is there instead of having an explicit list defined somehow.
I can get the file and open if from StreamingAssets using WWW just fine, but the next step of getting the ScriptableObject object from that WWW object is causing issues.
How do I get a the ScriptableObject subclass from WWW that was just loaded. Is this even a viable use case for ScriptableObjects?
WWW www = // .asset file loaded up from somewhere
MyScriptableObject msg = // How to set this from the WWW?
I don’t think such a scenario would be possible. The .asset files are usually built into runtime compatible format… I don’t think you could load these at runtime.
In order to dynamically load assets at runtime, i am familiar with the following techniques:
- Asset bundles - this is a proprietary Unity package that can include scenes, prefabs, and other asset types. You can distribute these under Streaming assets, or download them from a server at runtime and then load & use them in the game.
- Resources - an API for loading Unity assets at runtime. This is done via the Resources class. Note that all the assets must be placed under Resources/ (any folder in the project that is named Resources will do). This technique was hinted i believe to be deprecated in the future (since it’s very similar to asset bundles, so why keep 2 different mechanisms ?)
- JSON - Unity includes a JSON serialization in the newer versions (JsonUtility). It just came to my mind that this serializer is said to use Unity’s serialization system, and so it should work with MonoBehaviours and ScriptableObjects. you could serialize your ScriptableObjects and store them under Streaming assets, then at runtime deserialize them back into ScriptableObjects. That might just work. never tested this though.
If all you need is a way to (a) include ScriptableObject-based assets in your game and (b) have them in a list without worrying about the creation of this list, a simple solution could be to have a predefined folder (under Resources) to place these assets. On top of that, you could add custom editor code that manages a list of objects and gets rebuilt every time a new asset is added under a certain folder. That should be pretty straightforward to implement.
At runtime, you would access this list and be able to load your objects from it. Just a suggestion 