Search Unity

How can I change GUILayout.Label position and font color in Inspector ?

Discussion in 'Scripting' started by dubiduboni_unity, Mar 21, 2019.

  1. dubiduboni_unity

    dubiduboni_unity

    Joined:
    Feb 11, 2019
    Posts:
    116
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEditor;
    5.  
    6. [CustomEditor(typeof(GameObjectInfo))]
    7. public class GameObjectInfoButton : Editor
    8. {
    9.    public override void OnInspectorGUI()
    10.    {
    11.        DrawDefaultInspector();
    12.  
    13.        GameObjectInfo myScript = (GameObjectInfo)target;
    14.  
    15.        if (myScript.objectsinfo.Length == 0)
    16.        {
    17.            //GUI.Label(new Rect(0, 10, 10, 10), "No Results");
    18.            GUILayout.Label("No Results");
    19.        }
    20.        else
    21.        {
    22.            GUILayout.Label("Found Results");
    23.        }
    24.    }
    25. }
    26.  
    GUI.Label have a Rect position parameter but it's never show in the Inspector. GUILayout does show in the Inspector but I want it to be in the center and with a bit space from the rest and to change the font colors. "No Results" in red and "Found Results" in black.
     
  2. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    676
    I believe that your call to GUI.Label actually is showing something in the Inspector, but not where you expect it. Those x and y coordinates (0 and 10 in Line 17) are with respect to the upper left corner of the entire Inspector window, now just the part dedicated to your GameObjectInfoButton. When I run your original code (with Line 17 not a comment), I can see the beginning of a letter "N" in the upper left of the Inspector window. The rest of your label is being overwritten by other graphics in the window.
     
  3. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    676
    Try this and see if it gives you any ideas on how to do what you want:

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. [CustomEditor(typeof(GameObjectInfo))]
    5. public class GameObjectInfoButton : Editor
    6. {
    7.     public override void OnInspectorGUI()
    8.     {
    9.         DrawDefaultInspector();
    10.  
    11.         Rect rect = EditorGUILayout.GetControlRect(false, 200f);
    12.  
    13.         Rect buttonRect = new Rect(
    14.             rect.x + rect.width / 4,
    15.             rect.y + rect.height / 4,
    16.             rect.width / 2,
    17.             rect.height / 2);
    18.  
    19.         GUI.Button(buttonRect, "Centered");
    20.     }
    21. }