BeginHorizontal allignment

Hey! I have this code:

EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("",style,GUILayout.Width(15),GUILayout.Height(15));
                EditorGUILayout.BeginHorizontal(helpbox);
                EditorGUILayout.LabelField(error,style,GUILayout.Width(15),GUILayout.Height(15));
                EditorGUILayout.LabelField("","Make sure the tool handle is placed at the active object's pivot point.");
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.EndHorizontal();

that gives me this:

But as you can see, the red icon is not centered in the box, how would I change this?

the variable ‘error’ is defined like this:

GUIContent error = new GUIContent();
        error = EditorGUIUtility.IconContent("CollabConflict");

style is defined like this:

GUIStyle style = new GUIStyle();

Maybe this helps you:

Yes, I tried that already but so far, no success :confused:

Try adding the following for your guistyle:
style.alignment = TextAnchor.MiddleLeft;

Does nothing :confused:

Can you show “style” declaration please ?

GUIStyle style = new GUIStyle();

I edited the post

You don’t need any of that BeginHorizontal stuff. Just display a label with a GUIContent that has an image. Unless I’m misunderstanding something…

class MyEditor : Editor {

    public override void OnInspectorGUI() {

        GUILayout.Label(Styles.error, Styles.helpbox);
    }

    static class Styles {

        internal static GUIContent error;
        internal static GUIStyle helpbox;

        static Styles() {

            error = EditorGUIUtility.IconContent("CollabConflict");
            error.text = " Make sure the tool handle is placed at the active object's pivot point.";

            helpbox = new GUIStyle(EditorStyles.helpBox);
            helpbox.alignment = TextAnchor.MiddleLeft;
        }
    }
}

Unity also has an inbuilt Helpbox if that’s what you’re looking for:

1 Like

Works perfectly! Thanks CDF for helping me out again :slight_smile:

I know of the existence of the Helpbox but I wanted to override the error sign. I like the “CollabConflict” better.

1 Like

I encountered an issue, with this code:

static class Styles {
 
        internal static GUIContent PivotError;
            internal static GUIContent DoorError;
            internal static GUIContent AngleError;
            internal static GUIContent SpeedError;
        internal static GUIStyle helpbox;
 
        static Styles()
                {
            PivotError = EditorGUIUtility.IconContent("CollabConflict");
            PivotError.text = " Make sure the tool handle is placed at the active object's pivot point.";

                        DoorError = EditorGUIUtility.IconContent("CollabConflict");
                        DoorError.text = " If your door is not scaled in Unity3D units and the pivot position is not already positioned correctly, the hinge algorithm will not work as expected.";

                        AngleError = EditorGUIUtility.IconContent("CollabConflict");
                        AngleError.text = " The difference between FinalAngle and InitialAngle should be less than 360°.";

                        SpeedError = EditorGUIUtility.IconContent("CollabConflict");
                        SpeedError.text = " The speed of a rotation block should be higher than zero.";

            helpbox = new GUIStyle(EditorStyles.helpBox);
            helpbox.alignment = TextAnchor.MiddleLeft;
        }
    }

And then when I want to put a label on the screen like this:

GUILayout.Label(Styles.AngleError, Styles.helpbox);
GUILayout.Label(Styles.SpeedError, Styles.helpbox);

They both show up like this:

Ah yeah sorry, IconContent returns a cached GUIContent when possible. I’d make a simple wrapper function for it:

static Styles() {
          
    PivotError = IconContent("CollabConflict", " Make sure the tool handle is placed at the active object's pivot point.");
    DoorError = IconContent("CollabConflict", " If your door is not scaled in Unity3D units and the pivot position is not already positioned correctly, the hinge algorithm will not work as expected.");
    AngleError = IconContent("CollabConflict", " The difference between FinalAngle and InitialAngle should be less than 360°.");
    SpeedError = IconContent("CollabConflict", " The speed of a rotation block should be higher than zero.");

    helpbox = new GUIStyle(EditorStyles.helpBox);
    helpbox.alignment = TextAnchor.MiddleLeft;
}

static GUIContent IconContent(string icon, string text) {

    GUIContent cached = EditorGUIUtility.IconContent(icon);

    return new GUIContent(text, cached.image);
}
1 Like