Skip to content

Commit 2b21990

Browse files
authored
Rework Initialization (#2227)
1 parent 884b55b commit 2b21990

File tree

47 files changed

+935
-178
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+935
-178
lines changed

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@
44

55
### Breaking Changes
66

7-
- Dropped support for Unity 2019. It reached End of Life in 2022 ([#2231](https://github.com/getsentry/sentry-unity/pull/2231))
7+
- Removed Unity 2019 support, which reached End of Life in 2022. Minimum supported version now is 2020 ([#2231](https://github.com/getsentry/sentry-unity/pull/2231))
8+
9+
- **Breaking Change**: The Unity SDK's static API has been simplified moved from `Sentry.Unity.SentryUnity` and `Sentry.SentrySdk`
10+
to `Sentry.Unity.SentrySdk`.
11+
This change enables [manual/programatic SDK initialization](https://docs.sentry.io/platforms/unity/configuration/options/programmatic-configuration/) with full functionality, previously only available through auto-initialization.
12+
The underlying .NET SDK's `SentrySdk` class is now internal, and several previously public classes like `SentryInitialization`
13+
and `SentryIntegrations` are now internal.
14+
15+
**Migration**: Update your `using` directives from `using Sentry;` to `using Sentry.Unity;`. IDEs like Rider can automatically
16+
import the missing references. In some cases, you may need both `using Sentry.Unity;` (for the static API) and `using Sentry;`
17+
(for types like `SentryId`). No changes are required to your actual SDK method calls (e.g., `SentrySdk.CaptureException()`
18+
remains the same). ([#2227](https://github.com/getsentry/sentry-unity/pull/2227))
819

920
### Features
1021

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
using System;
4+
using Sentry.Unity;
5+
using Sentry.Unity.NativeUtils;
6+
7+
namespace Sentry.Unity.Editor
8+
{
9+
internal static class SentryEditorPlatformSetUp
10+
{
11+
/// <summary>
12+
/// Called by Unity during domain reloads in the editor and before builds.
13+
/// This ensures that platform services are available during build-time processing.
14+
/// </summary>
15+
[InitializeOnLoadMethod]
16+
private static void SetUpUnityInfoInEditor()
17+
{
18+
try
19+
{
20+
_ = SentryPlatformServices.UnityInfo;
21+
}
22+
catch (InvalidOperationException)
23+
{
24+
SentryPlatformServices.UnityInfo = new SentryUnityInfo();
25+
}
26+
}
27+
}
28+
}

package-dev/Editor/SentryEditorPlatformSetUp.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "io.sentry.unity.dev.editor",
3+
"rootNamespace": "",
4+
"references": [
5+
"GUID:c18c74693c5844315b1f3cd5ae87afb4"
6+
],
7+
"includePlatforms": [
8+
"Editor"
9+
],
10+
"excludePlatforms": [],
11+
"allowUnsafeCode": false,
12+
"overrideReferences": false,
13+
"precompiledReferences": [],
14+
"autoReferenced": true,
15+
"defineConstraints": [],
16+
"versionDefines": [],
17+
"noEngineReferences": false
18+
}

package-dev/Editor/io.sentry.unity.dev.editor.asmdef.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-dev/Runtime/SentryInitialization.cs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
#endif
1616

1717
using System;
18+
using Sentry.Unity;
1819
using Sentry.Extensibility;
20+
using Sentry.Unity.NativeUtils;
1921
#if UNITY_2020_3_OR_NEWER
2022
using System.Buffers;
2123
using System.Runtime.InteropServices;
@@ -39,61 +41,57 @@
3941

4042
namespace Sentry.Unity
4143
{
42-
public static class SentryInitialization
44+
internal static class SentryInitialization
4345
{
46+
/// <summary>
47+
/// This is intended for internal use only.
48+
/// The SDK relies on <c>SetupPlatformServices</c> getting called as the very first thing during the game's
49+
/// startup. This ensures that features like line number and native support are set up and configured properly.
50+
/// This is also the case when initializing manually from code.
51+
/// </summary>
4452
#if SENTRY_WEBGL
4553
// On WebGL SubsystemRegistration is too early for the UnityWebRequestTransport and errors with 'URI empty'
4654
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
4755
#else
4856
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
4957
#endif
50-
public static void Init()
58+
private static void Init()
5159
{
52-
var unityInfo = new SentryUnityInfo();
60+
// We're setting up `UnityInfo` and the platform specific configure callbacks as the very first thing.
61+
// These are required to be available during initialization.
62+
SetUpPlatformServices();
63+
5364
// Loading the options invokes the ScriptableOption`Configure` callback. Users can disable the SDK via code.
54-
var options = ScriptableSentryUnityOptions.LoadSentryUnityOptions(unityInfo);
65+
var options = ScriptableSentryUnityOptions.LoadSentryUnityOptions();
5566
if (options != null && options.ShouldInitializeSdk())
5667
{
57-
// Configures scope sync and (by default) initializes the native SDK.
58-
SetupNativeSdk(options, unityInfo);
59-
SentryUnity.Init(options);
68+
SentrySdk.Init(options);
6069
}
6170
else
6271
{
6372
// If the SDK is not `enabled` we're closing down the native layer as well. This is especially relevant
6473
// in a `built-time-initialization` scenario where the native SDKs self-initialize.
6574
#if SENTRY_NATIVE_COCOA
66-
SentryNativeCocoa.Close(options, unityInfo);
75+
SentryNativeCocoa.Close(options);
6776
#elif SENTRY_NATIVE_ANDROID
68-
SentryNativeAndroid.Close(options, unityInfo);
77+
SentryNativeAndroid.Close(options);
6978
#endif
7079
}
7180
}
7281

73-
private static void SetupNativeSdk(SentryUnityOptions options, SentryUnityInfo unityInfo)
82+
private static void SetUpPlatformServices()
7483
{
75-
try
76-
{
84+
SentryPlatformServices.UnityInfo = new SentryUnityInfo();
85+
7786
#if SENTRY_NATIVE_COCOA
78-
SentryNativeCocoa.Configure(options, unityInfo);
87+
SentryPlatformServices.PlatformConfiguration = SentryNativeCocoa.Configure;
7988
#elif SENTRY_NATIVE_ANDROID
80-
SentryNativeAndroid.Configure(options, unityInfo);
89+
SentryPlatformServices.PlatformConfiguration = SentryNativeAndroid.Configure;
8190
#elif SENTRY_NATIVE
82-
SentryNative.Configure(options, unityInfo);
91+
SentryPlatformServices.PlatformConfiguration = SentryNative.Configure;
8392
#elif SENTRY_WEBGL
84-
SentryWebGL.Configure(options);
93+
SentryPlatformServices.PlatformConfiguration = SentryWebGL.Configure;
8594
#endif
86-
}
87-
catch (DllNotFoundException e)
88-
{
89-
options.DiagnosticLogger?.LogError(e,
90-
"Sentry native-error capture configuration failed to load a native library. This usually " +
91-
"means the library is missing from the application bundle or the installation directory.");
92-
}
93-
catch (Exception e)
94-
{
95-
options.DiagnosticLogger?.LogError(e, "Sentry native error capture configuration failed.");
96-
}
9795
}
9896
}
9997

package-dev/Runtime/SentryUserFeedback.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void SendFeedback()
8686
else
8787
{
8888
// Since there is no screenshot added we can capture the feedback right away
89-
SentryUnity.CaptureFeedback(_description.text, _email.text, _name.text, addScreenshot: false);
89+
SentrySdk.CaptureFeedback(_description.text, _email.text, _name.text, addScreenshot: false);
9090
}
9191
}
9292

@@ -99,7 +99,7 @@ private IEnumerator HideFormAndCaptureFeedback()
9999
// We're waiting for the EndOfFrame so the FeedbackForm gets updated before capturing the screenshot
100100
yield return new WaitForEndOfFrame();
101101

102-
SentryUnity.CaptureFeedback(_description.text, _email.text, _name.text, addScreenshot: true);
102+
SentrySdk.CaptureFeedback(_description.text, _email.text, _name.text, addScreenshot: true);
103103

104104
ResetUserFeedback();
105105
}

package-dev/Runtime/io.sentry.unity.dev.runtime.asmdef

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"name": "io.sentry.unity.dev.runtime",
3-
"references": [
4-
""
5-
],
3+
"rootNamespace": "",
4+
"references": [],
65
"includePlatforms": [
76
"Android",
87
"Editor",

package-dev/Runtime/io.sentry.unity.dev.runtime.asmdef.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "io.sentry.unity.editor",
3+
"rootNamespace": "",
4+
"references": [
5+
"GUID:c18c74693c5844315b1f3cd5ae87afb4"
6+
],
7+
"includePlatforms": [
8+
"Editor"
9+
],
10+
"excludePlatforms": [],
11+
"allowUnsafeCode": false,
12+
"overrideReferences": false,
13+
"precompiledReferences": [],
14+
"autoReferenced": true,
15+
"defineConstraints": [],
16+
"versionDefines": [],
17+
"noEngineReferences": false
18+
}

0 commit comments

Comments
 (0)