1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. Unity 2017.1 beta is now available for download.
    Dismiss Notice
  5. Unity 5.6 is now released.
    Dismiss Notice
  6. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  8. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice

BeginHorizontal allignment

Discussion in 'Extensions & OnGUI' started by alexanderameye, May 11, 2017.

  1. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    222
    Hey! I have this code:
    Code (CSharp):
    1.                 EditorGUILayout.LabelField("",style,GUILayout.Width(15),GUILayout.Height(15));
    2.                 EditorGUILayout.BeginHorizontal(helpbox);
    3.                 EditorGUILayout.LabelField("","Make sure the tool handle is placed at the active object's pivot point.");
    4.                 EditorGUILayout.EndHorizontal();
    5.                 EditorGUILayout.EndHorizontal();
    that gives me this:

    [​IMG]

    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.         error = EditorGUIUtility.IconContent("CollabConflict");
    style is defined like this:

    Code (CSharp):
     
    Last edited: May 11, 2017
  2. roykoma

    roykoma

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

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    222
  4. roykoma

    roykoma

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

    alexanderameye

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

    PsyKaw

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

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    222
    Code (CSharp):
    I edited the post
     
  8. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    329
    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
     
  9. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    222
    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:
    222

    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:

    [​IMG]
     
  11. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    329
    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.