Search Unity

Question Gradients in visual scripting ?

Discussion in 'Visual Scripting' started by MoussaNeggaz, Nov 21, 2022.

  1. MoussaNeggaz

    MoussaNeggaz

    Joined:
    Nov 4, 2021
    Posts:
    18
    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 !
     
  2. TimHermelink

    TimHermelink

    Joined:
    Jan 20, 2022
    Posts:
    14
    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
     
  3. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,063
    You could also grab Dotween from the asset store and use Image.DOColor(Color to, float duration) shortcut.
     
  4. OBiwer

    OBiwer

    Joined:
    Aug 2, 2022
    Posts:
    61
    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:
    Code (CSharp):
    1. [CustomPropertyDrawer(typeof(Gradient))]
    2. public class GradientPropertyDrawer : PropertyDrawer
    3. {
    4.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    5.     {
    6.         // yes, this is stupid, and only for the visual scripting system:
    7.         EditorGUI.PropertyField(position, property, label);
    8.     }
    9. }
    1. Go To: Project settings > Visual Scripting
    2. in Type Options add "Gradient"
    3. Press Button: Regenerate Node Library
    4. Press Button Generate in "Custom Inspector Properties"

    Now you can use Gradients in Visual Scripting like everything else

    upload_2023-1-6_11-6-24.png
     
    Last edited: Jan 6, 2023
    skoteskote likes this.