# Gradient

> Represents a Gradient used for animating colors.

## Definition

* **Type:** Class
* **Namespace:** [UnityEngine](/engine/6000.6/script-reference/unityengine.md)
* **Assembly:** UnityEngine.CoreModule
* **Implements:** [IEquatable\<Gradient>](https://learn.microsoft.com/dotnet/api/system.iequatable-1)

```csharp
public class Gradient : IEquatable<Gradient>
```

## Remarks

Gradients allow animating or interpolating colors by having several "color keys" and "alpha keys". Color keys and alpha keys are separate, and each key has a time specified for it, ranging from 0.0 (0%) to 1.0 (100%). Note that the alpha and colors keys will be automatically sorted by time value and that it is ensured to always have a minimum of 2 color keys and 2 alpha keys.

How the colors are interpolated between the keys is controlled by [GradientMode](/engine/6000.6/script-reference/unityengine/gradientmode.md).

Public Gradient variables used in scripts automatically display the gradient editor in the inspector window. [GradientUsageAttribute](/engine/6000.6/script-reference/unityengine/gradientusageattribute.md) allows specifying whether the gradient colors should be high dynamic range for editing.

```csharp
using UnityEngine;

public class ExampleScript : MonoBehaviour
{
    void Start()
    {
        var gradient = new Gradient();

        // Blend color from red at 0% to blue at 100%
        var colors = new GradientColorKey[2];
        colors[0] = new GradientColorKey(Color.red, 0.0f);
        colors[1] = new GradientColorKey(Color.blue, 1.0f);

        // Blend alpha from opaque at 0% to transparent at 100%
        var alphas = new GradientAlphaKey[2];
        alphas[0] = new GradientAlphaKey(1.0f, 0.0f);
        alphas[1] = new GradientAlphaKey(0.0f, 1.0f);

        gradient.SetKeys(colors, alphas);

        // What's the color at the relative time 0.25 (25%) ?
        Debug.Log(gradient.Evaluate(0.25f));
    }
}
```

Additional Resources: [GradientColorKey](/engine/6000.6/script-reference/unityengine/gradientcolorkey.md), [GradientAlphaKey](/engine/6000.6/script-reference/unityengine/gradientalphakey.md), [SerializedProperty.gradientValue](/engine/6000.6/script-reference/unityeditor/serializedproperty/gradientvalue.md).

## Constructors

| Constructor                                                                | Description                   |
| -------------------------------------------------------------------------- | ----------------------------- |
| [Gradient()](/engine/6000.6/script-reference/unityengine/gradient/ctor.md) | Create a new Gradient object. |

## Properties

| Property                                                                               | Description                                                       |
| -------------------------------------------------------------------------------------- | ----------------------------------------------------------------- |
| [alphaKeyCount](/engine/6000.6/script-reference/unityengine/gradient/alphakeycount.md) | The number of alpha keys defined in the gradient.                 |
| [alphaKeys](/engine/6000.6/script-reference/unityengine/gradient/alphakeys.md)         | All alpha keys defined in the gradient.                           |
| [colorKeyCount](/engine/6000.6/script-reference/unityengine/gradient/colorkeycount.md) | The number of color keys defined in the gradient.                 |
| [colorKeys](/engine/6000.6/script-reference/unityengine/gradient/colorkeys.md)         | All color keys defined in the gradient.                           |
| [colorSpace](/engine/6000.6/script-reference/unityengine/gradient/colorspace.md)       | Indicates the color space that the gradient color keys are using. |
| [mode](/engine/6000.6/script-reference/unityengine/gradient/mode.md)                   | Controls how the gradient colors are interpolated.                |

## Methods

| Method                                                                               | Description                                                |
| ------------------------------------------------------------------------------------ | ---------------------------------------------------------- |
| [Evaluate](/engine/6000.6/script-reference/unityengine/gradient/evaluate.md)         | Calculate color at a given time.                           |
| [GetAlphaKeys](/engine/6000.6/script-reference/unityengine/gradient/getalphakeys.md) | Get all the alpha keys assigned to the gradient.           |
| [GetColorKeys](/engine/6000.6/script-reference/unityengine/gradient/getcolorkeys.md) | Get all the color keys assigned to the gradient.           |
| [SetAlphaKeys](/engine/6000.6/script-reference/unityengine/gradient/setalphakeys.md) | Setup gradient with an array of alpha keys.                |
| [SetColorKeys](/engine/6000.6/script-reference/unityengine/gradient/setcolorkeys.md) | Setup gradient with an array of color keys.                |
| [SetKeys](/engine/6000.6/script-reference/unityengine/gradient/setkeys.md)           | Setup Gradient with an array of color keys and alpha keys. |
