New in 0.4.0, “Initialization Objects” of Addressables is not that you can get an object at start up, it is not that you can load some asset address immediately on start up. The meaning is in the sense of doing something on initialization, based on contents in your ScriptableObject “initialization object” which you can keep serialized in the project and tweak it to change runtime behaviour.
The key point is right here at intialization. There is a loop over “real time data” (rtd) to CreateInstance on each, on the initialization of Addressables system. This create instance seems to throw away the resulting object… why?
Inside the RTD, is a list of ObjectIntializationData
Build script is the one who put ObjectInitializationData into the RTD, then serialized them all into a text file.
That aaSettings is this section of the settings file. Each one of this has a duty to produce ObjectInitializationData to be serialized.
Having an interface IObjectIntializationDataProvider on your ScriptableObject allows you to add the asset to that list. That SO file will be your config file data storage class to dictate what will happen at init at runtime.
So how can I provide this ObjectInitializationData as the requirement of this interface says?
Uh, it seems that everything in this struct is readonly, and there’s no constructor whatseover. Surely we don’t want to just new an empty ones?
Below, there is a UNITY_EDITOR scope which contains the only point that could create this struct with values inside.
objectType is the type you want to make at runtime. (not the same as the config file type earlier)dataObject will be directly ToJson , and you will be given this back as string later when the init script is making your objectType later at runtime.
Finally let’s take a look how your serialized dataObject will come back to life, the CreateInstance method of ObjectInitializationData :
This is the one at that useless throw away call mentioned at the top. With the power of reflection activator to create an instance of serialized type, it chck for IInitializableObject and call Initialize . In this Initialize is the main highlight, it will run this code at initialization. What do you want to do?
That id and data is the one you provide when calling CreateSerializedInitializationData . They come back at runtime here.
Let’s look at the only one example Unity has. This CacheInitializationSettings let you store some settings (in the form of CacheInitializationData ) then “do something” in Initialize logic defined in CacheInitialization class. (That’s 3 classes to make the whole thing work)
This SO script should be in EDITOR only scope, since we learned that CreateSerializedInitializationData is usable only in UNITY_EDITOR . So, an asset created from this ScriptableObject is a config file of sorts, which only make sence in-editor. Name is also the interface’s requirement.
Creating an asset file of this type, we can now modify CacheInitializationData as we like.
When building, it will serialize CacheInitializationData ( m_data ) using ToJson (that is to string). That data class has be in non-editor scope becuase we will use it together at runtime to activate the CacheInitialization ‘s Initialize
If you remember from the beginning, the “throw away” CreateInstance call will come to this. CreateInstance will use reflection to create an instance of CacheInitialization (it has the logic we want to call at init), then call Initialize on it if it has an interface IInitializableObject (it should, or else the throw away is useless for real).
You see here that it is finally doing something to the Caching static variable using the serialized data brought back to life from string with FromJson . If the thing was ToJson successfully, FromJson should give you the correct object.
Now you know the throw away CreateInstance is not useless, because the actual logic was hidden in here. This class was not meant to be instantiated, but just a host of this initialize method script to be run.
If you use this chance to modify static variables, you could make your own serializable config file system as you like! That’s it.