sentry-unity
sentry-unity copied to clipboard
Allow for a delayed or manual SDK initialization
Problem Statement
Currently, there is no straight forward way to delay the SDK's initialization. In addition, while manual initialization is possible, it might come with some hurdles.
Context
The Unity SDK relies on configuration provided within the Editor that is saved to /Resources as a .asset file.
The SDK compiles SentryInitialization.cs with your game. This way it has access to preprocessor directives.
https://github.com/getsentry/sentry-unity/blob/ed4cc86f89561aad47ea63668923764b3d83b9ce/package-dev/Runtime/SentryInitialization.cs#L54-L56
Part of loading the options is invoking the user's configuration callback.
https://github.com/getsentry/sentry-unity/blob/ed4cc86f89561aad47ea63668923764b3d83b9ce/package-dev/Runtime/SentryInitialization.cs#L60
https://github.com/getsentry/sentry-unity/blob/ed4cc86f89561aad47ea63668923764b3d83b9ce/src/Sentry.Unity/ScriptableSentryUnityOptions.cs#L214
So while you can call SentryUnity.Init, users are missing out on the native SDK setup (which includes init and scope sync) https://github.com/getsentry/sentry-unity/blob/ed4cc86f89561aad47ea63668923764b3d83b9ce/package-dev/Runtime/SentryInitialization.cs#L68
So in all practicality, manually calling call SentryUnity.Init has a very limited usecase.
Proposal
Users should have the option to control automatic initialization. This could be a separate option in addition to Enabled.
Users should have the option to manually call Init the same way the SDK does.
Workaround
- Setup programmatic configuration
- Have the
options.Enabledresolve tofalsebased on your condition for the delay - The SDK will attempt to auto-initialize and because of 2. skip
- When you're ready can call
SentryInitialization.Inityourself - The SDK will load the options again
- The SDK will invoke the options configuration callback again
- This time, you should have
options.Enabledresolve totrue - The SDK will do the rest of the initialization
** Note, that on mobile platforms, for this workaround it is recommended to have the options.InitializationType set to Runtime as otherwise, the SDK will close down the native SDK during the first initialization attempt.
I know, this is not a pretty solution but at least you're not blocked.