Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

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,197
    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,197
    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 :)
     
  5. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,197
    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
    LeftyRighty and elmar1028 like this.
  7. elmar1028

    elmar1028

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

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,814

    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. tiskolin

    tiskolin

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