# Native Support | Sentry for Unity

The Sentry SDK supports C# and Native crashes on Android, iOS, Windows, macOS and Linux. The support is set to `enabled` by default. For Game Consoles, the SDK supports C# errors. While Sentry supports native crashes on consoles, support for that through the Unity SDK is a work in progress. Reach out via GitHub for the platform you're interested in: [PlayStation](https://github.com/getsentry/sentry-unity/issues/2050), [Switch](https://github.com/getsentry/sentry-unity/issues/2052) and [Xbox](https://github.com/getsentry/sentry-unity/issues/2051)

## [Mobile](https://docs.sentry.io/platforms/unity/native-support.md#mobile)

The native crash support for Android and iOS is provided by adding platform-specific SDKs to the generated Xcode and Gradle projects at build time.

### [Initialization of the Native SDKs](https://docs.sentry.io/platforms/unity/native-support.md#initialization-of-the-native-sdks)

The [iOS SDK](https://docs.sentry.io/platforms/apple/guides/ios.md) and [Android SDK](https://docs.sentry.io/platforms/android.md) are capable of self-initializing before the Unity engine itself is started. This allows us to capture bugs/crashes happening within the engine itself. There are two initialization types:

* `NativeInitializationType.Runtime`: Native SDKs initialize during runtime alongside the C# SDK
* `NativeInitializationType.BuildTime`: Native SDKs initialize before Unity engine starts

### [Runtime Initialization (Default)](https://docs.sentry.io/platforms/unity/native-support.md#runtime-initialization-default)

With runtime initialization, the native SDKs are initialized at runtime alongside the C# SDK. This allows all options to be dynamically configured through C# code during execution.

#### [Android](https://docs.sentry.io/platforms/unity/native-support.md#android)

At build time, the SDK modifies the generated Gradle project to include the Android SDK but sets `io.sentry.auto-init` in the `AndroidManifest.xml` to `false`. The Unity SDK will then initialize the Android SDK when it initializes itself.

#### [iOS](https://docs.sentry.io/platforms/unity/native-support.md#ios)

At built time, the SDK modifies the generated Xcode project to include the iOS SDK but will not modify the `main.m` file or write the options to file. The Unity SDK will then initialize the iOS SDK when it initializes itself.

### [Build Time Initialization](https://docs.sentry.io/platforms/unity/native-support.md#build-time-initialization)

When using build time initialization, the native SDKs are configured during build time and initialize before the Unity engine starts. This means the options are baked into the outputted projects and cannot be modified at runtime via C# code. Changes to properties like `Release` and `Environment` will not apply to events generated by the native SDKs.

#### [Android](https://docs.sentry.io/platforms/unity/native-support.md#android-1)

The SDK modifies the generated Gradle project to include the Android SDK. At build time, it will also write the options to the `AndroidManifest.xml`. These options cannot be changed at runtime. Changes to the options in the configuration callback will not affect the Android SDK.

#### [iOS](https://docs.sentry.io/platforms/unity/native-support.md#ios-1)

The SDK modifies the generated Xcode project to include the iOS SDK. It adds the initialization code to the `main.m` and generates the options provided by the editor configuration window as `SentryOptions.m`. The SDK also copies the `SentryNativeBridge` that enables the C# layer to communicate with the iOS SDK. This means that there's no need to use the Unity [built-in crash reporting](https://docs.unity3d.com/ScriptReference/CrashReport.html) functionality.

The [iOS SDK](https://docs.sentry.io/platforms/apple/guides/ios.md) supports capturing Objective-C exceptions which are disabled in the generated Xcode project by default. Consider enabling them in the "Build Settings" tab by setting `GCC_ENABLE_OBJC_EXCEPTIONS` to true.

## [Desktop](https://docs.sentry.io/platforms/unity/native-support.md#desktop)

The native support is configured and enabled as early as possible by the C# layer, right after the Unity engine starts. This is slightly different from on mobile where we inject native initialization code that runs before the Unity engine runs.

### [Windows](https://docs.sentry.io/platforms/unity/native-support.md#windows)

On Windows, the Unity SDK includes the [Native SDK](https://docs.sentry.io/platforms/native.md) with the crashpad backend. Windows native crashes are automatically captured through minidumps. To upload these to Sentry, the SDK copies the `crashpad_handler.exe` to the build output directory at the end of the build process. This executable must be included when shipping your game.

### [macOS](https://docs.sentry.io/platforms/unity/native-support.md#macos)

The Native Support on macOS relies on the [macOS SDK](https://docs.sentry.io/platforms/apple/guides/macos.md). Stackwalking happens in process.

### [Linux](https://docs.sentry.io/platforms/unity/native-support.md#linux)

On Linux the Unity SDK includes the [Native SDK](https://docs.sentry.io/platforms/native.md) with the breakpad backend. A minidump is created in process and no handler executable is required.

## [Debug Symbols](https://docs.sentry.io/platforms/unity/native-support.md#debug-symbols)

Sentry requires [debug information files](https://docs.sentry.io/platforms/unity/data-management/debug-files.md) to symbolicate your crashes. The Unity SDK provides an automated upload functionality for those symbol files that rely on the [sentry-cli](https://docs.sentry.io/cli.md). This is done transparently so you're not required to run `sentry-cli` manually. The symbol upload happens during Unity build in the editor. We've included the executables for Windows, macOS, and Linux as part of the Unity SDK package.

The automated debug symbols upload is enabled by default but requires configuration. Go to **Tools > Sentry > Editor** to enter the [Auth Token](https://sentry.io/orgredirect/organizations/:orgslug/settings/auth-tokens/), Organization Slug, and the Project Name. Note that the Unity SDK creates a file at `Assets/Plugins/Sentry/SentryCliOptions.asset` to store the configuration, that should **not** be made publicly available.

### [iOS - dSYM](https://docs.sentry.io/platforms/unity/native-support.md#ios---dsym)

Debug information files on the iOS platform are called dSYM.

For Sentry to symbolicate your crash logs we need two types of files:

1. `dSYM` that the automated symbols upload will pick up at the end of the build process without further action required.
2. `BCSymbolMap` files that are created during the archiving process

The automated symbol upload will take care of the `BCSymbolMap` files by processing them during the archiving process.

### [Manual Upload Using sentry-cli](https://docs.sentry.io/platforms/unity/native-support.md#manual-upload-using-sentry-cli)

If you don't want to rely on the automated symbol upload, you can run `sentry-cli` through the commandline. For that, you can use the provided executables from within the package or follow the [sentry-cli documentation](https://docs.sentry.io/cli/installation.md) to make it available globally. To upload debug symbols, run it with:

```bash
sentry-cli --auth-token ___ORG_AUTH_TOKEN___ debug-files upload --org ___ORG_SLUG___ --project ___PROJECT_SLUG___ PATH_TO_SYMBOLS
```

## [Store Minidumps as Attachments](https://docs.sentry.io/platforms/unity/native-support.md#store-minidumps-as-attachments)

[Minidumps](https://docs.sentry.io/platforms/native/guides/minidumps.md#what-is-a-minidump) may contain sensitive information about the target system, such as environment variables, local pathnames, or in-memory representations of input fields, including passwords. By default, Sentry only uses minidump files to create events and immediately drops them. All sensitive information is stripped from the resulting events.

All attachments types, including log files, screenshots and minidumps (if you enable Store Minidumps As Attachments), are stored for 30 days when sent to Sentry. Note that Sentry does not apply data scrubbing to attachments.

☝ This feature is supported on Windows, Linux, and Android.

### [Enabling Minidump Storage](https://docs.sentry.io/platforms/unity/native-support.md#enabling-minidump-storage)

You can enable *Store Minidumps As Attachments* in your organization or project settings under **Security & Privacy**. By default, this setting is disabled. Determine the maximum number of crash reports that will be stored per issue; disabled, unlimited, or maximum per issue:

If you set a limit per issue, as in the example above, a limit of 5, Sentry will store the first 5 attachments associated with this issue, but drop any that follow. To make room for additional attachments, delete them. Sentry will then accept attachments until the limit is reached again.
