Adding custom Persistent Variable for Color type

Is it possible to add a custom Persistent Variable that can store a Color type and be configured in the editor? I’m wondering if it’s possible to achieve this without modifying the original package code. Currently, I’m using a StringVariable to store the hex code of the color, but this approach is not very convenient.

Yes we support custom variables.
https://docs.unity3d.com/Packages/com.unity.localization@1.5/manual/Smart/Persistent-Variables-Source.html#custom-persistent-variables

You should be able to do something like the MyVariable example but with a Color property.

E.G

[Serializable]
public class MyColorVariable : IVariable, IVariableValueChanged
{
    [SerializeField]
    Color m_Value;

    public Color Value
    {
        get => m_Value;
        set
        {
            if (m_Value == value)
                return;

            m_Value = value;
            ValueChanged?.Invoke(this);
        }
    }

    public event Action<IVariable> ValueChanged;

    public object GetSourceValue(ISelectorInfo _) => Value;
}
1 Like

Thank you! Your solution worked perfectly. I appreciate the help.

9840879--1415922--upload_2024-5-17_17-2-45.png

1 Like

Great. You may want to use ColorUtility.ToHtmlStringRGB to do the hex, save a few lines of code :slight_smile:
You will need to prepend the “#” on the front though, it doesn’t include one.