Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Skyblade · Jun 28, 2019 at 09:06 PM · editoreditor-scriptingeditorwindoweditorguilayout

Too much space between EditorGUILayout.SelectableLabel

I want to place selectable text into custom EditorWindow.

Here is my code:

 using UnityEditor;
 using UnityEngine;
 using System;
 
 public class MyEditorWindow : EditorWindow
 {
     Vector2 scrollPosition = Vector2.zero;
 
     [MenuItem("Window/MyEditorWindow")]
     public static void ShowExample()
     {
         var wnd = GetWindow<MyEditorWindow>();
         wnd.titleContent = new GUIContent("MyEditorWindow");
     }
 
     private void OnGUI()
     {
         scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(position.width), GUILayout.Height(position.height));
 
         var elapsed = new TimeSpan();
         for (int i = 0; i < 100; i++)
         {
             elapsed = elapsed.Add(TimeSpan.FromSeconds(1));
         }
 
         for (int i = 0; i < 100; i++)
         {
             EditorGUILayout.SelectableLabel("Some long long long long text");
         }
 
         GUILayout.EndScrollView();
     }
 }

It works, but there is too much space between the lines: alt text

How to make it smaller?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Jun 28, 2019 at 10:30 PM

The EditorGUILayout.SelectableLabel is defined like this:

 public static void SelectableLabel(string text, GUIStyle style, params GUILayoutOption[] options)
 {
     Rect position = EditorGUILayout.GetControlRect(false, 32f, style, options);
     EditorGUILayout.s_LastRect = position;
     EditorGUI.SelectableLabel(position, text, style);
 }

As you can see it allocates a fix layout rectangle of 32 pixels height. It does not adjust to the content. You essentially have two options:

  • use GUILayout.Height() to define a fix height for the label (default single line height is usually 15).

  • allocate the rect for the control yourself and use EditorGUI.SelectableLabel


Keep in mind that if you draw too many things in OnGUI it will have a huge impact on the performance of the editor. 100 items shouldn't be too bad, however 500+ will probably be and issue.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jun 28, 2019 at 10:35 PM 0
Share

Note if you have more questions regarding the I$$anonymous$$GUI system, have a look at my I$$anonymous$$GUI crash course (Since Unity Answers has trouble viewing markdown properly, here's a more readable mirror)

avatar image Skyblade · Jun 29, 2019 at 06:31 PM 0
Share

Omega cool, thanks!

avatar image
0

Answer by _geo__ · May 13 at 09:36 AM

An implementation of what @Bunny83 suggested ("allocate the rect for the control yourself "). For those in need of a quick copy n paste Solution ;-)

 public static void DrawSelectableLabel(string text, GUIStyle style = null)
 {
     if (style == null)
         style = GUI.skin.label;
     
     var content = new GUIContent(text);
     var position = GUILayoutUtility.GetRect(content, style);
     EditorGUI.SelectableLabel(position, text, style);
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

152 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity editor extension - create drag and drop (similar to Buildbox) 1 Answer

why I Cant Add Multiple Objects to the "Object Field" in Editor Window? 3 Answers

Create a custom editor window with repeating subsection (mockup included) 0 Answers

Custom Editor - Is there any way to detect whether the user is in Prefab editing mode? 1 Answer

Organizing Recursive EditorGUI elements 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges