IStylerHandler
Connect your own components to Styler presets and runtime theme changes
Overview
IStylerHandler lets your own components participate in Styler. Implement it when a custom widget, effect, or other component needs to read values from a preset and respond to theme changes. You decide how those values affect your component.
For standard graphics and text, Styler Object already handles the common cases. A custom handler is useful when you need to style something it does not cover, or when your component owns several visuals that should update together.
Usage
Implement Evo.UI.IStylerHandler on your component.
Store a StylerPreset reference and expose it through the Preset property. Its setter should call UpdateStyler() when the preset changes.
In UpdateStyler(), look up the required IDs and apply the values to your component. Keep this method safe to call repeatedly.
Apply your initial values and call Styler.Register(this) in OnEnable(). Call Styler.Unregister(this) in OnDisable().
Assign a preset and the component's target references in the Inspector. Use the same entry IDs in each theme.
Once registered, your component receives global changes made through Styler.ApplyPreset(). You can also target it directly with Styler.ApplyPresetTo(preset, handler) or apply a preset to a GameObject hierarchy. See the Styler Public API for these methods.
Customization
Choose what to style
Use the Styler Preset API to resolve colors, fonts, sprites, gradients, audio, text styles, or shape styles. Prefer TryGet...() when an entry might be missing. You can leave the current value in place, as the example does, or apply your own fallback.
The interface does not require a particular set of IDs or a specific visual component. One handler can update several targets from the same preset.
Support color transitions
Within UpdateStyler(), use ColorTransition.SetGraphicColor(targetGraphic, color) for a Unity UI Graphic. When the update comes from Styler.ApplyPreset() or Styler.ApplyPresetTo() with a transition duration, the color blends as part of that operation. A direct assignment to targetGraphic.color changes it immediately.
For example, Styler.ApplyPresetTo(theme, handler, transitionDuration: 0.25f) applies a theme with a quarter-second color transition. Fonts, sprites, and other values still change according to your own update code.
Expose interaction states
IStylerInteractable is a separate interface for custom Selectables that should supply state changes to Styler Object. It exposes the current InteractionState, a TransitionDuration, and an Action<InteractionState> event named OnStateChanged. Implement it and raise the event when your control's state changes. A component only needs IStylerHandler to receive preset changes.
Handler Lifecycle
- The
Presetsetter matters: when applying a different preset, Styler assigns this property. If the preset is already assigned, Styler callsUpdateStyler()directly to refresh its values. - Assign a preset before registering if the handler should inherit an existing
Styler.PersistentPreset. A handler whosePresetis null does not inherit it during registration. If you want the configured default as a fallback, resolve it withStyler.GetDefaultPreset()before registering. - A disabled handler that unregisters is excluded from global updates. Applying to a GameObject hierarchy can still reach inactive components, so check references inside
UpdateStyler()instead of relying only onStart()to initialize them. - Unregistering also cancels color transitions owned by a handler that derives from
Behaviour, such as the MonoBehaviour below. - Keep the custom component and preset assets in your own project folder so replacing the Evo UI package does not remove them.
Public API
Implement IStylerHandler from the Evo.UI namespace. The interface has one property and one method.
Properties
| Name | Type | Description |
|---|---|---|
Preset | StylerPreset | Read/write property for the handler's preset. Apply a newly assigned preset in the setter. |
Methods
| Name | Parameters | Description |
|---|---|---|
UpdateStyler() | None | Resolves the current preset's values and applies them to the component. |
Example
Add this component to your object, assign a preset and a UI Graphic, then set Color ID to an entry in that preset. The example leaves the graphic unchanged if the preset or color entry is missing.
using UnityEngine;
using UnityEngine.UI;
using Evo.UI;
public class CustomStylerHandler : MonoBehaviour, IStylerHandler
{
[SerializeField] private StylerPreset preset;
[SerializeField] private Graphic targetGraphic;
[SerializeField] private string colorID = "Accent";
public StylerPreset Preset
{
get => preset;
set
{
if (preset == value)
return;
preset = value;
UpdateStyler();
}
}
private void OnEnable()
{
UpdateStyler();
Styler.Register(this);
}
private void OnDisable()
{
Styler.Unregister(this);
}
public void UpdateStyler()
{
if (preset == null || targetGraphic == null)
return;
if (preset.TryGetColor(colorID, out var color))
ColorTransition.SetGraphicColor(targetGraphic, color);
}
}