Rich Gradient v1.0.0

Download on Asset Store

1.1. Overview

Rich Gradient is a serializable gradient type for Unity with up to 32 color keys and 32 alpha keys — four times what Unity's built-in Gradient allows, which helps with multi-stop looks like skies, status bars, or stylized lighting. Drop a RichGradient field into any MonoBehaviour or ScriptableObject exactly like a regular gradient field, and get a dedicated editor window, an OKLab-based Perceptual Blend mode for smoother transitions between saturated hues, and two-way conversion to and from Unity's native Gradient.

It has no render-pipeline dependency. Continue with Installation or jump straight to Quickstart.

1.2. Installation

Import Rich Gradient from the Unity Asset Store. The package files are added under:

Assets/Octopus Game Lab/Rich Gradient

Continue with Quickstart to add your first gradient field.

1.3. Quickstart

Add a RichGradient field to any component. A new gradient is white and fully opaque.

using OctopusGameLab.RichGradient;
using UnityEngine;

public class GradientUser : MonoBehaviour
{
    [SerializeField] private RichGradient _gradient = new();

    private void Start()
    {
        Color mid = _gradient.Evaluate(0.5f);
        Debug.Log(mid);
    }
}

Evaluate takes a time from 0 to 1 and returns the color, including alpha. Values outside that range are clamped. Times before the first key or after the last key use that endpoint key.

RichGradient swatch field in the Unity Inspector
The RichGradient swatch as it appears in the Inspector.

1.4. Requirements

  • Unity 2022.3 or newer (tested in 2022.3, 2023.2, and 6000.0)
  • No other package dependencies
  • No render-pipeline dependency — tested in the Built-in and Universal Render Pipelines
  • Works in both Linear and Gamma color space projects

2.1. Editor Basics

A serialized RichGradient is drawn as a swatch. Click the swatch, or focus it and press Space or Enter, to open the editor. It works much like Unity's own gradient editor:

  • Click an empty spot on the top row to add an alpha key, or on the bottom row to add a color key.
  • Drag a key to move it.
  • Drag a key away from its row to remove it.
  • Press Delete or Backspace to remove the selected key.
  • One color key and one alpha key always remain, and each row holds at most 32 keys.
  • Alpha is edited with its own slider — the color field does not edit alpha.
Rich Gradient Editor window with several color and alpha keys
The Rich Gradient Editor window.

2.2. Gradient Modes

Mode controls how both color and alpha interpolate between keys:

Mode Color Alpha
Blend Interpolates in RGB Interpolates
Fixed Holds each key until the next key Holds each key until the next key
PerceptualBlend Interpolates in OKLab color space Interpolates

Perceptual blending keeps transitions between saturated colors more even than RGB blending.

_gradient.Mode = RichGradientMode.PerceptualBlend;
Comparison of Blend and PerceptualBlend across saturated hues
Blend vs. PerceptualBlend across saturated hues.

3.1. RichGradient API

All types are in the OctopusGameLab.RichGradient namespace.

Member Description
Evaluate(float time) Returns the color at that time, including alpha.
SetKeys(colorKeys, alphaKeys) Replaces both sets of keys and raises OnChanged.
ColorKeys / AlphaKeys Read or replace one set of keys.
Mode Reads or sets the blend mode.
ColorKeyCount / AlphaKeyCount Current number of keys in each set.
Clone() Returns an independent copy.
OnChanged Raised after the keys or the mode change from code. Inspector edits do not raise it.
MaxKeyCount 32 — the maximum number of color or alpha keys.

Keys passed to the API are clamped to the 0–1 range, sorted by time, and limited to 32. Extra keys are discarded.

3.2. Color & Alpha Keys

RichGradientColorKey stores Color and Time. RichGradientAlphaKey stores Alpha and Time.

var colorKeys = new[]
{
    new RichGradientColorKey(Color.red, 0f),
    new RichGradientColorKey(Color.yellow, 0.5f),
    new RichGradientColorKey(Color.blue, 1f)
};

var alphaKeys = new[]
{
    new RichGradientAlphaKey(1f, 0f),
    new RichGradientAlphaKey(0f, 1f)
};

3.3. Unity Gradient Interop

FromGradient(Gradient) copies a Unity gradient into a new RichGradient. ToGradient() copies a RichGradient back into a Unity Gradient.

ToGradient() keeps at most 8 color keys and 8 alpha keys, because that is Unity's Gradient limit. Keys are sorted by time first, so the earliest 8 of each type are kept. See also Limitations.
RichGradient fromUnity = RichGradient.FromGradient(unityGradient);
Gradient backToUnity = fromUnity.ToGradient();

3.4. Code Examples

Reacting to OnChanged to refresh a texture whenever the keys or mode change from code:

private void OnEnable()
{
    _richGradient.OnChanged += UpdateTexture;
}

private void OnDisable()
{
    _richGradient.OnChanged -= UpdateTexture;
}

private void UpdateTexture()
{
    for (int x = 0; x < _texture.width; x++)
    {
        float time = x / (_texture.width - 1f);
        _texture.SetPixel(x, 0, _richGradient.Evaluate(time));
    }

    _texture.Apply();
}

4.1. FAQ

Why does ToGradient() show fewer keys than my Rich Gradient?

Unity's Gradient is limited to 8 keys. ToGradient() sorts by time and keeps the earliest 8 of each type.

Can I use HDR colors?

Not currently — the editor color field does not support HDR colors. It also does not support the eyedropper, since Unity's editor API does not expose that tool to custom color fields.

Does Rich Gradient depend on URP or HDRP?

No. It has no render-pipeline dependency and works in any pipeline.

4.2. Limitations

  • ToGradient() keeps at most 8 color keys and 8 alpha keys, because that is Unity's Gradient limit. Keys are sorted by time first, so the earliest 8 of each type are kept.
  • Keys beyond 32 are discarded without a warning.
  • The editor color field does not support HDR colors.
  • The eyedropper is not available either, since Unity's editor API does not expose that tool to custom color fields.

4.3. Changelog

Release notes for Rich Gradient are maintained in the package changelog.

View Changelog

4.4. Contact

Have a question, found a bug, or have feedback? Send us an email.

octopusgamelab@gmail.com