Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug Colors for DrawSolidRectangleWithOutline broken?

Discussion in 'Editor & General Support' started by thedevilsjester, Feb 9, 2021.

  1. thedevilsjester

    thedevilsjester

    Joined:
    May 21, 2018
    Posts:
    25
    I have the following Editor window code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. public class TestEditor : EditorWindow
    4. {
    5.     [MenuItem("Window/TestEditor")]
    6.     public static void ShowWindow()
    7.     {
    8.         EditorWindow.GetWindow(typeof(TestEditor));
    9.     }
    10.     void OnGUI()
    11.     {
    12.         for (int i = 0; i < 2; i++)
    13.         {
    14.             Handles.DrawSolidRectangleWithOutline(new Rect(10, 10 + (30 * i), 100, 5), Color.green, Color.blue);
    15.             Handles.color = Color.red;
    16.         }
    17.     }
    18. }
    19.  
    The results are ... not what I expect. The first rectangle draws correctly, the second one is black.
    TestEditor1.png

    If I comment out the Handles.color = Color.Red; portion, everything draws correctly.

    TestEditor2.png

    It seems that any change to the Handles.color causes all subsequent calls to DrawSolidRectangleWithOutline to draw black.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,072
    These (like most) colors are multiplied. Red (1,0,0) times Green (0,1,0) is black.

    Same with red (1,0,0) times blue (0,0,1).

    If you always draw white you can control the colors effectively with the Handles.color property.

    EDIT: looking at what you're doing above, I suggest you simply decide the color you want from a table already, pass it into the box call directly, no need for stateful Handle.color calls.
     
    MvNimwegen likes this.
  3. thedevilsjester

    thedevilsjester

    Joined:
    May 21, 2018
    Posts:
    25
    I noticed this shortly after posting this topic. Is that a bug, or is it the intended behavior? It does not mention this behavior in the documentation for DrawSolidRectangleWithOutline.

    The above is just an example. I draw many objects, most of which require the Handle.color property.

    For now I can just set the color to white, as you have noted, but it would be nice if either the documentation was clarified or this was fixed to not require resetting the color.
     
  4. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,319
    The
    Handles.color
    property is intended to modify the colors of other things painted through Handles, not replace them. Like @Kurt-Dekker said, this modification is a pointwise multiplication: Red (1,0,0) x Green (0,1,0) = (1*0,0*1,0*0) = Black (0,0,0). This is intended behavior.

    Were you expecting
    DrawSolidRectangleWithOutline
    not to be affected by
    Handles.color
    ? Rule of thumb for editor windows is that everything is filtered through the current color.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,072
    Almost every use of color in shader-ish logic is multiplied color: sprites, materials, UI.Images GUI, GUISkins, Lights, projectors, etc.

    It's also kinda how the real world works. Try shining red light on green plants sometime.

    I think they take it for granted.
     
  6. thedevilsjester

    thedevilsjester

    Joined:
    May 21, 2018
    Posts:
    25
    I understand the concept, and why its not considered a bug, and it makes sense when taken in that context, but adding a little bit to the Handles.color documentation that says its a multiplier, would have saved me hours of debugging and headache; and might just help the next person. At the very least, this topic might show up if they Google the problem.
     
    Last edited: Feb 9, 2021
    ruudvangaal and Madgvox like this.