Search Unity

BeginHorizontal allignment

Discussion in 'Immediate Mode GUI (IMGUI)' started by alexanderameye, May 11, 2017.

  1. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    1,383
    Hey! I have this code:
    Code (CSharp):
    1. EditorGUILayout.BeginHorizontal();
    2.                 EditorGUILayout.LabelField("",style,GUILayout.Width(15),GUILayout.Height(15));
    3.                 EditorGUILayout.BeginHorizontal(helpbox);
    4.                 EditorGUILayout.LabelField(error,style,GUILayout.Width(15),GUILayout.Height(15));
    5.                 EditorGUILayout.LabelField("","Make sure the tool handle is placed at the active object's pivot point.");
    6.                 EditorGUILayout.EndHorizontal();
    7.                 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:

    Code (CSharp):
    1. GUIContent error = new GUIContent();
    2.         error = EditorGUIUtility.IconContent("CollabConflict");
    style is defined like this:

    Code (CSharp):
    1. GUIStyle style = new GUIStyle();
     
    Last edited: May 11, 2017
  2. roykoma

    roykoma

    Joined:
    Dec 9, 2016
    Posts:
    176
  3. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    1,383
  4. roykoma

    roykoma

    Joined:
    Dec 9, 2016
    Posts:
    176
    Try adding the following for your guistyle:
    style.alignment = TextAnchor.MiddleLeft;
     
  5. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    1,383
    Does nothing :/
     
  6. PsyKaw

    PsyKaw

    Joined:
    Aug 16, 2012
    Posts:
    102
    Can you show "style" declaration please ?
     
  7. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    1,383
    Code (CSharp):
    1. GUIStyle style = new GUIStyle();
    I edited the post
     
  8. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    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...
    Code (CSharp):
    1. class MyEditor : Editor {
    2.  
    3.     public override void OnInspectorGUI() {
    4.  
    5.         GUILayout.Label(Styles.error, Styles.helpbox);
    6.     }
    7.  
    8.     static class Styles {
    9.  
    10.         internal static GUIContent error;
    11.         internal static GUIStyle helpbox;
    12.  
    13.         static Styles() {
    14.  
    15.             error = EditorGUIUtility.IconContent("CollabConflict");
    16.             error.text = " Make sure the tool handle is placed at the active object's pivot point.";
    17.  
    18.             helpbox = new GUIStyle(EditorStyles.helpBox);
    19.             helpbox.alignment = TextAnchor.MiddleLeft;
    20.         }
    21.     }
    22. }
    Unity also has an inbuilt Helpbox if that's what you're looking for:

    https://docs.unity3d.com/ScriptReference/EditorGUILayout.HelpBox.html
     
    DavidMiranda likes this.
  9. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    1,383
    Works perfectly! Thanks CDF for helping me out again :)

    I know of the existence of the Helpbox but I wanted to override the error sign. I like the "CollabConflict" better.
     
    CDF likes this.
  10. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    1,383

    I encountered an issue, with this code:

    Code (CSharp):
    1. static class Styles {
    2.  
    3.         internal static GUIContent PivotError;
    4.             internal static GUIContent DoorError;
    5.             internal static GUIContent AngleError;
    6.             internal static GUIContent SpeedError;
    7.         internal static GUIStyle helpbox;
    8.  
    9.         static Styles()
    10.                 {
    11.             PivotError = EditorGUIUtility.IconContent("CollabConflict");
    12.             PivotError.text = " Make sure the tool handle is placed at the active object's pivot point.";
    13.  
    14.                         DoorError = EditorGUIUtility.IconContent("CollabConflict");
    15.                         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.";
    16.  
    17.                         AngleError = EditorGUIUtility.IconContent("CollabConflict");
    18.                         AngleError.text = " The difference between FinalAngle and InitialAngle should be less than 360°.";
    19.  
    20.                         SpeedError = EditorGUIUtility.IconContent("CollabConflict");
    21.                         SpeedError.text = " The speed of a rotation block should be higher than zero.";
    22.  
    23.             helpbox = new GUIStyle(EditorStyles.helpBox);
    24.             helpbox.alignment = TextAnchor.MiddleLeft;
    25.         }
    26.     }
    And then when I want to put a label on the screen like this:

    Code (CSharp):
    1. GUILayout.Label(Styles.AngleError, Styles.helpbox);
    2. GUILayout.Label(Styles.SpeedError, Styles.helpbox);
    They both show up like this:

     
  11. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    Ah yeah sorry, IconContent returns a cached GUIContent when possible. I'd make a simple wrapper function for it:
    Code (CSharp):
    1. static Styles() {
    2.          
    3.     PivotError = IconContent("CollabConflict", " Make sure the tool handle is placed at the active object's pivot point.");
    4.     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.");
    5.     AngleError = IconContent("CollabConflict", " The difference between FinalAngle and InitialAngle should be less than 360°.");
    6.     SpeedError = IconContent("CollabConflict", " The speed of a rotation block should be higher than zero.");
    7.  
    8.     helpbox = new GUIStyle(EditorStyles.helpBox);
    9.     helpbox.alignment = TextAnchor.MiddleLeft;
    10. }
    11.  
    12. static GUIContent IconContent(string icon, string text) {
    13.  
    14.     GUIContent cached = EditorGUIUtility.IconContent(icon);
    15.  
    16.     return new GUIContent(text, cached.image);
    17. }
     
    alexanderameye likes this.