Gradients in visual scripting ?

Hello,
I would like to do an HP bar wich changes colors when the player loses HP. However i don’t know how to use gradients in visual scripting, could someone help me with that ?
Thanks a lot !

You can use “Color.lerp” for this.
If your HP varible goes from 0 to 100, make sure to divide it by 100 so it ends up being 0 to 1

You could also grab Dotween from the asset store and use Image.DOColor(Color to, float duration) shortcut.

Sadly, “Gradient” is missing in the stuff you can do with VisualScripting, even though it’s perfectly serializable by unity, you can’t create a GradientVariable in VisualScripting ‍♂️

A Gradient can obviously do a lot more stuff than a simple lerp.

First I thought: okay, just create a custom node for gradients should be easy, but the visual scripting system just shows a warning instead of the GradientField, that there is no drawer for it.
I then tried to wrap the Gradient into a custom type with a custom drawer, but it just ignored my custom drawer.

I then tried to add “Gradient” in the project settings > Visual Scripting to the Type Options and, while it now shows as a type and you can use all of it’s fuctions, you can’t setup the Gradient in the inspector. You’d have to create a script which modify’s the Gradient on the “Variables”-Component for you.

Edit:

Okay, now it worked:

add this script in an Editor folder or in an Editor asmdef:

[CustomPropertyDrawer(typeof(Gradient))]
public class GradientPropertyDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        // yes, this is stupid, and only for the visual scripting system:
        EditorGUI.PropertyField(position, property, label);
    }
}
  • Go To: Project settings > Visual Scripting
  • in Type Options add “Gradient”
  • Press Button: Regenerate Node Library
  • Press Button Generate in “Custom Inspector Properties”

Now you can use Gradients in Visual Scripting like everything else

Thanks for posting this! It would be great if Unity could just Update this internally, as well. I was getting the same thing while adding the Node in Settings, where it was not available in the Inspector.

Update: Just wanted to say this works amazingly!!! I am able to set different Gradients for my VFX Graph(s) now, via Visual Scripting. Thanks Again!