I’ve stumbled over a few API calls in the EditorBuildSettings class, e.g. AddConfigObject. From reading the scripting docs it’s not really clear to me if this is something new I can use for editor tooling or how this is intended to be used.
Are any Unity systems using this currently? In what way can I use this for my own editor build configuration?
The API looks as if I can add a custom object to the EditorBuildSettings class, but I need to save it to the project. So what benefit or use does it have to go through the EditorBuildSettings class?
Today I’ve stumbled across the Unity localization package using the API as per their documentation:
This sounds like it should be doing exactly what I’d hoped for, but in my own tests, I can’t get it to work. I have a ScriptableObject instance in the assets folder of my project. In InitializeOnLoad I call EditorBuildSettings.AddConfigObject with my instance. In the built player, I have a test script running in Start which looks for loaded objects via Resources.FindObjectsOfTypeAll and I also log output in OnEnable of the ScriptableObject, which should be called when it gets loaded. However, nothing turns up.
Does anyone know how to correctly use this API? I’d like to store global configuration without having to reference an object in a scene or explicitly load it via any resource path or asset bundle.
After some more investigation I finally found how this intended to be used (or maybe it is more of an internal thing since the documentation is so sparse).
The EditorBuildSettings ConfigObject is like an EditorPref for object references in the editor. For example, if I have a custom tool that uses a ScriptableObject in the assets folder as a settings object, I don’t have to hardcode the path to it. Instead, I can implement a settings menu via the new SettingsProvider API and show an object reference field to the asset in the project, which the user must select. Once the selection has been made, I can save this reference via the ConfigObject API and it will be persistent; I can close Unity, reopen and then ask for the ConfigObject by name to get a reference to it.
All of this has nothing to do with persisting object into the build, however. For this to work, we must manually add the object to the build. For example, by adding it to the Preloaded Assets list in the Project Settings, so it will be automatically loaded and can be retrieved from memory at runtime. I implemented OnPreprocessBuild and OnPostprocessBuild to automatically add my settings object into the list before the build and remove it afterwards. This is the same as the Unity Localization preview package does it and seems to work very well.
I think I need to correct myself here: The config object is actually stored as part of the EditorBuildSettings file on disk. At first I didn’t notice because Unity doesn’t write changes to disk until the project is saved. To me, this has the implication that I won’t be using ConfigObject as a user preference, since the build settings are shared between members of my team and comitted to source control and of course I don’t want to dirty this file with local data. Still, one might want to add config objects that are shared, etc, just something to be aware of.