- Home /
Loading a ScriptableObject from an Asset at Runtime (after downloading + caching)
I've made a plugin whereby the user can directly edit the quests (and other things like items etc.) that are to be used in the game. It uses Unity's ScriptableObjects and Assets.
Here is a diagram showing how the system works.
To summarise the diagram:
QuestDatabaseEditor creates new Quest object and stores it inside QuestDatabase (which is a Scriptable Object).
QuestDatabase is stored inside questDB.asset.
questDB.asset is bundled inside questDB.unity3d, an Asset Bundle.
On RUNTIME, questDB.unity3d is retrieved and questDB.asset is loaded.
THE PROBLEM is that I can't figure out how to get the ScriptableObject (QuestDatabase) out of questDB.asset on run-time! The game successfully loads the asset but I don't know to specifically get the ScriptableObject out so that it can be used.
If this isn't possible I will be switching to XML, however I'm unsure on how to serialize dictionaries in XML. I'm also open to suggestions on more suitable methods of serialisation. Thanks in advance.
Answer by Bunny83 · Jul 23, 2015 at 09:45 AM
Well, i haven't really used AssetBundles but AssetBundle.LoadAsset should just do what you want.
So in your case it should look something like:
// C#
var questDB = yourAssetBundle.Load<YourScriptableObjectType>("questDB");
As you can read on the linked page it's no longer possible to load individual components derectly from prefabs inside the bundle, but ScriptableObjects should still work as they are seperate assets.
If that doesn't work or if you don't know the name of the asset inside the bundle you could try using AssetBundle.LoadAllAssets instead and grab the one you want from the returned array.
edit
If the data should be editable after a build, it's usually easier to use a common format as you mentioned. XML and JSON are the common ones but there are others as well. Unity's serialization system has it's limits. Since it now supports the serialization callbacks it has become much more useable, but you're still bound to the Unity editor to save / serialize / create assets.
One small advantage of assetbundles is that they can't be modified that easy by cheaters. Since XML and JSON are human readable formats it's much easier. However since you want your users to edit those files i would recommend using XML or JSON. That also allows to create an editor for your database without requiring Unity or even integrate an editor into your game.
Your answer
Follow this Question
Related Questions
Merge conflicts in main level scene 2 Answers
I want to create app in mobile and PC. what should I use for the database? 1 Answer
Can't change properties of one object by using another object's information 0 Answers
Trees that I painted on terrain are really dark and also missing parts in it's hirearchy... 0 Answers
Sorting Layer not working 1 Answer