---
title: Unity Feature Flags
description: Set up Datadog Feature Flags for Unity applications.
breadcrumbs: Docs > Feature Flags > Client-Side Feature Flags > Unity Feature Flags
---

# Unity Feature Flags

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com, us2.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md). ().
{% /alert %}

{% /callout %}

## Overview{% #overview %}

This page describes how to instrument your Unity application with the Datadog Feature Flags SDK. Datadog feature flags provide a unified way to remotely control feature availability in your app, experiment safely, and deliver new experiences with confidence.

This guide explains how to install and enable the SDK, create and use a `FlagsClient`, and configure advanced options.

## Installation{% #installation %}

Declare the Datadog Unity SDK as a dependency in your project. The Datadog Unity SDK includes feature flags support.

1. Install the [External Dependency Manager for Unity (EDM4U)](https://github.com/googlesamples/unity-jar-resolver). This can be done using [Open UPM](https://openupm.com/packages/com.google.external-dependency-manager/).

1. Add the [Datadog SDK Unity package](https://github.com/DataDog/unity-package) using its Git URL `https://github.com/DataDog/unity-package.git`.

1. (Android only) Configure your project to use [Gradle templates](https://docs.unity3d.com/Manual/gradle-templates.html), and enable both `Custom Main Template` and `Custom Gradle Properties Template`.

1. (Android only) If you build and receive `Duplicate class` errors (common in Unity 2022.x), add the following code to the `dependencies` block of your `mainTemplate.gradle`:

   ```groovy
   constraints {
        implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0") {
            because("kotlin-stdlib-jdk8 is now a part of kotlin-stdlib")
        }
   }
   ```

## Initialize the SDK{% #initialize-the-sdk %}

Initialize Datadog as early as possible in your app lifecycle. Navigate to your `Project Settings` and click on the `Datadog` section to configure your client token, environment, and other settings. To create a client token, see [Client tokens](https://docs.datadoghq.com/account_management/api-app-keys.md#client-tokens).

For more information about setting up the Unity SDK, see [Unity Monitoring Setup](https://docs.datadoghq.com/real_user_monitoring/application_monitoring/unity/setup.md).

## Enable flags{% #enable-flags %}

After initializing Datadog, enable `Flags` to attach it to the current Datadog SDK instance and prepare for client creation and flag evaluation:

```csharp
using Datadog.Unity.Flags;

DdFlags.Enable();
```

You can also pass a configuration object; see Advanced configuration.

## Create and retrieve a client{% #create-and-retrieve-a-client %}

Create a client once, typically during app startup:

```csharp
var client = DdFlags.Instance.CreateClient();
```

You can also create multiple clients by providing the `name` parameter:

```csharp
var checkoutClient = DdFlags.Instance.CreateClient("checkout");
```

{% alert level="info" %}
If a client with the given name already exists, the existing instance is reused.
{% /alert %}

## Set the evaluation context{% #set-the-evaluation-context %}

Define who or what the flag evaluation applies to using a `FlagsEvaluationContext`. The evaluation context includes user or session information used to determine which flag variations should be returned. Call this method before evaluating flags to help ensure proper targeting.

```csharp
client.SetEvaluationContext(
    new FlagsEvaluationContext(
        targetingKey: "user-123",
        attributes: new Dictionary<string, object>
        {
            { "email", "user@example.com" },
            { "tier", "premium" },
        }
    ),
    onComplete: success =>
    {
        if (success)
        {
            // Flags are ready — begin evaluating
        }
        else
        {
            // Fetch failed — evaluations return default values
        }
    }
);
```

This method fetches flag assignments from the server asynchronously in the background. The operation is non-blocking and thread-safe. Flag updates are available for subsequent evaluations after the background operation completes.

## Evaluate flags{% #evaluate-flags %}

After creating the `FlagsClient` and setting its evaluation context, you can start reading flag values throughout your app. Flag evaluation is *local and instantaneous*—the SDK uses locally cached data, so no network requests occur when evaluating flags. This makes evaluations safe to perform on the main thread.

Each flag is identified by a *key* (a unique string) and can be evaluated with a *typed getter* that returns a value of the expected type. If the flag doesn't exist or cannot be evaluated, the SDK returns the provided default value.

### Boolean flags{% #boolean-flags %}

Use `GetBooleanValue(key, defaultValue)` for flags that represent on/off or true/false conditions. For example:

```csharp
var isNewCheckoutEnabled = client.GetBooleanValue("checkout.new", false);

if (isNewCheckoutEnabled)
{
    ShowNewCheckoutFlow();
}
else
{
    ShowLegacyCheckout();
}
```

### String flags{% #string-flags %}

Use `GetStringValue(key, defaultValue)` for flags that select between multiple variants or configuration strings. For example:

```csharp
var theme = client.GetStringValue("ui.theme", "light");

switch (theme)
{
    case "light": SetLightTheme(); break;
    case "dark":  SetDarkTheme();  break;
    default:      SetLightTheme(); break;
}
```

### Integer and double flags{% #integer-and-double-flags %}

For numeric flags, use `GetIntegerValue(key, defaultValue)` or `GetDoubleValue(key, defaultValue)`. These are appropriate when a feature depends on a numeric parameter such as a limit, percentage, or multiplier:

```csharp
var maxItems = client.GetIntegerValue("cart.items.max", 20);

var priceMultiplier = client.GetDoubleValue("pricing.multiplier", 1.0);
```

### Object flags{% #object-flags %}

For structured or JSON-like data, use `GetObjectValue(key, defaultValue)`. This method returns an `object`, which can be cast to the appropriate type. Object flags are useful for remote configuration scenarios where multiple properties need to be provided together. For example:

```csharp
var config = client.GetObjectValue(
    "ui.config",
    new Dictionary<string, object>
    {
        { "color", "#00A3FF" },
        { "fontSize", 14 },
    }
);

if (config is Dictionary<string, object> configDict)
{
    var color = configDict["color"] as string;
    var fontSize = (int)configDict["fontSize"];
}
```

### Flag evaluation details{% #flag-evaluation-details %}

When you need more than just the flag value, use the `Get<Type>Details` methods. These methods return both the evaluated value and metadata explaining the evaluation:

- `GetBooleanDetails(key, defaultValue)` -> `FlagDetails<bool>`
- `GetStringDetails(key, defaultValue)` -> `FlagDetails<string>`
- `GetIntegerDetails(key, defaultValue)` -> `FlagDetails<int>`
- `GetDoubleDetails(key, defaultValue)` -> `FlagDetails<double>`

For example:

```csharp
var details = client.GetStringDetails("paywall.layout", "control");

Debug.Log($"Value: {details.Value}");           // Evaluated value (for example: "A", "B", or "control")
Debug.Log($"Variant: {details.Variant}");       // Variant name, if applicable
Debug.Log($"Reason: {details.Reason}");         // Why this value was chosen (for example: "TARGETING_MATCH" or "DEFAULT")
Debug.Log($"Error: {details.Error?.ToString()}"); // The error that occurred during evaluation, if any
```

Flag details may help you debug evaluation behavior and understand why a user received a given value.

## Advanced configuration{% #advanced-configuration %}

The `DdFlags.Enable()` API accepts optional configuration with options listed below.

```csharp
DdFlags.Enable(new FlagsConfiguration(
    trackExposures: true,
    trackEvaluations: true,
    evaluationFlushIntervalSeconds: 10.0f
));
```

{% dl %}

{% dt %}
`trackExposures`
{% /dt %}

{% dd %}
When `true` (default), the SDK automatically records an *exposure event* when a flag is evaluated. These events contain metadata about which flag was accessed, which variant was served, and under what context. They are sent to Datadog so you can later analyze feature adoption. Set to `false` to disable exposure tracking.
{% /dd %}

{% dt %}
`trackEvaluations`
{% /dt %}

{% dd %}
When `true` (default), the SDK tracks flag evaluations and sends aggregated evaluation telemetry to Datadog. This enables analytics about flag usage patterns and performance. Set to `false` to disable evaluation tracking.
{% /dd %}

{% dt %}
`evaluationFlushIntervalSeconds`
{% /dt %}

{% dd %}
The interval in seconds at which batched evaluation events are sent to Datadog. Accepted values are between `1` and `60`. Default is `10.0` seconds.
{% /dd %}

{% dt %}
`customFlagsEndpoint`
{% /dt %}

{% dd %}
Configures a custom server URL for retrieving flag assignments.
{% /dd %}

{% dt %}
`customExposureEndpoint`
{% /dt %}

{% dd %}
Configures a custom server URL for sending flags exposure data.
{% /dd %}

{% dt %}
`customEvaluationEndpoint`
{% /dt %}

{% dd %}
Configures a custom server URL for sending flags evaluation telemetry.
{% /dd %}

{% /dl %}

## Further reading{% #further-reading %}

- [Client-Side Feature Flags](https://docs.datadoghq.com/feature_flags/client.md)
- [Unity Monitoring](https://docs.datadoghq.com/real_user_monitoring/application_monitoring/unity.md)
- [dd-sdk-unity source code](https://github.com/DataDog/dd-sdk-unity)
