Search Unity

GUI.DrawTexture & GUI.Label color do not match visually, even though same value

Discussion in 'Editor & General Support' started by MvNimwegen, Oct 29, 2021.

  1. MvNimwegen

    MvNimwegen

    Joined:
    Dec 19, 2019
    Posts:
    56
    Hi!

    I am trying to draw some text over a drawn texture in the scene view. I generate a texture with a given color, which I draw in the Scene View using
    GUI.DrawTexture
    . I then render text on screen using
    GUI.Label
    , for which I create a
    GUIStyle
    for which I set its
    normal.textColor
    to that same color. Yet these colors do not match visually.
    It seems that the textcolor has some offset which causes the color to shift a little.

    This is the code I use to do this:
    Code (CSharp):
    1.         //Calculate 2d position on screen from 3d position
    2.         float sceneViewControlBarHeight = 21f;
    3.         Vector2 screenPosition = SceneView.currentDrawingSceneView.camera.WorldToScreenPoint(_component.transform.position);
    4.         Vector2 invertedScreenPosition = new Vector2(screenPosition.x, (-screenPosition.y + SceneView.lastActiveSceneView.position.height - sceneViewControlBarHeight));
    5.  
    6.         //Create GUIStyle
    7.         GUIStyle style = new GUIStyle(GUI.skin.label);
    8.         style.normal.textColor = _component.TextColor;
    9.         style.fontSize = 48;
    10.  
    11.         //Create image Rect
    12.         Rect imageRect = new Rect(invertedScreenPosition.x, invertedScreenPosition.y, 265, 90);
    13.  
    14.         //Draw background, border, icon and label
    15.         Handles.BeginGUI();
    16.         GUI.DrawTexture(imageRect, _component.BackgroundTexture, ScaleMode.StretchToFill, true, 0f, _component.BackgroundColor, 0f, 30);
    17.         GUI.DrawTexture(imageRect, _component.BorderTexture, ScaleMode.StretchToFill, true, 0f, _component.TextColor, 10, 30);
    18.         GUI.Label(imageRect, "Some Label", style);
    19.         Handles.EndGUI();
    And here is what it looks like:
    Unity_Zt6CyF5gXn.png Unity_V7VQtFyJFO.png

    Although this effect does not show equally for all colors, as seen here:
    Unity_VsXX1QIZiQ.png

    Can somebody tell me why this effect occurs, and how I might be able to fix it?

    Thank you!
     
  2. MvNimwegen

    MvNimwegen

    Joined:
    Dec 19, 2019
    Posts:
    56
    SOLVED (sorta):
    During reviewing I the post once more after posting, I realized my own mistake. By setting the color in the
    GUI.DrawTexture
    , I created that offset. By setting those color to
    Color.White
    , both colors are now offset. They are now both not exactly the color that has been set, but at least the are equal now. That fixes my problem for now, even though I still do not know why the offset in color is there...