Search Unity

Gradient Editor for the custom inspector? HOW???

Discussion in 'Scripting' started by elmar1028, Jan 18, 2016.

  1. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    Hi guys,

    I am trying to expose a Gradient variable in the custom inspector. It looks like this:



    Any ideas?

    Thanks! :)
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    I am out of blue links on Google, mate :( This is the first thing that came up in the search results, and I have no idea how to implement it into my existing codebase.
     
  4. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    It actually should work, and that look is intended. It's that you use it wrong. Try clicking on lower left or lower right drag 'lever'(or is it 'button'? Those small things that you can drag) and change color(click on white bar near 'color' and change it). Your gradient bar should change. The problem is by default it gradients between white and white both with alpha = 255 :)
     
    ragut79 likes this.
  5. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    There are no issues with the gradient editor itself. I am building a custom inspector (editor extension), and want a gradient editor in it.
     
  6. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    Following code creates what's on screenshot you posted in first post using only EditorGUILayout.
    Code (CSharp):
    1. using UnityEditor;
    2.  
    3. [CustomEditor(typeof(DebugScript))]
    4. public class DebugInspector : Editor
    5. {
    6.     public override void OnInspectorGUI()
    7.     {
    8.         EditorGUI.BeginChangeCheck();
    9.         SerializedObject serializedGradient = new SerializedObject(target);
    10.         SerializedProperty colorGradient = serializedGradient.FindProperty("gradient");
    11.         EditorGUILayout.PropertyField(colorGradient, true, null);
    12.         if (EditorGUI.EndChangeCheck())
    13.             serializedGradient.ApplyModifiedProperties();
    14.     }
    15. }
    16. //Another file (DebugScript.cs):
    17. using UnityEngine;
    18. public class DebugScript : MonoBehaviour {
    19.     public Gradient gradient;
    20. }
    21.  
    BTW code taken from
    If you want to have gradient editor's contents without window in same inspector(you didn't say that, that's a guess) though... I doubt that's possible. You'll have to write it from scratch.
     
    Last edited: Jan 19, 2016
    delait-7, LeftyRighty and elmar1028 like this.
  7. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    That's exactly what I was looking for. It worked! Thank you very much! :D
     
  8. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,971

    Apparently it needs a line of code to apply modified properties:

    After the EditorGUILayout line...

    Code (CSharp):
    1. serializedGradient.ApplyModifiedProperties();
     
    Last edited: Jan 18, 2018
  9. ElnuDev

    ElnuDev

    Joined:
    Sep 24, 2017
    Posts:
    298
    Just use
    public Gradient gradient;
    , then double-click on it to open the advanced menu.:)
     
  10. The_Pied_Shadow

    The_Pied_Shadow

    Joined:
    Jul 25, 2018
    Posts:
    14
    I'm going to throw into this thread because it was the most useful thing for me but didn't actually end up solving my issue here in 2020. But literally the solution for me was to serialize the gradient variable in the target class.

    Code (CSharp):
    1. [HideInInspector] [SerializeField] Gradient gradient;
    Then in my editor class the normal strategy worked fine of creating a serialized property and using the EditorGUILayout.PropertyField. It was possibly a stupid moment for me. If I understood the concept of serialization more perhaps it would have been a 'no duh' moment but so far all the other properties don't require the serialization.